Skip to content

Instantly share code, notes, and snippets.

@manashcse11
Last active September 7, 2022 06:59
Show Gist options
  • Save manashcse11/ba77df8276f88336a3b0775730eec3a2 to your computer and use it in GitHub Desktop.
Save manashcse11/ba77df8276f88336a3b0775730eec3a2 to your computer and use it in GitHub Desktop.

Observer Design Pattern:

  1. Give a way to avoid tight coupling between components.
  2. One object make itself observable(publisher), by adding a method that allows another objects observe itself.
  3. By registering to the observer list, registered objects can get notification when observable object change its state.

Factory Design Pattern:

  1. A class that has some methods, that creates object for you.
  2. Allows to create object runtime, without specifying the exact class.
  3. Just call the Factory with parameter(by which Factory decide), then Factory will create specific object for you.

Abstract Factory Design Pattern:

  1. Like Factory pattern, but everything is encapsulated
    • The method that request the object
    • The factories that create the object
    • The final objects (Contain objects that use Strategy pattern)
  2. Allows to create families of related objects without specifying a concrete class
  3. Objects can be added & changed runtime
  4. A generic layer before the specific factory & can connect generic layer to the specific factory
  5. Negative: Complicated

Singleton Design Pattern:

  1. Keep memory usage low by restricting the instantiation of a class to one object
  2. Useful when exactly one object is required to coordinate actions across the systems
  3. Use cases: configuration class, session class, database class etc.
  4. Negative:
    • Singleton is generally a bad idea if you are doing unit testing
    • Violate a couple of SOLID principles and introduce Global State

Builder Design Pattern:

  1. Builder is an interface that build parts of a complex object.
  2. Instead of using numerous constructors, it uses another object(builder), that receives each intialization parameter step by step & then retrun constructed object at once.
  3. Benefit: Can be used for objects that contain flat data(HTML code, SQL query etc).
  4. Negative:
    • Requires separate concrete builder for each different type of item
    • If you have a complex inheritance tree for objects, then your builders will have complex inheritance tree

Prototype Design Pattern:

  1. We create a standard object for each class, then clone that object to create new instances
  2. Allows to add any subclass instance of a known superclass at run time
  3. Benefit:
    • Reduces the necessity of subclass
    • Avoid the cost of creating objects in the standard way(new Foo())

JAVA Reflection API:

  1. The process of analyzing & modifying all the capabilities of a class at runtime.
  2. Used to manipulate class & its members(fields, methods, constructor etc) at run time.
  3. Benefit: It can also manipulate private members of a class.

Decorator Design Pattern:

  1. Allows to modify objects dynamically.
  2. Capabilities of inheritance, but need to add the functionality at run time.
  3. Benefit:
    • More flexible than inheritance
    • Simplify the code, because of adding functionality using many simple classes

Command Design Pattern:

  1. An object is used to represent or encapsulate all the info(method name, values & the object that owns the method) needed to call the method
  2. Receiver: Contains the actual implementation (knows how to carry out requested command) of any commands.
  3. Command: Contains information about the necessary action to be taken. It calls the required method from receiver.
  4. Client: This element really behaves like a client (deciding what to do). Its job is to determine which command to execute, without knowing who will execute it and how it will be executed.
  5. Invoker: Initiates the whole process. It takes arguments from the client and invokes the process to call the required command.

Adapter design Pattern:

  1. Works as a bridge between two incompatible interfaces.
  2. This pattern involves a single class which is responsible to join functionalities of independent or incompatible interfaces.
  3. Make existing classes work with others, without modifying their source code.

Facade Design Pattern:

  1. Provides a simplified interface to a larger/complex body of code
  2. Hides the complexities of the system.
  3. Wrap a poorly designed collection of APIs with a single well-designed API.

Bridge Design Pattern:

  1. Decouple an abstraction from its implementation, so that the two can vary independently.

  2. Bridge emphasizes identifying and decoupling "interface" abstraction from "implementation" abstraction.

  3. This pattern involves an interface which acts as a bridge which makes the functionality of concrete classes independent from interface implementer classes. Both types of classes can be altered structurally without affecting each other.

  4. Prefer composition over inheritance

  5. When:

                ----Shape---
               /            \
      Rectangle              Circle
     /         \            /      \
    

