Design Patterns Flashcards

1
Q

What is the Observer pattern?

A

any messaging queue and data service implementations borrow the observer pattern concept. The observer pattern defines one-to-many dependencies between objects so that when one object changes state, all of its dependents are notified and updated automatically.

Observer pattern: Subscribe to the topics/subjects where the client can be notified if there is an update.

This pattern defines Subject and Observer objects.

When a subject changes state, all registered observers are notified and updated automatically (and probably asynchronously).

The sole responsibility of a subject is to maintain a list of observers and to notify them of state changes by calling their update() operation.

The responsibility of observers is to register (and unregister) themselves on a subject (to get notified of state changes) and to update their state (synchronize their state with subject’s state) when they are notified.

This notification-registration interaction is also known as publish-subscribe.

This makes subject and observers loosely coupled. Subject and observers have no explicit knowledge of each other. Observers can be added and removed independently at run-time.

The Observer pattern is a behaviour design pattern that implements a subscription mechanism. This pattern employs an Object called the “subject” to maintain a list of what are called dependant objects. These are known as observers. When the subject’s state is changed, a method is called on each of the observers to notify them of this change.

This pattern aims to solve the following problems:

  • A one-to-many dependency between objects should be defined without making the objects tightly coupled.
  • It should be ensured that when one object changes state an open-ended number of dependent objects are updated automatically.
  • It should be possible that one object can notify an open-ended number of other objects.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What problems can the observer pattern cause?

A

The observer pattern can cause memory leaks, known as the lapsed listener problem, because in basic implementation it requires both explicit registration and explicit deregistration, as in the dispose pattern, because the subject holds strong references to the observers, keeping them alive. This can be prevented by the subject holding weak references to the observers.

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

What problems does the observer pattern solve?

A

Real apps might have dozens of different subscriber classes that are interested in tracking events of the same publisher class. You wouldn’t want to couple the publisher to all of those classes. Besides, you might not even know about some of them beforehand if your publisher class is supposed to be used by other people.

That’s why it’s crucial that all subscribers implement the same interface and that the publisher communicates with them only via that interface. This interface should declare the notification method along with a set of parameters that the publisher can use to pass some contextual data along with the notification.

If your app has several different types of publishers and you want to make your subscribers compatible with all of them, you can go even further and make all publishers follow the same interface. This interface would only need to describe a few subscription methods. The interface would allow subscribers to observe publishers’ states without coupling to their concrete classes.

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

When to use the observer pattern?

A
  • Use the Observer pattern when changes to the state of one object may require changing other objects, and the actual set of objects is unknown beforehand or changes dynamically.
    • You can often experience this problem when working with classes of the graphical user interface. For example, you created custom button classes, and you want to let the clients hook some custom code to your buttons so that it fires whenever a user presses a button.
    • The Observer pattern lets any object that implements the subscriber interface subscribe for event notifications in publisher objects. You can add the subscription mechanism to your buttons, letting the clients hook up their custom code via custom subscriber classes.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the facade pattern?

A

Type: structural design pattern

Facade design pattern provides a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.

The facade pattern is appropriate when we have a complex system that we want to expose to clients in a simplified way. Its purpose is to hide internal complexity behind a single interface that appears simple from the outside.

Facade also decouples the code that uses the system from the details of the subsystems, making it easier to modify the system later.

A Facade Pattern says that just “just provide a unified and simplified interface to a set of interfaces in a subsystem, therefore it hides the complexities of the subsystem from the client”.

In other words, Facade Pattern describes a higher-level interface that makes the sub-system easier to use.

Facade is a structural design pattern that provides a simplified interface to a library, a framework, or any other complex set of classes.

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

When to use facade?

A
  • Use the Facade pattern when you need to have a limited but straightforward interface to a complex subsystem.
    • Often, subsystems get more complex over time. Even applying design patterns typically leads to creating more classes. A subsystem may become more flexible and easier to reuse in various contexts, but the amount of configuration and boilerplate code it demands from a client grows ever larger. The Facade attempts to fix this problem by providing a shortcut to the most-used features of the subsystem which fit most client requirements.
  • Use the Facade when you want to structure a subsystem into layers.
    • Create facades to define entry points to each level of a subsystem. You can reduce coupling between multiple subsystems by requiring them to communicate only through facades.
    • For example, let’s return to our video conversion framework. It can be broken down into two layers: video- and audio-related. For each layer, you can create a facade and then make the classes of each layer communicate with each another via those facades. This approach looks very similar to the Mediator pattern.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How to implement facade?

A
  1. Check whether it’s possible to provide a simpler interface than what an existing subsystem already provides. You’re on the right track if this interface makes the client code independent from many of the subsystem’s classes.
  2. Declare and implement this interface in a new facade class. The facade should redirect the calls from the client code to appropriate objects of the subsystem. The facade should be responsible for initializing the subsystem and managing its further life cycle unless the client code already does this.
  3. To get the full benefit from the pattern, make all the client code communicate with the subsystem only via the facade. Now the client code is protected from any changes in the subsystem code. For example, when a subsystem gets upgraded to a new version, you will only need to modify the code in the facade.
  4. If the facade becomes too big, consider extracting part of its behavior to a new, refined facade class.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly