L8 Design Patterns 1 - Observer and Adapter Flashcards
What are design patterns?
- Software design patterns are reusable solutions to common problems that occur in software development.
- They capture the experience of experts in a form that others can reuse.
- They represent proven solutions to recurring software design problems and embody best practices.
- Many software design patterns address common situations that developers need to implement in every application they build (e.g., object initialization, incompatible interfaces or APIs, simplifying complex interfaces, providing controlled access to objects, generating objects on the fly etc ).
What are the key 4 elements of design patterns?
- The pattern name is a handle we can use to describe a design problem and its solution in a word or two
- The problem describes the context and when to apply the pattern
- The solution describes the elements that make up the design, and pattern of their relationships, responsibilities and collaborations.
- The consequences are the result and trade-offs of applying the pattern.
What are the 3 category of design patterns?
- Creational
- Structural
- Behavioural
What is the application of the Creational Design Patterns, and give me the examples
It is used on Class Instantiation (or class initialization)
The examples such as:
1. Abstract Factory
2. Builder
3. Factory Method
4. Prototype
5. Singleton
What is the application of the Structural Design Patterns, and give me the examples
It is used in Class and Object composition
The examples such as:
1. Adapter
2. Bridge
3. Composite
4. Decorator
5. Facade
6. Flyweight
7. Proxy
What is the application of the Behavioural Design Patterns, and give me the examples
It is used on communication between objects
The examples such as:
1. Chain of responsibility
2. Command
3. Interpreter
4. Iterator
5. Mediator
6. Memento
7. Observer
8. State
9. Strategy
10. Template method
11. Visitor
What is the Behavioural pattern: Observer
Is a behavioural design pattern that lets you define a subscription mechanism to notify multiple objects about any events that happen to the object that they are observing
- Subject is independent of the observers
- Observers are dynamically assigned to the subject
- Subject informs observers about changes in its state
- A subscription mechanism lets individual objects subscribe to event notifications.
What is the Structural Pattern: Adapter
The Adapter structural design pattern converts the interface of a class into an interface that a client expects
Structural pattern: Class Adapter
Adapter adapts Adaptee by inheriting from it and implementing the Target interface
What is the Structural pattern: Object Adapter
- Object adapter
– An object adapter relies on object composition
– Adapter implements the Target interface and contains the Adaptee instance to which it forwards requests
Observer pattern with subject as abstract class:
Observer pattern with subject as interface:
Observer Pattern
Code example
Observer pattern: Home security system
Code example - application
Adapter pattern
- You have been asked develop an adapter to allow pre-existing sensors to turn ON and OFF the three actuators above. The sensors work with a target interface that has two method signatures: turnActuatorON(:int); turnActuatorOFF(:int);
The :int parameter represents the ID of an actuator.
- Each actuator is specified as a class with two methods turnON(); and turnOFF();
Adapting actuator to sensor – Class Adapter
Adapting actuator to sensor – Object Adapter
Applying design patterns
- Prerequisites for applying design patterns properly:
– Know your design patterns
– Creational patterns
* Abstract Factory groups object factories that have a common theme.
* Builder constructs complex objects by separating construction and representation.
* Factory Method creates objects without specifying the exact class to create.
* Prototype creates objects by cloning an existing object.
* Singleton restricts object creation for a class to only one instance.
– Structural patterns
* Adapter allows classes with incompatible interfaces to work together by wrapping its own interface around that of an already existing class.
* Bridge decouples an abstraction from its implementation so that the two can vary independently.
* Composite composes one-or-more similar objects so that they can be manipulated as one object.
* Decorator dynamically adds/overrides behaviour in an existing method of an object.
* Facade provides a simplified interface to a large body of code.
* Flyweight reduces the cost of creating and manipulating a large number of similar objects.
* Proxy provides a placeholder for another object to control access, reduce cost, and reduce complexity.
- Behavioural patterns
- Chain of responsibility delegates commands to a chain of processing objects.
- Command creates objects which encapsulate actions and parameters.
- Interpreter implements a specialized language.
- Iterator accesses the elements of an object sequentially without exposing its underlying representation.
- Mediator allows loose coupling between classes by being the only class that has detailed knowledge of their methods.
- Memento provides the ability to restore an object to its previous state (undo).
- Observer is a publish/subscribe pattern which allows a number of observer objects to see an event.
- State allows an object to alter its behavior when its internal state changes.
- Strategy allows one of a family of algorithms to be selected on-the-fly at runtime.
- Template method defines the skeleton of an algorithm as an abstract class, allowing its subclasses to provide concrete behaviour.
- Visitor separates an algorithm from an object structure by moving the hierarchy of methods into one object.
- But:
– It’s enough to know the intent – you can look up the details later