43
Style & Design Principles Chapter 02: Design Patterns Nick Prühs

Style & Design Principles 02 - Design Patterns

Embed Size (px)

DESCRIPTION

Chapter 02 of the lecture Style & Design Principles taught at SAE Institute Hamburg. Introduction to advanced concepts of object-oriented design, such as delegation, polymorphism, cohesion and coupling, and to behavioral, creational and structural design patterns.

Citation preview

Page 1: Style & Design Principles 02 - Design Patterns

Style & Design PrinciplesChapter 02: Design Patterns

Nick Prühs

Page 2: Style & Design Principles 02 - Design Patterns

5 Minute Review Session

• Name a few characteristics of good code!

• How can you achieve good code?

• Tabs or spaces?

• When should you use a struct instead of a class?

• When should you use a method instead of a property?

• Name the three common interfaces and base classes that can be used for collections in .NET!

• What is the main purpose of the interface IEquatable?

• What is the main purpose of the interface IComparable?

• How are Equals and GetHashCode related to each other?

2 / 58

Page 3: Style & Design Principles 02 - Design Patterns

Assignment Solution #1

DEMO

3 / 58

Page 4: Style & Design Principles 02 - Design Patterns

Objectives

• To learn advances concepts of object-oriented design

• To understand the motivation behind design patterns

• To get an idea of the different types of design patterns and their application

4 / 78

Page 5: Style & Design Principles 02 - Design Patterns

Design Patterns

• General reusable solution to a commonly occurring problem within a given context

• Formalized best practices that the programmer must implement themselves in the application• Not a finished design that can be transformed directly

into source code

• Gained popularity in computer science after the book Design Patterns: Elements of Reusable Object-Oriented Software was published in 1994 by the so-called "Gang of Four" (Gamma et al.)

5 / 78

Page 6: Style & Design Principles 02 - Design Patterns

Advantages ofDesign Patterns• Speed up the development process by providing

tested, proven development paradigms

• Improve code readability for coders and architects who are familiar with the patterns

6 / 78

Page 7: Style & Design Principles 02 - Design Patterns

Design Pattern Types

• Creational (object creation)

• Structural (relationships between objects)

• Behavioral (communication between objects)

7 / 78

Page 8: Style & Design Principles 02 - Design Patterns

Object-Oriented Design 101

• Aggregation• Combine simple objects or data types into more

complex ones

• Usually expressed by means of references from one object to another

• Inheritance• Adding detail to a general data type to create a more

specific data type

8 / 78

Page 9: Style & Design Principles 02 - Design Patterns

Object-Oriented Design 101

• Delegation• Handing a task over to another part of the program

• Polymorphism• Ad hoc polymorphism (function overloading)

• Parametric polymorphism (generic programming)

• Subtyping (subclassing)

9 / 78

Page 10: Style & Design Principles 02 - Design Patterns

Object-Oriented Design 101

• Cohesion• Degree to which the elements of a module belong

together

• How much functionalities embedded in a class have in common

• Coupling• Degree to which each program module relies on the

other modules

10 / 78

Page 11: Style & Design Principles 02 - Design Patterns

Object-Oriented Design 101

• Cohesion• Degree to which the elements of a module belong

together

• How much functionalities embedded in a class have in common

• Coupling• Degree to which each program module relies on the

other modules

11 / 78

Page 12: Style & Design Principles 02 - Design Patterns

Why getters and settersare evil

“Don’t ask for the information you need to do the work; ask the object that has the information to do the work for you.”

- Allen Holub

12 / 78

Page 13: Style & Design Principles 02 - Design Patterns

Why getters and settersare evil

• Getter and setter methods are dangerous for the same reason that public fields are dangerous

• They’re okay if• They return interface references• You don’t know in advance how your class will be used

13 / 78

Page 14: Style & Design Principles 02 - Design Patterns

Behavioral Design Patterns

Communication Between Objects:

• Iterator

• Observer

• Command

• Memento

• Strategy

14 / 78

Page 15: Style & Design Principles 02 - Design Patterns

Iterator Pattern

Provides a way to access the elements of an

aggregate object sequentially without exposing

its underlying representation.

15 / 78

Examples:

• Contains• Where• Count

Page 16: Style & Design Principles 02 - Design Patterns

Iterator Pattern

Provides a way to access the elements of an

aggregate object sequentially without exposing

its underlying representation.

16 / 78

Page 17: Style & Design Principles 02 - Design Patterns

Observer Pattern

Subject maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods.

17 / 78

Examples:

• Event Handling

Page 18: Style & Design Principles 02 - Design Patterns

Observer Pattern

Subject maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods.

18 / 78

Page 19: Style & Design Principles 02 - Design Patterns

Command Pattern

Encapsulates all the information needed to call a method at a later time in an object.

19 / 78

Examples:

• Networking• Replays• Undo

Page 20: Style & Design Principles 02 - Design Patterns

Command Pattern

Encapsulates all the information needed to call a method at a later time in an object.

20 / 78

Page 21: Style & Design Principles 02 - Design Patterns

Command Pattern

Encapsulates all the information needed to call a method at a later time in an object.

21 / 78

Page 22: Style & Design Principles 02 - Design Patterns

