Object Oriented Design Flashcards
What are design patterns?
In software engineering, a design pattern is a general repeatable solution to a commonly occurring problem in software design.
What types of design patterns exist?
Creational, Structural, Behavioural
Factory Pattern (Creational)
Many design patterns are similar from a code point of view. What really changes is their intention which is depicted by the name of the pattern. The factory pattern allows to create a new object at runtime based on some input which can be a configuration file, some data introduced by a user, etc. In order to keep our code highly cohesive and loosely coupled, we can put the branches leading to the creation of different objects in a director class. These objects will contain, if it’s needed, some predefined values that will passed to our final objects’ constructors.
Builder Pattern (Creational)
It’s used to provide specifications in order to create a new object and avoid using telescoping constructors. One of the ways to do this is using for example using a static method named builder which returns an instance of a static inner which using the concept of fluid setters allows us to build a new object.
Facade (Structural)
A facade is an object that serves as a front-facing interface masking more complex underlying or structural code. A facade can.
- improve the readability and usability of a software library by masking interaction with more complex components behind a single (and often simplified) API
- provide a context-specific interface to more generic functionality (complete with context-specific input validation)
- serve as a launching point for a broader refactor of monolithic or tightly-coupled systems in favor of more loosely-coupled code
Proxy (Structural)
A proxy is a wrapper or agent object that is being called by the client to access the real serving object behind the scenes. Use of the proxy can simply be forwarding to the real object, or can provide additional logic.
Strategy (Behavioral)
Many design patterns are similar from a code point of view. What really changes is their intention which is depicted by the name of the pattern.
The strategy pattern allows to select an algorithm at runtime.
Instead of implementing a single algorithm directly, code receives run-time instructions as to which in a family of algorithms to use
Iterator (Behavioral)
The iterator pattern is a design pattern in which an iterator is used to traverse a container and access the container’s elements.
Iterator interface has the following methods:
- hasNext()
- next()