Design Patterns

Singleton

  • A creational design pattern which ensures a class has only one instance and a global access point to the instance.
  • Make the default constructor private to prevent other from creating new instance.
  • Create a static creation method that acts as a constructor. This method will call the private constructor to create an object and save it in a static field and return it in all following calls.

Applicability

  • when a class in your program should have just a single instance available to all clients; for example, a single database object shared by different parts of the program.
  • when you need stricter control over global variables. Unlike global variables, the Singleton pattern guarantees that there’s just one instance of a class. Nothing, except for the Singleton class itself, can replace the cached instance.

Alt text

Alt text

References:

Factory

  • another creational design pattern that provides an interface for creating objects in a superclass but allows subclasses to alter the type of objects that will be created.
  • is used when we have a superclass and multiple sub-classes and based on input we need to return one of the sub-class.

Applicability

  • when we don't know beforehand the exact type and dependencies of the objects the code should work with.
  • when we want to provide a way to extend the internal components. For instance, in existing payment methods, we want to allow extending the payment through other process in future.
  • when you want to save system resources by reusing existing objects instead of rebuilding them each time.

Alt text

References:

Abstract Factory

  • A creational design pattern that lets you produce families of related objects without specifying their concrete classes.

Builder

  • design pattern that lets you construct complex objects step by step. It allows to produce different types and representation of an object using same construction code.
  • it can be used when and object has a lot of attributes.

Applicability

  • Use the Builder pattern to get rid of a “telescopic constructor”. When we have ten or more optional parameters, it is inconvenient to create a constructor with all those parameters. Instead, with overload constructor and create several constructors with few parameters. Builder pattern allows to build objets step by step using the parameters that we only need. It removes the necessity of creating multiple constructors with different parameters.
  • Use it when you want to create different representations of some product.

Alt text

References:

Prototype

  • Design pattern that lets you copy existing objects without making the code dependent on their classes.
  • Used when creation of object directly is costly.
  • For instance, an object to be created after a costly database operation. The object can be cached and return its clone on next request.

Alt text

References:

Object Pool

When objects are expensive to create, and they are needed only for short periods of time it is advantageous to utilize the Object Pool pattern. The Object Pool provides a cache for instantiated objects tracking which ones are in use and which are available.

Object pool pattern is a software creational design pattern which is used in situations where the cost of initializing a class instance is very high. Basically, an Object pool is a container which contains some amount of objects. So, when an object is taken from the pool, it is not available in the pool until it is put back.

Alt text

References:

Structural Design pattern explain how to assemble objects and classes into larger structure while keeping these structures flexible and efficient.

Adapter

  • Adapter is a structural design pattern that allows objects with incompatible interfaces to collaborate.

Alt text

Alt text

References:

Facade

  • A structural design pattern whose main objective is to hide the complexity of system and provide an interface to the client using which client can access the system.
  • Example: A customer ordering food from the menu in a restaurant. the menu acts as an interface, which makes customer easier to order food and not care about the complexities behind like who cooks, for how long or who will be washing the dishes.
  • It is a wrapper class used to hide implementation details.

Alt text

References:

Proxy

Proxy is a structural design pattern that lets you provide a substitute or placeholder for another object. A proxy controls the access to the original object, allow performing something either before or after the request gets through to the original object.

Proxies delegate all the real work to some other object. Each proxy method should, in the end, refer to a service object unless the proxy is a subclass of a service.

For heavy Java object like JDBC connection, or a SessionFactory that requires some initial configuration, we only want them to be initialized on demand. And once they are initialized, we want to reuse them for all calls.

Alt text

When to Use

  • When we want a simplified version of a complex or heavy object. In this case, we may represent it with a skeleton object which loads the original object on demand, also called as lazy initialization. This is known as the Virtual Proxy.

  • When the original object is present in different address space, and we want to represent it locally. We can create a proxy which does all the necessary boilerplate stuff like creating and maintaining the connection, encoding, decoding, etc., while the client accesses it as it was present in their local address space. This is called the Remote Proxy.

  • When we want to add a layer of security to the original underlying object to provide controlled access based on access rights of the client. This is called Protection Proxy.

Reference: