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