Style & Design Principles
Chapter 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?
• Wh...
Assignment Solution #1
DEMO
3 / 58
Objectives
• To learn advances concepts of object-oriented
design
• To understand the motivation behind design
patterns
• ...
Design Patterns
• General reusable solution to a commonly occurring
problem within a given context
• Formalized best pract...
Advantages of
Design Patterns
• Speed up the development process by providing
tested, proven development paradigms
• Impro...
Design Pattern Types
• Creational (object creation)
• Structural (relationships between objects)
• Behavioral (communicati...
Object-Oriented Design 101
• Aggregation
• Combine simple objects or data types into more
complex ones
• Usually expressed...
Object-Oriented Design 101
• Delegation
• Handing a task over to another part of the program
• Polymorphism
• Ad hoc polym...
Object-Oriented Design 101
• Cohesion
• Degree to which the elements of a module belong
together
• How much functionalitie...
Object-Oriented Design 101
• Cohesion
• Degree to which the elements of a module belong
together
• How much functionalitie...
Why getters and setters
are evil
“Don’t ask for the information you need to do the
work; ask the object that has the infor...
Why getters and setters
are evil
• Getter and setter methods are dangerous for the
same reason that public fields are dang...
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...
Iterator Pattern
Provides a way to access the elements of an
aggregate object sequentially without exposing
its underlying...
Observer Pattern
Subject maintains a list of its dependents,
called observers, and notifies them
automatically of any stat...
Observer Pattern
Subject maintains a list of its dependents,
called observers, and notifies them
automatically of any stat...
Command Pattern
Encapsulates all the information needed to
call a method at a later time in an object.
19 / 78
Examples:
•...
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:
...
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#
pu...
Factory Method Pattern
Defines an interface for creating an object, but
let the classes that implement the interface
decid...
Factory Method Pattern
Defines an interface for creating an object, but
let the classes that implement the interface
decid...
Factory Method Pattern
Defines an interface for creating an object, but
let the classes that implement the interface
decid...
Object Pool Pattern
Uses a set of initialized objects kept ready to
use, rather than allocating and destroying
them on dem...
Object Pool Pattern
Uses a set of initialized objects kept ready to
use, rather than allocating and destroying
them on dem...
Singleton (Anti-)Pattern
Restricts the instantiation of a class to one object.
Disadvantages:
• Introduces unnecessary res...
Singleton (Anti-)Pattern
Restricts the instantiation of a class to one object.
34 / 78
C#
public class Singleton
{
private...
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 a...
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...
Decorator Pattern
Allows behavior to be added to an individual
object, either statically or dynamically,
without affecting...
Assignment #2
1. Event Manager
Implement an event manager based on the Observer
pattern!
1. Provide a method for adding a ...
Assignment #2
2. Object Pool
Implement an object pool based on the Object Pool
pattern!
1. Define an IPoolable interface f...
References
• Wikipedia. Software design pattern.
http://en.wikipedia.org/wiki/Software_design_patt
ern, October 29, 2013.
...
Thank you for your attention!
Contact
Mail
dev@npruehs.de
Blog
http://www.npruehs.de
Twitter
@npruehs
Github
https://githu...
Upcoming SlideShare
Loading in...5
×

Style & Design Principles 02 - Design Patterns

416

Published on

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.

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
416
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
14
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Style & Design Principles 02 - Design Patterns

  1. 1. Style & Design Principles Chapter 02: Design Patterns Nick Prühs
  2. 2. 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
  3. 3. Assignment Solution #1 DEMO 3 / 58
  4. 4. 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
  5. 5. 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
  6. 6. Advantages of Design 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
  7. 7. Design Pattern Types • Creational (object creation) • Structural (relationships between objects) • Behavioral (communication between objects) 7 / 78
  8. 8. 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
  9. 9. 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
  10. 10. 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
  11. 11. 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
  12. 12. Why getters and setters are 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
  13. 13. Why getters and setters are 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
  14. 14. Behavioral Design Patterns Communication Between Objects: • Iterator • Observer • Command • Memento • Strategy 14 / 78
  15. 15. 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
  16. 16. Iterator Pattern Provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation. 16 / 78
  17. 17. 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
  18. 18. 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
  19. 19. Command Pattern Encapsulates all the information needed to call a method at a later time in an object. 19 / 78 Examples: • Networking • Replays • Undo
  20. 20. Command Pattern Encapsulates all the information needed to call a method at a later time in an object. 20 / 78
  21. 21. Command Pattern Encapsulates all the information needed to call a method at a later time in an object. 21 / 78
  22. 22. Memento Pattern Provides the ability to restore an object to its previous state. 22 / 78 Examples: • Undo
  23. 23. Memento Pattern Provides the ability to restore an object to its previous state. 23 / 78
  24. 24. Strategy Pattern Defines a family of algorithms, encapsulates each one, and makes them interchangeable. 24 / 78 Examples: • Calculator • Sorting • AI
  25. 25. Strategy Pattern Defines a family of algorithms, encapsulates each one, and makes them interchangeable. 25 / 78
  26. 26. Creational Design Patterns Object Creation: • Prototype • Factory • Object Pool • Singleton 26 / 78
  27. 27. 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]); } } }
  28. 28. 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
  29. 29. 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
  30. 30. 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
  31. 31. 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
  32. 32. Object Pool Pattern Uses a set of initialized objects kept ready to use, rather than allocating and destroying them on demand. 32 / 78
  33. 33. 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
  34. 34. 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()); } } }
  35. 35. Structural Design Patterns Relationships Between Objects: • Composite • Decorator 35 / 78
  36. 36. Composite Pattern Treats a group of objects in the same way as a single instance of an object. 36 / 78 Examples: • Files and Directories
  37. 37. Composite Pattern Treats a group of objects in the same way as a single instance of an object. 37 / 78
  38. 38. 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
  39. 39. 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
  40. 40. 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
  41. 41. 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
  42. 42. References • Wikipedia. Software design pattern. http://en.wikipedia.org/wiki/Software_design_patt ern, 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
  43. 43. Thank you for your attention! Contact Mail dev@npruehs.de Blog http://www.npruehs.de Twitter @npruehs Github https://github.com/npruehs 43 / 78
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×