L10: Structuring relationships in a system Flashcards

1
Q

Decorator: Aim, what it does, what is it used for

A

A decorator aims to customize objects at runtime.

Attach additional functionalities to an object:
- Note: to an object (at runtime, not all objects), not a class (at design time)
- Allows to adhere to separation of concerns / single responsibility principle and the open-closed principle.

Used for:
- Adapting GUI elements on demand
- I/O operations

E.g.: LineNumberReader read = new LineNumberReader(new FileReader(file));

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

What does the decorator essentially do?

A

Promotes composition to extend an object’s functionality

We do so by wrapping new functionality around an existing object.

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

What is the diamond problem?

A

In multiple inheritance, it can be unclear what super method to call.

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

Being careful with the decorator

A
  • Added behavior can interact (unintentionally)
  • Complex hierarchies and nested structures can complicate comprehension
  • Overheads in performance, code, maintenance, …
  • Checking for object identity is complicated
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

instanceof

A

Instanceof tests whether an object is of a specific type (important to use before casting an object into another type).

Switch can also be used instead of Instanceof

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

Facade: what does it provide, what does it do, what is it used for

A

A facade provides an unified interface for a unit.

Hide the implementation of a larger unit behind one interface
- Define a single access point
- Hide the complexity of a larger unit
- Put contraints on the order of method calls
- Decouple the system (Minimise Coupling, Maximise Cohesion)

Used for
- Any API
- SwingWorker

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

Being careful with the facade

A

The facade aims to implement many good design principles.

There are some things to keep in mind:
- Hiding details of the underlying system can also complicate its use
- The facade must also be maintained
- If not used appropriately, it can introduce unnecessary complexity

  • As for any design pattern: Do not overuse, make sure it actually improves your software design! (hammer and nail topic)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly