DEV Community

vikash tiwary
vikash tiwary

Posted on

Design Patterns Cheat Sheet.

Creational Patterns ✍️:

  1. Abstract Factory: Creates related objects without specifying the concrete classes. For example -> A car manufacturing plant that produces multiple models and brands of cars without specifying the concrete classes of each part.

  2. Builder: Separates the construction of complex objects from their representation. For example ->A pizza restaurant that lets customers build their own pizza by selecting toppings, crust, and sauce separately.

  3. Factory Method: Defines an interface for creating objects, but lets subclasses decide which class to instantiate. For example -> A car dealership that sells different types of cars and lets customers choose which brand and model they prefer.

  4. Prototype: Creates new objects by cloning an existing object. For example -> A graphic design software that lets users clone an existing object and modify it to create a new design.

  5. Singleton: Ensures only one instance of a class is created and provides a global point of access to it. For example -> A logging system that ensures only one instance is created and used throughout the application to manage log messages.

Structural Patterns ✍️:

  1. Bridge: Separates an object’s interface from its implementation. For example -> A camera that separates its user interface from its hardware components, allowing users to adjust settings and capture images separately.

  2. Composite: Composes objects into tree structures to represent part-whole hierarchies. For example -> A file manager that displays files and folders in a tree structure, allowing users to navigate and manage them hierarchically.

  3. Decorator: Dynamically adds responsibilities to an object. For example -> A coffee shop that offers a variety of flavors and toppings to add to a basic coffee drink.

  4. Facade: Provides a unified interface to a set of interfaces in a subsystem. For example -> An online shopping site that provides a simplified interface to a complex e-commerce system.

  5. Flyweight: Shares objects to reduce memory usage. For example -> A game that shares textures and sprites to reduce memory usage and improve performance.

  6. Proxy: Provides a placeholder for another object to control access to it. For example -> A user authentication system that provides a proxy interface to validate user credentials and control access to restricted resources.

  7. Adapter: Converts the interface of a class into another interface that clients expect. For example -> A power adapter that converts the interface of a foreign electrical device to match the interface of the socket.

Behavioral Patterns ✍️:

  1. Command: Encapsulates a request as an object to allow its invocation at a later time. For example -> A TV remote control that sends commands to a TV set, allowing users to turn it on/off, change channels, and adjust the volume.

  2. Interpreter: Defines a representation for a grammar of a language and provides an interpreter for it. For example -> A language translation service that provides an interpreter to translate text from one language to another.

  3. Iterator: Provides a way to access the elements of an aggregate object sequentially without exposing its underlying representation. For example -> A music player that allows users to navigate through a playlist sequentially without exposing the underlying structure of the playlist.

  4. Mediator: Defines an object that encapsulates how a set of objects interact. For example -> A chat application that allows users to send messages to each other through a centralized server.

  5. Memento: Captures an object’s internal state to restore it later. For example -> A game that allows players to save and restore their game progress at any point in time.

  6. Observer: Defines a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. For example -> A weather app that notifies users of weather changes in real-time based on their location.

  7. State: Allows an object to change its behavior when its internal state changes. For example -> A traffic light system that changes its behavior depending on the current state of traffic flow.

  8. Strategy: Defines a family of algorithms, encapsulates each one, and makes them interchangeable. For example -> A payment system that offers different payment methods, such as credit card, PayPal, and bank transfer, and allows users to choose which one to use.

  9. Template Method: Defines the skeleton of an algorithm in a method, allowing subclasses to redefine certain steps of the algorithm without changing its structure. For example -> A recipe app that provides a template for cooking a dish, allowing users to customize certain steps of the recipe.

  10. Visitor: Separates an algorithm from an object structure by moving the algorithm into a separate object. For example -> A code editor that analyzes the syntax of a program and provides a visitor to detect and fix coding errors.

  11. Chain of Responsibility: Allows multiple objects to handle a request. For example -> A customer service hotline that forwards customer complaints to multiple levels of support until they are resolved.

Top comments (0)