Builder Design Pattern (Generic Construction -Different Representation)

60 %
40 %
Information about Builder Design Pattern (Generic Construction -Different Representation)
Technology

Published on February 18, 2014

Author: SameerRathoud

Source: slideshare.net

Description

Generic Construction -Different Representation
This presentation provide information to understand builder design pattern, it’s structure, it’s implementation.

Builder Design Pattern Generic Construction - Different Representation Sameer Singh Rathoud

About presentation This presentation provide information to understand builder design pattern, it’s structure, it’s implementation. I have tried my best to explain the concept in very simple language. The programming language used for implementation is c#. But any one from different programming background can easily understand the implementation.

Definition Separate the construction of complex object from its representation, such that same construction process can be used to build different representation. Builder pattern is a creational design pattern.

Motivation and Intent At times, application contains complex objects and these complex objects are made of other objects. Such application requires a generic mechanism to build these complex object and related objects. • Separate the construction of complex object from its representation. • Same construction process is used to create different representation.

Structure Director Construct() Builder.Buildpart() builder << interface >> Builder << interface >> Product BuildPart() Inheritance Concrete BuilderA Concrete BuilderB BuildPart() BuildPart() GetProduct() GetProduct() Inheritance Concrete ProductA Concrete ProductB

Participants in structure • Director: Construct a object using Builder interface. Client interacts with the Director. • Builder (interface): Provides an abstract interface for creating parts of the Product object. • Product (interface): Provides an interface for Product family.

Participants in structure continue … • ConcreteBuilder (Concrete BuilderA and Concrete BuilderB): Provides implementation to Builder interface • Construct and assembles parts of the Product object. • Define and keeps track of representation it creates. • Provide an interface for retrieving the Product object (GetProduct()). • ConcreteProduct (Concrete ProductA and Concrete ProductB): Implements Product interface. • Represents the complex object under construction. ConcreteBuilder builds the product's internal representation and defines the process by which it's assembled. • Includes classes that define the constituent parts, including interfaces for assembling the parts into the final result.

Collaborations • The client creates the Director object and configures it with the desired Builder object. • Director notifies the builder whenever a part of the product should be built. • Builder handles requests from the director and adds parts to the product. • The client retrieves the product from the builder/Director. Client new Concrete Builder Director Concrete Builder new Director(Concrete Builder) Construct() BuildPart1() BuildPart2() BuildPart3() GetResult()

