Thursday 11 May 2017

CPP DP

Design Patterns

Design patterns have been evolved provide best solutions to certain problems faced during software development.

1Creational Patterns
These design patterns provide a way to create objects while hiding the creation logic, rather than instantiating objects directly using new operator. This gives program more flexibility in deciding which objects need to be created for a given use case.
2Structural Patterns
These design patterns concern class and object composition. Concept of inheritance is used to compose interfaces and define ways to compose objects to obtain new functionalities.
3Behavioral Patterns
These design patterns are specifically concerned with communication between objects.

 Singleton and Factory patterns are most populary used for Creational patterns.

In Factory pattern, we create object without exposing the creation logic to the client and refer to newly created object using a common interface.

 

The Factory Method defines an interface for creating objects, but lets subclasses decide which classes to instantiate. Injection molding presses demonstrate this pattern. Manufacturers of plastic toys process plastic molding powder, and inject the plastic into molds of the desired shapes. The class of toy (car, action figure, etc.) is determined by the mold.


//get an object of Circle and call its draw method.
      Shape shape1 = shapeFactory.getShape("CIRCLE");

      //call draw method of Circle
      shape1.draw();

      //get an object of Rectangle and call its draw method.
      Shape shape2 = shapeFactory.getShape("RECTANGLE");

      //call draw method of Rectangle
      shape2.draw();

Singleton pattern involves a single class which is responsible to create an object while making sure that only single object gets created.  

 The Singleton pattern ensures that a class has only one instance and provides a global point of access to that instance.

Facade pattern comes under structural pattern as this pattern adds an interface to existing system to hide its complexities.This pattern involves a single class which provides simplified methods required by client and delegates calls to methods of existing system classes.

 

The Facade defines a unified, higher level interface to a subsystem that makes it easier to use. Consumers encounter a Facade when ordering from a catalog. The consumer calls one number and speaks with a customer service representative. The customer service representative acts as a Facade, providing an interface to the order fulfillment department, the billing department, and the shipping department.
 public ShapeMaker() {
      circle = new Circle();
      rectangle = new Rectangle();
      square = new Square();
   }

Observer pattern is used when there is one-to-many relationship between objects such as if one object is modified, its depenedent objects are to be notified automatically. Observer pattern falls under behavioral pattern category

 

The Observer defines a one-to-many relationship so that when one object changes state, the others are notified and updated automatically. Some auctions demonstrate this pattern. Each bidder possesses a numbered paddle that is used to indicate a bid. The auctioneer starts the bidding, and "observes" when a paddle is raised to accept the bid. The acceptance of the bid changes the bid price which is broadcast to all of the bidders in the form of a new bid.
 

The state pattern is a behavioral software design pattern that implements a state machine in an object-oriented way. This pattern is used in computer programming to encapsulate varying behavior for the same object based on its internal state. This can be a cleaner way for an object to change its behavior at runtime without resorting to large monolithic conditional statements and thus improve maintainability.

  

Strategy Pattern

In Strategy pattern, a class behavior or its algorithm can be changed at run time.
 Context context = new Context(new OperationAdd());  
      System.out.println("10 + 5 = " + context.executeStrategy(10, 5));

      context = new Context(new OperationSubstract());  
      System.out.println("10 - 5 = " + context.executeStrategy(10, 5));


No comments:

Post a Comment