View
1.187
Download
3
Category
Tags:
Preview:
DESCRIPTION
Generic Construction -Different Representation This presentation provide information to understand builder design pattern, it’s structure, it’s implementation.
Citation preview
Builder Design PatternGeneric 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
• Separate the construction of complex object from its representation.
• Same construction process is used to create different representation.
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.
Structure
builder
Director
Construct()
Concrete ProductA
<< interface >>
Product
Concrete ProductB
InheritanceInheritance
<< interface >>
Builder
BuildPart()
Concrete BuilderA
BuildPart()
GetProduct()
Concrete BuilderB
BuildPart()
GetProduct()
Builder.Buildpart()
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 Director Concrete Buildernew 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 mBrakingSystem;
public string BrakingSystem {
get { return mBrakingSystem; }
set { mBrakingSystem = value; }
}
private string mBody;
public string Body {
get { return mBody; }
set { mBody = value; }
}
private string mInterior;
public string Interior {
get { return mInterior; }
set { mInterior = value; }
}
public abstract void Drive();
}
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 }
- mBrakingSystem (string)
+ BrakingSystem { get set }
- mInterior (string)
+ Interior { get set }
- mBody (string)
+ Body { get set }
+ Drive ()
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 andoverriding method “Drive” (giving class specific
definition of function) of “Vehicle” class.
<< interface >> Product
- mEngine (string)
+ Engine { get set }
- mBrakingSystem (string)
+ BrakingSystem { get set }- mInterior (string)
+ Interior { get set }
- mBody (string)
+ Body { get set }
+ Drive ()
Car Truck
Drive () Drive ()
Implementation (C#)Builder (interface)
public abstract class VehicleBuilder {
protected Vehicle vehicle;
public abstract void BuildEngine();
public abstract void BuildBrakingSystem();
public abstract void BuildBody();
public abstract void 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 fullyconstructed “Vehicle” object. Here this
“VehicleBuilder” class is also
responsible for assembling the entire“Vehicle” object.
<< interface >> VehicleBuilder
- vehicle (Vehicle)
+ BuildEngine ()
+ BuildBrakingSystem ()
+ BuildBody ()
+ 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 ()
+ BuildBrakingSystem ()
+ BuildBody ()
+ 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 ()
+ BuildBrakingSystem ()+ BuildBody ()
+ 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();
}
public Vehicle GetVehicle() {
return vehicleBuilder.GetVehicle();
}
}
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.
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 theconstructed “Vehicle” by calling
the “GetVehicle” from
“VehicleBuilder” object.
End of Presentation . . .
Recommended