Implementation (C#) Product (Interface) public abstract class Vehicle { private string mEngine; public string Engine { get { return mEngine; } set { mEngine = value; } } private string mInterior; public string Interior { get { return mInterior; } set { mInterior = value; } } public abstract void Drive(); private string mBrakingSystem; } public string BrakingSystem { get { return mBrakingSystem; } set { mBrakingSystem = value; } } private string mBody; public string Body { get { return mBody; } set { mBody = value; } } Here “Vehicle” is an abstract class with some “properties” and abstract method “Drive”. Now all the concrete classes implementing this abstract class will inherit these properties and will override “Drive” method. << interface >> Product - mEngine (string) + Engine { get set } - mBody (string) + Body { get set } - mBrakingSystem (string) + BrakingSystem { get set } + Drive () - mInterior (string) + Interior { get set }

Implementation (C#) ConcreteProduct class Car : Vehicle { public override void Drive() { System.Console.WriteLine("Drive Car"); } } class Truck : Vehicle { public override void Drive() { System.Console.WriteLine("Drive Truck"); } } Here the concrete classes “Car” and “Truck” are implementing abstract class “Vehicle” and these concrete classes are inheriting the properties and overriding method “Drive” (giving class specific definition of function) of “Vehicle” class. << interface >> Product - mEngine (string) + Engine { get set } - mBody (string) + Body { get set } - mBrakingSystem (string) + BrakingSystem { get set } - mInterior (string) + Interior { get set } + Drive () Car Truck Drive () Drive ()

Implementation (C#) Builder (interface) public abstract class VehicleBuilder { protected Vehicle vehicle; public abstract void public abstract void public abstract void public abstract void BuildEngine(); BuildBrakingSystem(); BuildBody(); BuildInterior(); public Vehicle GetVehicle() { return vehicle; } } Here “VehicleBuilder” is an abstract builder class having the reference of “Vehicle” object, few abstract methods for setting the properties of “Vehicle” and method for returning the fully constructed “Vehicle” object. Here this “VehicleBuilder” class is also responsible for assembling the entire “Vehicle” object. << interface >> VehicleBuilder - vehicle (Vehicle) + BuildEngine () + BuildBody () + BuildBrakingSystem () + BuildInterior () + GetVehicle ()

Implementation (C#) ConcreteBuilder class CarBuilder : VehicleBuilder { public CarBuilder() { vehicle = new Car(); } public override void BuildEngine() { vehicle.Engine = "Car Engine"; } public override void BuildBrakingSystem() { vehicle.BrakingSystem = "Car Braking System"; } public override void BuildBody() { vehicle.Body = "Car Body"; } public override void BuildInterior() { vehicle.Interior = "Car Interior"; } } Here “CarBuilder” is a concrete class implementing “VehicleBuilder” class. In the constructor of “CarBuilder”, “Car” object is assigned to the reference of “Vehicle” and class “CarBuilder” overrides the abstract methods defined in “Vehicle” class. << interface >> VehicleBuilder - vehicle (Vehicle) + BuildEngine () + BuildBody () + BuildBrakingSystem () + BuildInterior () + GetVehicle () CarBuilder + BuildEngine () + BuildBrakingSystem () + BuildBody () + BuildInterior ()

Implementation (C#) ConcreteBuilder class TruckBuilder : VehicleBuilder { public TruckBuilder() { vehicle = new Truck(); } public override void BuildEngine() { vehicle.Engine = "Truck Engine"; } public override void BuildBrakingSystem() { vehicle.BrakingSystem = "Truck Braking System"; } public override void BuildBody() { vehicle.Body = "Truck Body"; } public override void BuildInterior() { vehicle.Interior = "Truck Interior"; } } Here “TruckBuilder” is another concrete class implementing “VehicleBuilder” class. In the constructor of “TruckBuilder”, “Truck” object is assigned to the reference of “Vehicle” and class “TruckBuilder” overrides the abstract methods defined in “Vehicle” class. << interface >> VehicleBuilder - vehicle (Vehicle) + BuildEngine () + BuildBody () + BuildBrakingSystem () + BuildInterior () + GetVehicle () TruckBuilder + BuildEngine () + BuildBrakingSystem () + BuildBody () + BuildInterior ()

Implementation (C#) Director class VehicleMaker { private VehicleBuilder vehicleBuilder; public VehicleMaker(VehicleBuilder builder) { vehicleBuilder = builder; } public void BuildVehicle() { vehicleBuilder.BuildEngine(); vehicleBuilder.BuildBrakingSystem(); vehicleBuilder.BuildBody(); vehicleBuilder.BuildInterior(); } Here “VehicleMaker” is a class acting as “Director” for builder pattern. This class will have a reference of “VehicleBuilder”. In the constructor of “VehicleMaker” appropriate builder will get assigned to the reference of “VehicleBuilder”. Additionally this class implements “BuildVehicle” and “GetVehicle” methods. “BuildVehicle” will create the different parts of the vehicle and “GetVehicle” will return the fully constructed “Vehicle” object by calling the “VehicleBuilder.GetVehicle” method. public Vehicle GetVehicle() { return vehicleBuilder.GetVehicle(); } VehicleMaker } + BuildVehicle () + GetVehicle ()

Implementation (C#) Client class Client { static void Main(string[] args) { VehicleBuilder carBuilder = new CarBuilder(); VehicleMaker maker1 = new VehicleMaker(carBuilder); maker1.BuildVehicle(); Vehicle car = carBuilder.GetVehicle(); car.Drive(); VehicleBuilder truckBuilder = new TruckBuilder(); VehicleMaker maker2 = new VehicleMaker(truckBuilder); maker2.BuildVehicle(); Vehicle truck = truckBuilder.GetVehicle(); truck.Drive(); } } For using this builder pattern the client has to create a required “VehicleBuilder” object and a “VehicleMaker” object, pass the created object of “VehicleBuilder” to the constructor of “VehicleMaker”. Call the “BuildVehicle” from “VehicleMaker” object (this call will create the “Vehicle” and assemble it) and we will get the constructed “Vehicle” by calling the “GetVehicle” from “VehicleBuilder” object.

End of Presentation . . .

Add a comment

Related presentations

Related pages

Jean Paul's Blog | c# | Page: 14

Builder Pattern In this ... Definition of Builder The GoF says that the Builder design patterns let you “Separate the construction of a ...
Read more

Jean Paul's Blog | 2011 November | Page: 4

In this article I am trying to demonstrate the usage of Builder Pattern ... Builder The GoF says that the Builder design patterns ... Generic Argument ...
Read more

Factory Pattern in C# - C# (C Sharp) - TechTalkz.com ...

Builder Separates object construction from its representation ... The factory pattern will not solve ... orthogonal to the question of the API design ...
Read more

Introduction to Patterns

Introduction to Patterns. 2 ... Generic, Generic, reusable parts ... design pattern (e.g., LinkStrategy, HyperMediaDecorator)
Read more

REPRINTED FROM IBM SYSTEMS JOURNAL, VOL26, NO 3, 1987 ...

REPRINTED FROM IBM SYSTEMS JOURNAL, VOL26, ... Directs construction activities ... representation Builder's
Read more

Trends on Adaptive Object Models Research

Trends on Adaptive Object Models ... which have been documented as design patterns. A design pattern is a good solution ... representation construction ...
Read more

John Frazer's Accelerating Architecture Lectures

The largest industry in the world is construction, ... theory books and pattern books, generic design, ... © 2012 John Frazer unless otherwise stated.
Read more

Proceedings of the - ResearchGate - Share and discover ...

Proceedings of the 3rd International ... Instead of using a generic pattern specification ... we investigate the automated construction of customized ...
Read more

Death by buzzwords - Web Builder Zone | Web design, PHP ...

Death by buzzwords. ... and a definition close to their code representation is that a pattern cannot be used as ... Construction and ...
Read more

Object-Oriented Software Construction

Object-Oriented Software Construction, ... a generic solution for software-hardware mismatch problem, ... Design pattern density defined, ...
Read more