Style & Design Principles 02 - Design Patterns
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Style & Design Principles 02 - Design Patterns

on

  • 402 views

Chapter 02 of the lecture Style & Design Principles taught at SAE Institute Hamburg. ...

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.

Statistics

Views

Total Views
402
Views on SlideShare
402
Embed Views
0

Actions

Likes
0
Downloads
3
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Style & Design Principles 02 - Design Patterns Presentation Transcript

  • 1. Style & Design Principles Chapter 02: Design Patterns Nick Prühs November 6, 2013
  • 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. Assignment Solution #1 DEMO 3 / 58
  • 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. 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 ObjectOriented Software was published in 1994 by the socalled "Gang of Four" (Gamma et al.) 5 / 78
  • 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. Design Pattern Types • Creational (object creation) • Structural (relationships between objects) • Behavioral (communication between objects) 7 / 78
  • 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. 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. 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. 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. 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. 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. Behavioral Design Patterns Communication Between Objects: • • • • • Iterator Observer Command Memento Strategy 14 / 78
  • 15. Iterator Pattern Provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation. Examples: • Contains • Where • Count 15 / 78
  • 16. Iterator Pattern Provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation. 16 / 78
  • 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. Examples: • Event Handling 17 / 78
  • 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. Command Pattern Encapsulates all the information needed to call a method at a later time in an object. Examples: • Networking • Replays • Undo 19 / 78
  • 20. Command Pattern Encapsulates all the information needed to call a method at a later time in an object. 20 / 78
  • 21. Command Pattern Encapsulates all the information needed to call a method at a later time in an object. 21 / 78
  • 22. Memento Pattern Provides the ability to restore an object to its previous state. Examples: • Undo 22 / 78
  • 23. Memento Pattern Provides the ability to restore an object to its previous state. 23 / 78
  • 24. Strategy Pattern Defines a family of algorithms, encapsulates each one, and makes them interchangeable. Examples: • Calculator • Sorting • AI 24 / 78
  • 25. Strategy Pattern Defines a family of algorithms, encapsulates each one, and makes them interchangeable. 25 / 78
  • 26. Creational Design Patterns Object Creation: • • • • Prototype Factory Object Pool Singleton 26 / 78
  • 27. Prototype Pattern Objects are created using a prototypical instance, which is cloned to produce new objects. 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]); } } } 27 / 78
  • 28. Factory Method Pattern Defines an interface for creating an object, but let the classes that implement the interface decide which class to instantiate. Examples: • Frameworks 28 / 78
  • 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. 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. Object Pool Pattern Uses a set of initialized objects kept ready to use, rather than allocating and destroying them on demand. Examples: • Unity3D Game Objects • Database Connections • Threads 31 / 78
  • 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. 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. Singleton (Anti-)Pattern Restricts the instantiation of a class to one object. C# public class Singleton { private static Singleton instance; private Singleton() { } public static Singleton Instance { get { return instance ?? (instance = new Singleton()); } } } 34 / 78
  • 35. Structural Design Patterns Relationships Between Objects: • Composite • Decorator 35 / 78
  • 36. Composite Pattern Treats a group of objects in the same way as a single instance of an object. Examples: • Files and Directories 36 / 78
  • 37. Composite Pattern Treats a group of objects in the same way as a single instance of an object. 37 / 78
  • 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. Examples: • Streams 38 / 78
  • 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. Assignment #2 1. Event Manager Implement an event manager based on the Observer pattern! 1. 2. 3. 4. Provide a method for adding a new listener. Provide a method for removing a listener. Provide a method for queuing a new event. Provide a method for passing all events to the listeners. 40 / 78
  • 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. 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/javaworld/jw-092003/jw-0905-toolbox.html?page=1, September 5, 2003. 42 / 78
  • 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