Docsity

Reid Holmes' Course: Understanding Design Patterns and More, Exams of Law

Partial preview of the text

Download Reid Holmes' Course: Understanding Design Patterns and More and more Exams Law in PDF only on Docsity!

Material and some slide content from:

  • Head First Design Patterns Book
  • GoF Design Patterns Book

Design Patterns

Reid Holmes

GoF design patterns

Design Patterns

                         !       "  # $      !! $   %! !      &        

Observer example

‣ (^) Weather data example (similar to Eclipse example) ‣ (^) WeatherData ‣ (^) temp, humidity, pressure ‣ (^) calls newData() whenever something changes ‣ (^) bad: update views directly from here ‣ (^) WeatherViews ‣ (^) Current View ‣ (^) Forecast View ‣ (^) Stats View

Singleton

‣ (^) Intent: “Ensure a class has only one instance” ‣ (^) Motivation: For situations when having multiple copies of an object is either unnecessary or incorrect. ‣ (^) Applicability: ‣ (^) Situations when there must be only one copy of a class.

Singleton

‣ (^) Collaborations ‣ (^) All collaboration via instance operation. ‣ (^) Consequences: ‣ (^) Controlled access to instance. ‣ (^) Reduced name space. ‣ (^) Permits variable number of instances. ‣ (^) More flexible than class operation

Singleton

‣ (^) Implementation:

  1. Ensure a unique instance.
  2. Provide an easy access point. ‣ (^) Related to: ‣ (^) Can be used to create Abstract Factory, Builder, and Prototype.

Adapter

Adapter

‣ (^) Participants: ‣ (^) Client: Calls the adapter; is completely isolated from the adaptee. ‣ (^) Adapter: Forwards calls between client and adapter. The adapter may have to interact with multiple classes. ‣ (^) Adaptee: Component being adapted. ‣ (^) Alternatives: ‣ (^) Both class and object adapters exist. Class adapters use multiple inheritance to be an instance of both objects simultaneously while object adaptors rely on composition.

Composite

‣ (^) Intent: “Enable a group of objects to be treated as single object” ‣ (^) Motivation: Differentiating between interior and leaf nodes in tree-structured data data increases system complexity. ‣ (^) Applicability: ‣ (^) If you notice you are treating groups and individual of objects the same way ‣ (^) Can also be used when primitives and objects need to be treated identically

Composite

‣ (^) Participants: ‣ (^) Component: base class ‣ (^) Leaf: individual leaf node ‣ (^) Composite: node that maintains a list of children nodes

Facade

‣ (^) Intent: “Provide a unified, higher-level, interface to a whole module making it easier to use.” ‣ (^) Motivation: Composing classes into subsystems reduces complexity. Using a Facade minimizes the communication dependencies between subsystems. ‣ (^) Applicability: ‣ (^) When you want a simple interface to a complex subsystem. ‣ (^) There are many dependencies between clients and a subsystem. ‣ (^) You want to layer your subsystems.

Facade

‣ (^) Participants: ‣ (^) Facade ‣ (^) Subsystem classes ‣ (^) Collaborations: ‣ (^) Clients interact subsystem via Facade. ‣ (^) Consequences: ‣ (^) Shields clients from subsystem components. ‣ (^) Promotes weak coupling. (strong within subsystem, weak between them) ‣ (^) Doesn’t prevent access to subsystem classes.

Template Method

‣ (^) Intent: “Define the skeleton of an algorithm deferring some steps to client subclasses” ‣ (^) Motivation: When two algorithms are largely the same but differ in only a few small details the algorithm can be encapsulated in a base class and defer specific functionality to child classes. ‣ (^) Applicability: ‣ (^) Template method implements invariant parts of the algorithm. ‣ (^) Base class contains the majority of functionality, reducing code duplication.

Template Method

‣ (^) Participants: ‣ (^) Base class, defining common functionality in concrete method making calls to abstract ‘hook’ methods. ‣ (^) Sub classes, overriding subclass-specific parts of the algorithm.