Design patterns you should know
Tags: discuss, refactoring, design, code
Published: 2021-01-10 18:56:14

# Factory pattern
This pattern is used to create an object without specifying or knowing the exact class to instantiate, by defining abstract class or interfaces.

To implement this pattern, define an abstract class (or interface) which represents the base mode of the object, and the concrete models.
Then create a factory class which defines the factory method, which returns an object of the base type model.


## Builder pattern
This pattern is used to create an object which requires multiple steps and complex processes to do.

To implement this pattern, create an abstract class Builder with its subclass (concrete builder) which encapsulates the complex construction logic, and should have at least a method called build() which returns the created object.

## Prototype pattern
This pattern is used to create an object based on a prototypical instance (that could be cached on previously created, or copied).

It allows us to avoid the cost of creating a new object (with the keyword new) when it is very expensive.

To implement this pattern, create an abstract class name Prototype (or an interface) which has a method called clone, any client object (Business bean) that is very expensive to create should extends (or implements) this base class (or interface).

## Adapter pattern
This pattern is used to allow a class A or an interface to be used as another interface B, without modifying the source code of A .

Example: the client is using an interface A which has a certain method, but you have developed a new object which does not have the same method that the client expects.

To implement this pattern, create an Adapter class which extends your class (which does not behave like the predefined interface), and implement this interface.
Inside the new adapter class override the methods to make it similar to what the interface does.

## Bridge pattern
This pattern is used to split a large class or a set of closely related classes into two separate abstractions and implementations, which can be developed independently of each other.

To implement this pattern, put the abstraction on a single class, and the implementation in a single interface, then create your concrete model, instead of creating a single class that has all the logic.

## Facade pattern
This pattern is used to define a front-facing interface of one or multiple classes masking complex logic and structure (think of API) or to facilitate the usage of multiple classes for the user.

To implement this pattern, create a facade class that masks the complex structure.

## Proxy pattern

This pattern is used to provide a placeholder or substitute of an object, it allows to control access to the original object or perform some pre-post operation before the actual operation.

To implement this pattern, create a class that wraps the original object, and if needed override the same methods where we control the access to the original.

## Observer pattern

This pattern is used to define an object called subject (publisher or listener) which has a list of dependencies called observers (subscriber), that are notified by the subject when a change has occurred.
It is mostly used to implement event handling mechanisms in distributed systems.