style & design principles 02 - design patterns

Post on 10-May-2015

369 Views

Category:

Technology

0 Downloads

Preview:

Click to see full reader

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.

TRANSCRIPT

Style & Design PrinciplesChapter 02: Design Patterns

Nick Prühs

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

Assignment Solution #1

DEMO

3 / 58

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

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

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

Design Pattern Types

• Creational (object creation)

• Structural (relationships between objects)

• Behavioral (communication between objects)

7 / 78

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

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

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

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

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

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

Behavioral Design Patterns

Communication Between Objects:

• Iterator

• Observer

• Command

• Memento

• Strategy

14 / 78

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

Iterator Pattern

Provides a way to access the elements of an

aggregate object sequentially without exposing

its underlying representation.

16 / 78

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

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

Command Pattern

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

19 / 78

Examples:

• Networking• Replays• Undo

Command Pattern

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

20 / 78

Command Pattern

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

21 / 78

Memento Pattern

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

22 / 78

Examples:

• Undo

Memento Pattern

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

23 / 78

Strategy Pattern

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

24 / 78

Examples:

• Calculator• Sorting• AI

Strategy Pattern

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

25 / 78

Creational Design Patterns

Object Creation:

• Prototype

• Factory

• Object Pool

• Singleton

26 / 78

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]);}

}}

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

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

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

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

Object Pool Pattern

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

32 / 78

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

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());}

}}

Structural Design Patterns

Relationships Between Objects:

• Composite

• Decorator

35 / 78

Composite Pattern

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

36 / 78

Examples:

• Files and Directories

Composite Pattern

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

37 / 78

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

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

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

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

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

Thank you for your attention!

Contact

Mail

dev@npruehs.de

Blog

http://www.npruehs.de

Twitter

@npruehs

Github

https://github.com/npruehs

43 / 78

top related