Wednesday, January 28, 2015

DesignPatterns

Types Of Design Patterns:
  • Creational Design Patterns
    • Singleton Design
    • Prototype Design
    • Factory Design
    • Abstract Design
    • Builder Design
  • Structural Design Patterns
    • Adapter Design
    • Bridge Design
    • Decorator Design
    • Proxy Design
    • Flyweight Design
    • Composite Design
    • Facade Design
  • Behavioral Design Patterns
    • Observer Design
    • Iterator Design
    • Strategy Design
    • State Design
    • Visitor Design
    • Mediator Design
    • Command Design
    • Template Design
    • Null Object Design
    • Memento Design

Creational Design Patterns:

Deal with the process of object creation, trying to make it more flexible and efficient. It makes the system independent and how its objects are created, composed, and represented.

Singleton Design Pattern:

  1. Define a private static attribute in the "single instance" class.
  2. Define a public static accessor function in the class.
  3. Do "lazy initialization" (creation on first use) in the accessor function.
  4. Define all constructors to be protected or private.
  5. Clients may only use the accessor function to manipulate the Singleton.

Factory Design Pattern & Abstract Design Pattern

  • Factory Method is used to create one product only but Abstract Factory is about creating families of related or dependent products.
  • Factory Method pattern exposes a method to the client for creating the object whereas in the case of Abstract Factory they expose a family of related objects which may consist of these Factory methods.
  • Factory Method pattern hides the construction of a single object whereas Abstract Factory hides the construction of a family of related objects. Abstract factories are usually implemented using (a set of) factory methods.
  • Abstract Factory pattern uses composition to delegate the responsibility of creating an object to another class while Factory Method design pattern uses inheritance and relies on a derived class or subclass to create an object.
  • The idea behind the Factory Method pattern is that it allows for the case where a client doesn't know what concrete classes it will be required to create at runtime, but just wants to get a class that will do the job while Abstract Factory pattern is best utilized when your system has to create multiple families of products or you want to provide a library of products without exposing the implementation details.!





Chain Of Responsibilities:
Only one receiver in the chain can handle the requests
One or more receiver in the change can handle the requests.

Real Time Scenario:
Production Support: Level 1, Level 2, Level 3

Template Design:

Abstract class Coffee {
prepareCoffee(){
addWater()
addMilk()
addSugar()
addPower()
}
}

Momento:
It will capture and restore the object internal state without violating its encapsulation/ without exposing its implementation details.
Eg: Text Editor, Visual Studio code

Real-World Analogy of Memento Design Pattern

Imagine you’re an artist painting a picture (Originator). You have a beautiful painting that you’ve been working on, and you want to make sure you can save its progress and go back to previous versions if needed.

  • Originator (The Artist):
    • You, the artist, represent the Originator.
    • You’re responsible for creating and managing the state of your painting (object).
    • As you work on your painting, you can create snapshots or “Mementos” of its current state.
  • Memento (The Photograph):
    • A Memento in this analogy could be likened to a photograph of your painting at a specific point in time.
    • Just like taking a picture captures the appearance of your painting at that moment, a Memento captures the state of your painting.
    • You could take a photograph of your painting at various stages of completion to have snapshots of its progress.
  • Caretaker (The Art Collector):
    • The Caretaker is like an art collector who helps you manage your paintings and their snapshots.
    • They don’t paint the pictures themselves (like the Originator), but they’re responsible for keeping track of the snapshots (Mementos) and ensuring they’re stored safely.
    • They might label each photograph with a date or description to help you remember which stage of the painting it represents.
    • When you want to revisit an earlier version of your painting, you ask the art collector (Caretaker) to retrieve the corresponding photograph (Memento) for you to study or restore.

So, in this analogy, you, the artist, create and manage your painting (Originator), take photographs to capture its progress (Memento), and rely on an art collector (Caretaker) to organize and store those photographs for future reference or restoration. This illustrates how the components of the Memento pattern work together in a real-life scenario.

FlyWeight:
Charcters

Facade:
Instacart
Amazon
Door Dash

State Design Pattern:
Traffic Light
Delivery package

No comments:

Security Certificates

  1. Cryptography Basics Understand Key Concepts : Encryption, decryption, hashing, and digital signatures. Key terms: confidentiality, inte...