What is Design Pattern ??

Usage of Design Patterns

Types of Design Patterns

Creational Patterns

  • Singleton Design Pattern
  • Factory Design Pattern
  • Abstract Factory Design Pattern
  • Builder Design Pattern
  • Prototype Design Pattern

Structural Patterns

  • Adapter Design Pattern
  • Composite Design Pattern
  • Proxy Design Pattern
  • Fly Weight Design Pattern
  • Facade Design Pattern
  • Bridge Design Pattern
  • Decorator Design Pattern

Behavioral Patterns

  • Chain of Responsibility Design Pattern
  • Template Design Pattern
  • Observer Design Pattern
  • Strategy Design Pattern
  • Memento Design Pattern
  • Command Design Pattern

Singleton Design Pattern

Usecase of Singleton Design Pattern

Imagine we have a printer in an office. For the operation of that printer, we have a singleton class called PrintManager. PrintManager is responsible to create a single instance of itself for printer management purposes. The PrintManager is also responsible for the creation and provision of access to a single instance of Printer for printing purposes.

🔸 You can find the implementation for the Singleton Design Pattern in this GitHub Repository.

Figure 1: Overview UML diagram of the Singleton Design Pattern Usecase

Factory Design Pattern

Usecase of Factory Design Pattern

In this scenario, imagine we have different kinds of printers in an office as LaserPrinter, InkjetPrinter, LEDPrinter, and ThreeDPrinter. All these printer types are subclasses of the class Printer that implements the base functionalities of printers. For the creation of different printer instances depending on the user’s input, we have a class called PrinterFactory. In our program, we can pass the type of printer we need to the PrinterFactory class. Then we can get the desired printer instance as the output for operation purposes.

🔸 You can find the implementation for the Factory Design Pattern in this GitHub Repository.

Figure 2: Overview UML diagram of the Factory Design Pattern Usecase

Prototype Design Pattern

  • Shallow Copy: Copy the value type fields to a new object when cloning. When the data is a reference type, only the reference is copied but not the referenced object itself. Therefore the original and clone refer to the same object.
  • Deep Copy: A complete copy of the internal reference type is made when cloning the objects. Copy the value type fields to a new object. If the specified field is a reference type, then a new copy of the referenced object is assigned. Therefore the original and clone will refer to different objects.

Usecase of Prototype Design Pattern

In this scenario, imagine we have different kinds of meals in a Restaurant as ChildrenMeal, VegeterianMeal, and NonVegeterianMeal. All these meal types are subclasses of the class Meal that implements the base functionalities and base attributes of different meals. Since several steps needed to be performed when creating different meals, such as picking the main meal, side meal, beverages, dessert, etc. MealRegistry class is used for the creation and storage of all default meal types in a HashMap. After that, when a user asks for a meal-type, they can pass that meal-type they want and get a clone of the pre-created instances of ChildrenMeal, VegetarianMeal, and NonVegetarianMeal classes. After obtaining cloned instance, if we like, we can set different values to the main meal, side meal, beverages, dessert, etc.

🔸 You can find the implementation for the Prototype Design Pattern in this GitHub Repository.

Figure 3: Overview UML diagram of the Prototype Design Pattern Usecase

Software Engineer at Virtusa, Graduate of Sri Lanka Institute of Information Technology (SLIIT).