Quick Start-Up With Important Design Patterns

Introduction

This article will cover the following things:
  • Brief Concepts
  • Implementation - Creational Patterns
  • Implementation - Structural Patterns
  • Implementation - Behavioral Patterns

Brief Concepts

What are design patterns?

“Provide solutions for iterative OOPS problems”.

Design patterns are divided in to three categories

Class creation problem

Structural design pattern

Instantiation problems

Creational design pattern.

Runtime problems

Behavioral design pattern.


Implementation - Creational Patterns (Using the code)

Factory Design Pattern

  • It centralizes the creation of the objects i.e. it allows us to create a new class, named as “Factory”, which would be responsible for all the object creation.

It is being divided into two sections – Problem and Solution

Steps for creating the Problem

  • Create a console Application named “Design Patterns”
  • Create a folder “Factory” to keep all the factory related classes and interfaces.

     
  • Create two classes “Factory1” and “Factory2” with a method, named as Display (), which is displaying Factory1 and Factory2 as the text within the folder “Factory”.

  • Create the instance of both the classes (Factory1 and Factory2) into the main class and call the Display () method.

  • Run the Application.



    Steps for resolving the problem using Factory Pattern (Solution)
  • Use the same console Application, named “Design Patterns”
  • Create an interface, named “IFactory” with a method “Display ()”.

  • Implement the classes “Factory1” and “Factory2” with an interface “IFactory”.

  • Create a Factory Class “FactoryManager”, which has a method GetClass(int input), based on the input provided and then return the interface type.

  • Call the FactoryManager class from the main method to call both the classes “Factory1” and “Factory2”.

  • Run the Application.

Prototype Design Pattern

  • It allows us to create a clone of the existing object without changing the existing object.

It is being divided into the two sections – Problem and Solution.

Steps for creating the Problem

  • Use an existing console Application, named design patterns.
  • Create a folder “Prototype” to keep all the related classes and interfaces.

  • Create a class, named as Prototype1 with a property Value1 of the string type.

  • Create two objects of the class Prototype1 as an object1 and object2. Assign the value to object1 property – Value1 and then to create a clone of object1, assign the object1 into object2.

  • Run the Application and see the output that the object2’s property value has been assigned to the object1 property.

Steps for resolving the problem using Prototype Pattern (Solution)

  • Use the same console Application, named design patterns.
  • Add another method into the existing class “Prototype1”, named as GetClone, which actually returns the clone of the object, using MemberwiseClone() method.

  • Now, call the GetClone() in the main method, instead of assigning the objects.

  • Run the Application and see the results, that the problem is solved.

Singleton Design Pattern

  • It is used, where we want only one instance of the object to be created and shared between the clients.

It is being divided into the two sections – Problem and Solution

Steps for creating the Problem

  • Use an existing console Application, named design patterns.
  • Create a folder “Singleton” to keep all the related classes and interfaces.

  • Create a class Singleton1, which creates and returns the list of items.

  • As soon as we call ReturnList method from the class Singleton1, it creates a new object for example in the screenshot, given below, the object has been created a couple of times.

  • Run the Application and notice that the result is same, but two instances have been created unnecessarily.

Steps for resolving the problem using Singleton Pattern (Solution)

  • Use the same console Application, named “Design Patterns”.
  • Create a sealed class, named as “SingletonPattern”, which has the initiation of the Singleton1 class and a static property, named as “Instance”, which returns the object of SingletonPattern.

  • Call the SingletonPattern class directly from the main method to access the list.

  • Run the Application to analyz if the same object is being used in both the lists.

Implementation - Structural Patterns (Using the code)

Adapter Design Pattern

  • It helps us to wrap a class around the existing class and make the classes compatible with each other. Consider the figure, given below, ‘Incompatible interfaces’, where both of them are collections to hold the string values. Both of them have a method, which helps us to add the string into the collection. One of the methods is named as ‘Add’ and the other as ‘Push’. One of them uses the collection object and the other uses the stack. We want to make the stack object compatible with the collection object.

It is being divided into two sections – Problem and Solution

Steps for creating the Problem

  • Use an existing console Application, named “Design Patterns”
  • Create a folder “Adapter” to keep all the factory related classes and interfaces.

  • Create two classes “Adapter1”, which is inherited from CollectionBase class with a method as Add(), ReturnValue() and “Adapter2” inherits from Stack class with a method as Push() and ReturnValue(), both are doing the same thing to add the items into the list and stack.
  • Both of them are not compatible, although doing the same thing.

Adapter1 Class

Adapter2 Class



Steps for resolving the problem using Adaptor Pattern (Solution)

  • Use the same console Application, named “Design Patterns”
  • Create a class AdapterPattern, which inherits the Adapter2 class and write the wrapper on it within the method and name it as “Add”.

     
  • As soon as we have another Add method into our wrapper class, they are compatible and can be used for only one Interface.

  • Call the FactoryManager class from the main method to call both the classes “Factory1” and “Factory2”.

  • Run the Application.

Facade Design Pattern

  • It sits on the top of group of subsystems and allows them to communicate in a unified manner.

It is being divided into two sections – Problem and Solution.

Steps for creating the Problem

  • Use an existing console Application, named “Design Patterns”.
  • Create a folder “Facade” to keep all the factory related classes and interfaces.

  • Create two classes “Facade1” and “Facade2”, which are having methods Task1 and Task2 in the respective classes.

     
  • Calling both the class methods (Task1 and Task2) one by one through the main method.
  • Run the Application.

Steps for resolving the problem using Facade Pattern (Solution)

  • Use the same console Application, named “Design Patterns”
  • Create a class FacadePattern, which has a method TaskExecution (), which creates the objects of both the classes and call it one by one.

  • Afterwards, in the main, we just need to call one method of FacadePattern class, which has got all the executions.

  • Run the Application (Output is same).

Implementation - Behavioral Patterns (Using the code)

Observer Design Pattern

  • It helps us to communicate between the parent class and its associated or dependent classes. There are two important concepts in the observer pattern ‘Subject’ and ‘Observers’.
  • The subject sends the notifications, while the observers receive the notifications, if they are registered with the subject and similar to the publisher and subscriber model.

It is being divided into two sections – Problem and Solution

Steps for creating the Problem

  • Create a console Application, named “Design Patterns”.
  • Create a folder “Observer” to keep all the factory related classes and interfaces.

  • Create two classes “Event1” and “Event2” with a method, named as Notify(), which implements the interface IEvent.

     
  • Create the instance of both the classes (Event1 and Event2) into the main class and call the Notify() method of both, as we need to notify all the events.
  • Run the Application.

Steps for resolving the problem using Observer Pattern (Solution)

  • Use the same console Application, named “Design Patterns
  • Create a new class ObserverPattern, which allows us to add all the event with the method AddEvent() and to notify all of them, using NotifyAll().

  • Create the objects of the Event class and add the events into the ObserverPattern class method. Call the NotifyAll method to get the same output.

  • Run the Application (Gets the same output).

 

Points of Interest

After reading and understanding all the design patterns, I came to the conclusion, tha a few of the patterns, listed in the article, mentioned above, must be known to every DEV or ARCHITECT.

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now