What is Builder Pattern?
Builder pattern builds a complex object by using a step by step approach. Builder interface defines the steps to build the final object. This builder is independent from the objects creation process. A class that is known as Director, controls the object creation process.Moreover, builder pattern describes a way to separate an object from its construction. The same construction method can create different representation of the object.
Builder Pattern - UML Diagram & Implementation
The UML class diagram for the implementation of the builder design pattern is given below:- Builder
This is an interface which is used to define all the steps to create a product - ConcreteBuilder
This is a class which implements the Builder interface to create complex product. - Product
This is a class which defines the parts of the complex object which are to be generated by the builder pattern. - Director
This is a class which is used to construct an object using the Builder interface.
- public interface IBuilder
- {
- void BuildPart1();
- void BuildPart2();
- void BuildPart3();
- Product GetProduct();
- }
- public class ConcreteBuilder : IBuilder
- {
- private Product _product = new Product();
- public void BuildPart1()
- {
- _product.Part1 = "Part 1";
- }
- public void BuildPart2()
- {
- _product.Part2 = "Part 2";
- }
- public void BuildPart3()
- {
- _product.Part3 = "Part 3";
- }
- public Product GetProduct()
- {
- return _product;
- }
- }
- public class Product
- {
- public string Part1 { get; set; }
- public string Part2 { get; set; }
- public string Part3 { get; set; }
- }
- public class Director
- {
- public void Construct(IBuilder IBuilder)
- {
- IBuilder.BuildPart1();
- IBuilder.BuildPart2();
- IBuilder.BuildPart3();
- }
- }
Example
- IVehicleBuilder - Builder interface
- HeroBuilder & HondaBuilder- Concrete Builder
- Vehicle- Product
- Vehicle Creator - Director
- /// <summary>
- /// The 'Builder' interface
- /// </summary>
- public interface IVehicleBuilder
- {
- void SetModel();
- void SetEngine();
- void SetTransmission();
- void SetBody();
- void SetAccessories();
- Vehicle GetVehicle();
- }
- /// <summary>
- /// The 'ConcreteBuilder1' class
- /// </summary>
- public class HeroBuilder : IVehicleBuilder
- {
- Vehicle objVehicle = new Vehicle();
- public void SetModel()
- {
- objVehicle.Model = "Hero";
- }
- public void SetEngine()
- {
- objVehicle.Engine = "4 Stroke";
- }
- public void SetTransmission()
- {
- objVehicle.Transmission = "120 km/hr";
- }
- public void SetBody()
- {
- objVehicle.Body = "Plastic";
- }
- public void SetAccessories()
- {
- objVehicle.Accessories.Add("Seat Cover");
- objVehicle.Accessories.Add("Rear Mirror");
- }
- public Vehicle GetVehicle()
- {
- return objVehicle;
- }
- }
- /// <summary>
- /// The 'ConcreteBuilder2' class
- /// </summary>
- public class HondaBuilder : IVehicleBuilder
- {
- Vehicle objVehicle = new Vehicle();
- public void SetModel()
- {
- objVehicle.Model = "Honda";
- }
- public void SetEngine()
- {
- objVehicle.Engine = "4 Stroke";
- }
- public void SetTransmission()
- {
- objVehicle.Transmission = "125 Km/hr";
- }
- public void SetBody()
- {
- objVehicle.Body = "Plastic";
- }
- public void SetAccessories()
- {
- objVehicle.Accessories.Add("Seat Cover");
- objVehicle.Accessories.Add("Rear Mirror");
- objVehicle.Accessories.Add("Helmet");
- }
- public Vehicle GetVehicle()
- {
- return objVehicle;
- }
- }
- /// <summary>
- /// The 'Product' class
- /// </summary>
- public class Vehicle
- {
- public string Model { get; set; }
- public string Engine { get; set; }
- public string Transmission { get; set; }
- public string Body { get; set; }
- public List<string> Accessories { get; set; }
- public Vehicle()
- {
- Accessories = new List<string>();
- }
- public void ShowInfo()
- {
- Console.WriteLine("Model: {0}", Model);
- Console.WriteLine("Engine: {0}", Engine);
- Console.WriteLine("Body: {0}", Body);
- Console.WriteLine("Transmission: {0}", Transmission);
- Console.WriteLine("Accessories:");
- foreach (var accessory in Accessories)
- {
- Console.WriteLine("\t{0}", accessory);
- }
- }
- }
- /// <summary>
- /// The 'Director' class
- /// </summary>
- public class VehicleCreator
- {
- private readonly IVehicleBuilder objBuilder;
- public VehicleCreator(IVehicleBuilder builder)
- {
- objBuilder = builder;
- }
- public void CreateVehicle()
- {
- objBuilder.SetModel();
- objBuilder.SetEngine();
- objBuilder.SetBody();
- objBuilder.SetTransmission();
- objBuilder.SetAccessories();
- }
- public Vehicle GetVehicle()
- {
- return objBuilder.GetVehicle();
- }
- }
- /// <summary>
- /// Builder Design Pattern Demo
- /// </summary>
- class Program
- {
- static void Main(string[] args)
- {
- var vehicleCreator = new VehicleCreator(new HeroBuilder());
- vehicleCreator.CreateVehicle();
- var vehicle = vehicleCreator.GetVehicle();
- vehicle.ShowInfo();
- Console.WriteLine("---------------------------------------------");
- vehicleCreator = new VehicleCreator(new HondaBuilder());
- vehicleCreator.CreateVehicle();
- vehicle = vehicleCreator.GetVehicle();
- vehicle.ShowInfo();
- Console.ReadKey();
- }
- }
- Need to create an object in several steps (a step by step approach).
- The creation of objects should be independent from the way the object's parts are assembled.
- Runtime control over the creation process is required.
Prototype Design PatternPrototype pattern falls under Creational Pattern of Gang of Four (GOF) Design Patterns in .Net. It is used to used to create a duplicate object or clone of the current object. It provides an interface for creating parts of a product. In this article, I would like share what is Prototype pattern and how is it work?
For Example: An object is to be created after a costly database operation. We can cache the object, returns its clone on next request and update the database as and when needed thus reducing database calls.
Prototype Pattern - UML Diagram & Implementation
The UML class diagram for the implementation of the Prototype design pattern is given below:
- Prototype
This is an interface which is used for the types of object that can be cloned itself. - ConcretePrototype
This is a class which implements the Prototype interface for cloning itself.
- public interface Prototype
- {
- Prototype Clone();
- }
- public class ConcretePrototypeA : Prototype
- {
- public Prototype Clone()
- {
- // Shallow Copy: only top-level objects are duplicated
- return (Prototype)MemberwiseClone();
- // Deep Copy: all objects are duplicated
- //return (Prototype)this.Clone();
- }
- }
- public class ConcretePrototypeB : Prototype
- {
- public Prototype Clone()
- {
- // Shallow Copy: only top-level objects are duplicated
- return (Prototype)MemberwiseClone();
- // Deep Copy: all objects are duplicated
- //return (Prototype)this.Clone();
- }
- }
Example
- IEmployee - Prototype interface
- Developer & Typist- Concrete Prototype
- /// <summary>
- /// The 'Prototype' interface
- /// </summary>
- public interface IEmployee
- {
- IEmployee Clone();
- string GetDetails();
- }
- /// <summary>
- /// A 'ConcretePrototype' class
- /// </summary>
- public class Developer : IEmployee
- {
- public int WordsPerMinute { get; set; }
- public string Name { get; set; }
- public string Role { get; set; }
- public string PreferredLanguage { get; set; }
- public IEmployee Clone()
- {
- // Shallow Copy: only top-level objects are duplicated
- return (IEmployee)MemberwiseClone();
- // Deep Copy: all objects are duplicated
- //return (IEmployee)this.Clone();
- }
- public string GetDetails()
- {
- return string.Format("{0} - {1} - {2}", Name, Role, PreferredLanguage);
- }
- }
- /// <summary>
- /// A 'ConcretePrototype' class
- /// </summary>
- public class Typist : IEmployee
- {
- public int WordsPerMinute { get; set; }
- public string Name { get; set; }
- public string Role { get; set; }
- public IEmployee Clone()
- {
- // Shallow Copy: only top-level objects are duplicated
- return (IEmployee)MemberwiseClone();
- // Deep Copy: all objects are duplicated
- //return (IEmployee)this.Clone();
- }
- public string GetDetails()
- {
- return string.Format("{0} - {1} - {2}wpm", Name, Role, WordsPerMinute);
- }
- }
- /// <summary>
- /// Prototype Pattern Demo
- /// </summary>
- class Program
- {
- static void Main(string[] args)
- {
- Developer dev = new Developer();
- dev.Name = "Rahul";
- dev.Role = "Team Leader";
- dev.PreferredLanguage = "C#";
- Developer devCopy = (Developer)dev.Clone();
- devCopy.Name = "Arif"; //Not mention Role and PreferredLanguage, it will copy above
- Console.WriteLine(dev.GetDetails());
- Console.WriteLine(devCopy.GetDetails());
- Typist typist = new Typist();
- typist.Name = "Monu";
- typist.Role = "Typist";
- typist.WordsPerMinute = 120;
- Typist typistCopy = (Typist)typist.Clone();
- typistCopy.Name = "Sahil";
- typistCopy.WordsPerMinute = 115;//Not mention Role, it will copy above
- Console.WriteLine(typist.GetDetails());
- Console.WriteLine(typistCopy.GetDetails());
- Console.ReadKey();
- }
- }
- The creation of each object is costly or complex.
- A limited number of state combinations exist in an object.
Really enjoyed this article.Much thanks again. Want more
ReplyDeletedot net training
dot net online training