L8 Design Patterns 1 - Observer and Adapter Flashcards

1
Q

What are design patterns?

A
  • 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 ).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the key 4 elements of design patterns?

A
  1. The pattern name is a handle we can use to describe a design problem and its solution in a word or two
  2. The problem describes the context and when to apply the pattern
  3. The solution describes the elements that make up the design, and pattern of their relationships, responsibilities and collaborations.
  4. The consequences are the result and trade-offs of applying the pattern.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the 3 category of design patterns?

A
  1. Creational
  2. Structural
  3. Behavioural
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the application of the Creational Design Patterns, and give me the examples

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the application of the Structural Design Patterns, and give me the examples

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the application of the Behavioural Design Patterns, and give me the examples

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the Behavioural pattern: Observer

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the Structural Pattern: Adapter

A

The Adapter structural design pattern converts the interface of a class into an interface that a client expects

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Structural pattern: Class Adapter

A

Adapter adapts Adaptee by inheriting from it and implementing the Target interface

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the Structural pattern: Object Adapter

A
  • Object adapter
    – An object adapter relies on object composition
    – Adapter implements the Target interface and contains the Adaptee instance to which it forwards requests
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Observer pattern with subject as abstract class:

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Observer pattern with subject as interface:

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Observer Pattern
Code example

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Observer pattern: Home security system

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Code example - application

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

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();
A

Adapting actuator to sensor – Class Adapter

17
Q

Adapting actuator to sensor – Object Adapter

A
18
Q

Applying design patterns

A
  • 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