Design Patterns in Oops

Creational Design Pattern

Following are most used common creational design pattern: 

    Builder Pattern

    Builder Pattern is used to avoid complex Constructors which takes a lot of parameters. For example, a Person class constructor can have name, nationality, weight, height, color, gender, etc. So Builder Pattern helps us doing this constructor creation easy. It created an inner class and through that, we can handle all mandatory and non-mandatory fields.

    Prototype Pattern

    Prototype makes a copy of one original object so that you do not want a system to know how it is created. In most cases, this is created with the help of clone(). Also, it is useful only if we are creating all the objects of similar types. In Spring if we provide scope as a prototype then each it creates a new object

    Factory Method

    This helps us handle the many redundant if-else conditions better, as in runtime throughout Factory design we can decide which type of product is needed. A pizza factory decides which type of pizza to be created instead of Chef creating pizzas directly.

    Abstract Factory Method

    In Abstract Factory we use an Interface for a group of objects without knowing showing how objects are created. Say we have a different type of Pizza like non-veg or veg pizza. So Veg Pizza will have different ingredients than non-veg, so abstract factory comes here to picture to handle it.

    Singleton Pattern

    Singleton Pattern is when we want only one instance of a class that exists throughout the application. This is mainly used when we use heavy resources like DB connection,  file access, etc. Here is an example of java singleton class.

Structural Design Pattern

    Facade Design Pattern

    Facade means the outside of a building, which hides the complexity of internal building structure.
    The facade is used when we have a lot of interconnecting classes and interfaces. So it is difficult for client code to communicate or understand all classes. Facade helps us providing a single interface and makes interaction easier.
    Example: You can consider JustDial as a Facard pattern, as a user you do not know which app to use for flight booking or recharging, that complexity gets taken care by Facade.

    Difference between Facade and Abstract Factory?

    The facade pattern is used when you want to hide an implementation.
    Abstract factory pattern is used when you want to hide the details on constructing instances.

    Adapter design Pattern

    In Adapater design pattern converts interfce of a class into another interface that the client expects.
    So it allows classes work together that coud not work otherwise.

    Bridge Design Pattern

    Composite Design Pattern

    Decorator Design Pattern

    Proxy Pattern

    FLyweight Pattern

    Flyweight pattern is used for sharing a large number of objects, that have the same internal state or property. Flyweight reuses the already existing objects.
    From the interview point of view, the interviewer might ask how to create 3 or 5 objects of a class. So we can do that using the flyweight pattern.

Behavioural Design Pattern

    Startegy Design Pattern

    Template Design Pattern

    Observer Design Pattern

    Iterator Design Pattern

    Interpreter Design Pattern

    Mediator Design Pattern

    Command Design Pattern

Previous
Next Post »

Pages