Design patterns Flashcards

1
Q

What are the benefits of using design patterns?

A

Universality, communication, guaranteed solutions, readability, and reduced length of code

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

How are they categorized?

A

They are categorized by their purpose: creational, structural, and behavioral.

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

What do creational patterns do?

A

They provide flexibility in what gets created, who creates it and how, when it gets created.

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

What are the 5 design patterns in creational category?

A

Singleton, Factory method, Builder, Abstract Factory, Prototype

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

What does the builder pattern do, and what are the benefits of using it?

A

The builder pattern lets you create different representations of an object using the same construction. For a large class, and you don’t want to use all the class fields, the builder pattern is the choice.(an alternative is creating sublclasses or multiple constructors)

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

What does the factory method do and what are its benefits?

A

The factory method allows us to create objects without specifying the exact class to create, delegating the creation to another class(factory). This way, the internals of the class are hidden, good for code maintenance.

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

What does the singleton pattern and what are the benefits of using it?

A

The singleton pattnern restricts object creation for a class to only one instance. (you can not have different instances of the object). The constructor will be private.

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

What are the 7 design patterns in the structural category?

A

Composite, Adapter, Decorator, Facade, Proxy, Bridge, Flyweight

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

What do structural patterns do?

A

They are used to piece together multiple classes into bigger picture without affecting the flexibility of the individual classes.

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

What does the adapter pattern do and what are its benefits?

A

The adapter pattern allows classes with incompatible interfaces to communicate with one another by wrapping its own interface around that of an already existing class. With this pattern you can reuse existing code for different use cases. (it will implement a “middleman” that will convert/adapt the interface from one class so other one can comprehend

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

What does the decorator pattern do and what are its benefits?

A

It dynamically adds or overrides behavior in an existing method of an object by wrapping these objects in new objects that contain the desired behavior. It adds new functionality to existing objects without changing its structure.

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

What does the proxy pattern do and what are its benefits?

A

It creates expensive objects on demand (virtual proxy)/ representative of objects in a different address space (remote proxy)/ control access to an object (protection proxy)

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

What does the facade pattern do and what are its benefits?

A

It provides a simplified interface to a codebase, library, framework. It simplifies the integration of a complex codebase into other codebases. The programmer doesn’t need to know details of this complex codebase.

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

What are the 7 design patterns in the behavioral
category?

A

Iterator, observer, template method, strategy, state, command, chain of responsability.

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

What do the behavioral patterns do?

A

They facilitate complex control flow (interaction and distribution of responsability)

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

What does the command pattern do and what are its advantages?

A

This pattern treats commands/ requests like objects. Implementing it allows you to have many different commands(defined as classes) that each implement a Command interface. Therefore, the sender and receiver are decoupled. (The behavior of the handler- not defined in handler anymore). => much cleaner architecture=> more readable and maintainable

17
Q

What does the iterator pattern do and what are its advantages?

A

It adds an abstraction to all collection objects that allows you to iterate them without having any intrinsic knowledge about the underlying representation. It helps simplifying the design of the code, by adhering to one standard (how the iterator operates)

18
Q

How do you implement the iterator pattern?

A

You create an iterator interface that will force you to implement hasNext()- method, nextInt() and reset().

19
Q

What does the observer pattern do and what are its benefits?

A

This patterns allows subscribers to update themselves whenever a publisher sends an update.

20
Q

What are the differences between subscribers in the old observer pattern and the new one?

A

They implement PropertyChangeListener instead of Observer. update() method from the old observer pattern is now replaced by propertyChange(PropertyChangeEvent e). Now the name of the property that was changed is saved, the old value and the new value are saved.

21
Q

What are the differences in the Observables(publishers) of the new observer pattern and the new one?

A

There is no class to extend now, but they have a field PropertyChangeSupport (a class with a list of all the observers and methods needed for adding removing and notifying them).

22
Q

How are you firing an event with the observer pattern?

A

Instead of calling setChanged() and notifyObservers() as in the old observer pattern, now PropertyChangeEvent is used, which is initialized with the properties that were changed. example: PropertyChangeEvent ageEvent = new PropertyChangeEvent(this, “age”, age, newAge). Then to fire it, we do: propertyChangeSupport.firePropoertyChange(ageEvent);

23
Q

What does the state pattern do and what are its benefits?

A

Allows an object to alter its behavior when its internal state changes (similar to a finite-state machine where you have limiter nr of states and you can transition from one state to another). You can do this by creating separate classes for each state that implement the same interface or inherit from the same superclass. This leads to smaller, more readable classes, with less dependencies between them. +more extensible

24
Q

What does the strategy pattern do and what are its benefits?

A

It simplifies the process of selecting an algorithm or strategy on the fly at runtime. You create a separate class for each strategy with a common interface/ abstract class. It allows for easy switching between strategies, minimises the coupling between classes, more maintainable code.

25
Q

What does the template method pattern do and what are its benefits?

A

It defines a template of an algorithm as an abstract class. Therefore, it enforces the subclasses to have a similar structure. It gives a consistent structure among related class and it allows switching between algorithms easily. example: games have the same structure

26
Q

What does the chain of responsibility pattern do and what are its benefits?

A

It’s useful when you need to sends requests that are handled like commands, but more than one handler can process a request. (A handler processes a request depending on the run-time conditions or forward it to successor on the chain(if any)

27
Q

What does the composite pattern do?

A

It composes similar objects to manipulate all at once. (allows treating individual objects and composition of objects in the same way-can be viewed as a tree structure made up of types that inherit a base type)