Memento Pattern

Provides the ability to restore an object to its previous state.

22 / 78

Examples:

• Undo

Page 23: Style & Design Principles 02 - Design Patterns

Memento Pattern

Provides the ability to restore an object to its previous state.

23 / 78

Page 24: Style & Design Principles 02 - Design Patterns

Strategy Pattern

Defines a family of algorithms, encapsulates each one, and makes them interchangeable.

24 / 78

Examples:

• Calculator• Sorting• AI

Page 25: Style & Design Principles 02 - Design Patterns

Strategy Pattern

Defines a family of algorithms, encapsulates each one, and makes them interchangeable.

25 / 78

Page 26: Style & Design Principles 02 - Design Patterns

Creational Design Patterns

Object Creation:

• Prototype

• Factory

• Object Pool

• Singleton

26 / 78

Page 27: Style & Design Principles 02 - Design Patterns

Prototype Pattern

Objects are created using a prototypical instance, which is cloned to produce new objects.

27 / 78

C#public Map(Map map){

// Set width and height.this.Width = map.Width;this.Height = map.Height;

// Deep copy map tiles.this.Tiles = new MapTile[this.Width,this.Height];

for (var x = 0; x < this.Width; x++){

for (var y = 0; y < this.Height; y++){

this.Tiles[x, y] = new MapTile(map[x, y]);}

}}

Page 28: Style & Design Principles 02 - Design Patterns

Factory Method Pattern

Defines an interface for creating an object, but let the classes that implement the interface decide which class to instantiate.

28 / 78

Examples:

• Frameworks

Page 29: Style & Design Principles 02 - Design Patterns

Factory Method Pattern

Defines an interface for creating an object, but let the classes that implement the interface decide which class to instantiate.

29 / 78

Page 30: Style & Design Principles 02 - Design Patterns

Factory Method Pattern

Defines an interface for creating an object, but let the classes that implement the interface decide which class to instantiate.

30 / 78

Page 31: Style & Design Principles 02 - Design Patterns

Object Pool Pattern

Uses a set of initialized objects kept ready to use, rather than allocating and destroying them on demand.

31 / 78

Examples:

• Unity3D Game Objects• Database Connections• Threads

Page 32: Style & Design Principles 02 - Design Patterns

Object Pool Pattern

Uses a set of initialized objects kept ready to use, rather than allocating and destroying them on demand.

32 / 78

Page 33: Style & Design Principles 02 - Design Patterns

Singleton (Anti-)Pattern

Restricts the instantiation of a class to one object.

Disadvantages:

• Introduces unnecessary restrictions in situations where a sole instance of a class is not actually required

• Introduces global state into an application

• Needs to be thread-safe!

33 / 78

Page 34: Style & Design Principles 02 - Design Patterns

Singleton (Anti-)Pattern

Restricts the instantiation of a class to one object.

34 / 78

C#public class Singleton{

private static Singleton instance;

private Singleton() { }

public static Singleton Instance{

get{

return instance ?? (instance = new Singleton());}

}}

Page 35: Style & Design Principles 02 - Design Patterns

Structural Design Patterns

Relationships Between Objects:

• Composite

• Decorator

35 / 78

Page 36: Style & Design Principles 02 - Design Patterns

Composite Pattern

Treats a group of objects in the same way as a single instance of an object.

36 / 78

Examples:

• Files and Directories

Page 37: Style & Design Principles 02 - Design Patterns

Composite Pattern

Treats a group of objects in the same way as a single instance of an object.

37 / 78

Page 38: Style & Design Principles 02 - Design Patterns

Decorator Pattern

Allows behavior to be added to an individual object, either statically or dynamically, without affecting the behavior of other objects from the same class.

38 / 78

Examples:

• Streams

Page 39: Style & Design Principles 02 - Design Patterns

Decorator Pattern

Allows behavior to be added to an individual object, either statically or dynamically, without affecting the behavior of other objects from the same class.

39 / 78

Page 40: Style & Design Principles 02 - Design Patterns

Assignment #2

1. Event Manager

Implement an event manager based on the Observer pattern!

1. Provide a method for adding a new listener.

2. Provide a method for removing a listener.

3. Provide a method for queuing a new event.

4. Provide a method for passing all events to the listeners.

40 / 78

Page 41: Style & Design Principles 02 - Design Patterns

Assignment #2

2. Object Pool

Implement an object pool based on the Object Pool pattern!

1. Define an IPoolable interface for resetting pooled objects.

2. Create an ObjectPool class with Alloc and Free methods for allocating and returning pooled objects.

3. Decide what to do if no object can be allocated!

41 / 78

Page 42: Style & Design Principles 02 - Design Patterns

References

• Wikipedia. Software design pattern. http://en.wikipedia.org/wiki/Software_design_pattern, October 29, 2013.

• Holub, Allen. Why getter and setter methods are evil.http://www.javaworld.com/article/2073723/core-java/why-getter-and-setter-methods-are-evil.html, September 5, 2003.

42 / 78

Page 43: Style & Design Principles 02 - Design Patterns

Thank you for your attention!

Contact

Mail

[email protected]

Blog

http://www.npruehs.de

Twitter

@npruehs

Github

https://github.com/npruehs

43 / 78