1. Singleton Pattern: This one’s all about exclusivity. It ensures that a class has just one instance, like having a VIP pass to a club. You can access that instance from anywhere, making it handy for situations where you want a single point of control or coordination in your application.
  • Class is defined in such a way that only one instance of the class is created in the complete execution of a program or project.
  • It is used where only a single instance of a class is required to control the
  • action throughout the execution.
  • Singleton classes are used for logging, driver objects, caching and thread pool, database connections.

structural patterns

  • Structural  Design  Patterns  are  Design  Patterns  that  ease  the  design  by identifying a simple way to realize relationships between entities.
  • flexible interconnecting modules which can work together in a larger system.
  • describes how data moves through the pattern.
  1. Adapter:Adapting an interface into another according to client expectation.
  • allows objects with incompatible interfaces to work together.

Problem: Imagine that you’re creating a stock market monitoring app. The app downloads the stock data from multiple sources in XML format and then displays nice-looking charts and diagrams for the user.

At some point, you decide to improve the app by integrating a smart 3rd- party analytics library. But there’s a catch: the analytics library only works with data in JSON format.

Solution: You can create an adapter. This is a special object that converts the interface of

  • one object so that another object can understand it.
  • An adapter wraps one of the objects to hide the complexity of conversion happening behind the scenes.
  • The wrapped object isn’t even aware of the adapter. Adapter help objects with different interfaces collaborate
  1. Bridge:Separating abstraction (interface) from implementation.
  • Separates an object’s interface from its implementation.
  • bridge pattern uses encapsulation, aggregation,and can use inheritance to separate responsibilities into different classes.
  • avoid a permanent binding between an abstraction and its implementation.
  • Bridge Patterns enables us to separate the interface from the implementation and Improves extensibility. Also, it hides implementation details from clients.

Abstraction (also called interface) is a high-level control layer for some entity. ==This layer isn’t supposed to do any real work on its own. It should delegate the work to the implementation layer (also called platform).==

In a worst-case scenario, this app might look like a giant spaghetti bowl, where hundreds of conditionals connect different types of GUI with various APIs all over the code.

You can bring order to this chaos by extracting the code related to specific interface-platform combinations into separate classes. However, soon you’ll discover that there are lots of these classes. The class hierarchy will grow exponentially because adding a new GUI or supporting a different API would require creating more and more classes.

Let’s try to solve this issue with the Bridge pattern. It suggests that we divide the classes into two hierarchies:

  • Abstraction: the GUI layer of the app.
  • Implementation: the operating systems’ APIs.

The abstraction object controls the appearance of the app, delegating the actual work to the linked implementation object. based on the operating systems

  1. Composite:Allowing clients to operate on hierarchy of objects - also know as object tree Composite is a structural design pattern that lets you compose objects into tree structures and then work with these structures as if they were individual objects.

Problems :

  •  Composite pattern makes sense only when the core model of your app can be represented as a tree.

 the direct approach: unwrap all the boxes, go over all the products and then calculate the total. That would be doable in the real world; but in a program, it’s not as simple as running a loop. You have to know the classes of Products and Boxes you’re going through, the nesting level of the boxes and other nasty details beforehand. All of this makes the direct approach either too awkward or even impossible.

  • pattern suggests that you work with Products and Boxes through a common interface which declares a method for calculating the total price.
  1. Decorator:Adding functionality to an object dynamically.
  • attach new behaviors to objects by placing these objects inside special wrapper objects that contain the behaviors.
  • The initial version of the library was based on the Notifier class that had only a few fields, a constructor and a single send method. The method could accept a message argument from a client and send the message to a list of emails that were passed to the notifier via its constructor. A third-party app which acted as a client was supposed to create and configure the notifier object once, and then use it each time something important happened
  • Structure of the library after implementing other notification types
  • Each notification type is implemented as a notifier’s subclass.
  • combined several notification methods within one class. explosion of subclasses
  1. Façade:Providing an interface to a set of interfaces.
  • provides a simplified interface to a library, a framework, or any other complex set of classes.
  • A facade is a class that provides a simple interface to a complex subsystem which contains lots of moving parts.

Instead of making your code work with dozens of the framework classes directly, you create a facade class which encapsulates that functionality and hides it from the rest of the code. This structure also helps you to minimize the effort of upgrading to future versions of the framework or replacing it with another one. The only thing you’d need to change in your app would be the implementation of the facade’s methods.

  1. Flyweight:Reusing an object by sharing it.

  2. Proxy: Representing another object.

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

Refs :