BlueRectangle RedRectangle BlueCircle RedCircle

Refactor to:

      ----Shape---                        Color
     /            \                       /   \

Rectangle(Color) Circle(Color) Blue Red

Template Method Design Pattern:

  1. Template Method lets subclasses redefine some steps of an algorithm, without changing the algorithm structure.
  2. Base class declares algorithm 'placeholders', and derived classes implement the placeholders.
  3. Decide which steps of an algorithm are invariant (or standard), and which are variant (or customizable).The invariant steps are implemented in an abstract base class, while the variant steps are either given a default implementation, or no implementation at all.
  4. Allows the component client to extend or replace some number of steps.
  5. Template Method is used prominently in frameworks.

Iterator Design Pattern:

  1. A design pattern in which an iterator is used to traverse a container and access the container's elements. The iterator pattern decouples algorithms from containers
  2. Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.
  3. Iterator pattern is very commonly used design pattern in Java and .Net programming environment.

Composite Design Pattern:

  1. Describe a group of objects, & that is treated as the same way as single instance of the same type of object.
  2. Compose objects into tree structures to represent whole-part hierarchies.
  3. Recursive composition
  4. Directories contain entries, each of which could be a directory.
  5. At the heart of this pattern is the ability for a client to perform operations on an object without needing to know that there are many objects inside.

Flyweight Design Pattern:

  1. Used to reduce the number of objects created and to decrease memory footprint and increase performance.
  2. Use sharing to support large numbers of fine-grained objects efficiently.
  3. Flyweight pattern tries to reuse already existing similar kind objects by storing them and creates new object when no matching object is found.
  4. Divide the target class's state into: shareable (intrinsic) state, and non-shareable (extrinsic) state.

State Design Pattern:

  1. A class behavior changes based on its state.
  2. In State pattern, we create objects which represent various states and a context object whose behavior varies as its state object changes.
  3. The State pattern is a solution to the problem of how to make behavior depend on state.

Proxy Design Pattern:

  1. Provide a surrogate or placeholder for another object to control access to it.
  2. Use an extra level of indirection to support distributed, controlled, or intelligent access.
  3. Design a surrogate, or proxy, object that: instantiates the real object the first time the client makes a request of the proxy, remembers the identity of this real object, and forwards the instigating request to this real object. Then all subsequent requests are simply forwarded directly to the encapsulated real object.

Chain of Responsibility Design Pattern:

  1. Send data to an object, if that object can't process the request, transfer to any number of other objects
  2. Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it.
  3. Launch-and-leave requests with a single processing pipeline that contains many possible handlers.
  4. An object-oriented linked list with recursive traversal.

Interpreter Design Pattern:

  1. Given a language, define a representation for its grammar along with an interpreter, that uses the representation to interpret sentences in the language.
  2. Map a domain to a language, the language to a grammar, and the grammar to a hierarchical object-oriented design.
  3. Uses:
    • Specialized database query languages such as SQL.
    • Specialized computer languages that are often used to describe communication protocols.
    • Most general-purpose computer languages actually incorporate several specialized languages.

Mediator Design Pattern:

  1. Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently.
  2. Design an intermediary to decouple many peers.
  3. Promote the many-to-many relationships between interacting peers to "full object status".
  4. Acts as the hub of communication & reduce communication complexity.

Memento Design Pattern:

  1. A way to store object states & restore to its previous state (undo via rollback).
  2. The memento pattern is implemented with three objects: the originator, a caretaker and a memento.
    • Originator: the object that knows how to save itself.
    • Caretaker: the object that knows why and when the Originator needs to save and restore itself.
    • Memento: the lock box that is written and read by the Originator, and shepherded by the Caretaker.
  3. A magic cookie that encapsulates a "check point" capability.

Visitor Design Pattern:

  1. Allows to add methods to classes without much altering.
  2. Visitor's primary purpose is to abstract functionality that can be applied to an aggregate hierarchy of "element" objects.
  3. Visitor implements "double dispatch". In "double dispatch", the operation executed depends on: the name of the request, and the type of TWO receivers (the type of the Visitor and the type of the element it visits).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment