設計模式(9)---建造者模式

1、定義設計模式

建造者模式:將一個複雜對象的構建與它的表示分離,使得一樣的構建過程能夠建立不一樣的表示。ide

解釋:使用建造者模式,用戶只須要指定須要構造的類型就能夠獲得它們,而具體建造的過程和細節就不須要知道。ui

 

2、UML類圖spa

3、基本代碼設計

class Program
    {
        static void Main(string[] args)
        {
            Director director = new Director();
            Builder builder1 = new ConcreteBuilder1();
            director.Construct(builder1);
            Product product = builder1.GetProduct();
            product.Show();

            Console.Read();
        }
    }

    class Product
    {
        IList<string> parts = new List<string>();

        public void Add(string part)
        {
            parts.Add(part);
        }

        public void Show()
        {
            Console.WriteLine("\n product create");
            foreach (string part in parts)
            {
                Console.WriteLine(part);
            }
        }
    }

    abstract class Builder
    {
        public abstract void BuildPartA();
        public abstract void BuildPartB();
        public abstract Product GetProduct();
    }

    class ConcreteBuilder1 : Builder
    {
        private Product product = new Product();

        public override void BuildPartA()
        {
            product.Add("part A");
        }

        public override void BuildPartB()
        {
            product.Add("part B");
        }

        public override Product GetProduct()
        {
            return product;
        }
    }

    class Director
    {
        public void Construct(Builder builder)
        {
            builder.BuildPartA();
            builder.BuildPartB();
        }
    }

基本代碼中:Director指揮者指揮建造者對產品進行建立。全部具體建造者都繼承接口類builder,從而完成具體產品的建立。而在客戶端代碼中,首先實例化指揮者、具體建造者,指揮者指揮具體建造者對產品進行建立,從而完成生產產品的功能。3d

 

4、具體實例code

   具體需求:採購員(客戶端)須要採購一批電腦,他會到電腦城找到老闆(指揮者)說明要求,老闆會安排人員按照客戶的要求進行電腦組裝。整個過程,採購員不會涉及到電腦組裝的複雜工程中去,他只負責提需求,而後採購到電腦便可。對象

產品類電腦:blog

    public class Computer
    {
        private IList<string> parts = new List<string>();

        public void Add(string part)
        {
            parts.Add(part);
        }

        public void Show()
        {
            Console.WriteLine("電腦開始組裝.................");
            foreach (string part in parts)
            {
                Console.WriteLine("組件" + part + "已裝好");
            }

            Console.WriteLine("電腦組裝好了");
        }
    }

 

抽象建造者:繼承

public abstract class Builder
    {
        public abstract void BuildPartCPU();
        public abstract void BuildPartMainBoard();
        public abstract Computer GetComputer();
    }

 

具體的建造者:

public class ConcreteBuilder1 : Builder
    {
        Computer computer = new Computer();

        public override void BuildPartCPU()
        {
            computer.Add("CPU");
        }

        public override void BuildPartMainBoard()
        {
            computer.Add("main board");
        }

        public override Computer GetComputer()
        {
            return computer;
        }
    }

 

指揮者老闆:

public class Director
    {
        public void Construct(Builder builder)
        {
            builder.BuildPartCPU();
            builder.BuildPartMainBoard();
        }
    }

 

客戶端:

Director director = new Director();
            Builder builder1 = new ConcreteBuilder1();
            director.Construct(builder1);
            Computer computer1 = builder1.GetComputer();
            computer1.Show();

 

5、簡化示例:

在具體應用中,指揮者和抽象建造者能夠省略一個或者都省略,本例中只有產品類和建造者:

產品類:

public class Product
    {
        private IList<string> parts = new List<string>();

        public void Add(string part)
        {
            parts.Add(part);
        }

        public void Show()
        {
            Console.WriteLine("產品開始組裝.................");
            foreach (string part in parts)
            {
                Console.WriteLine("組件" + part + "已裝好");
            }

            Console.WriteLine("產品組裝好了");
        }
    }
View Code

 

建造者:

public class Builder
    {
        private Product product = new Product();

        public void BuildPartA()
        {
            product.Add("PartA");
        }

        public void BuildPartB()
        {
            product.Add("PartB");
        }

        public Product GetProduct()
        {
            return product;
        }

        public void Construct()
        {
            BuildPartA();
            BuildPartB();
        }
    }
View Code

 

客戶端:

builder = new Builder();
            builder.Construct();
            Product product = builder.GetProduct();
            product.Show();

 

6、總結

  建造者模式實現步驟:1)編寫產品類;2)編寫抽象建造者類(可省略);3)編寫具體的建造者;4)編寫指揮者類指揮建造者如何組裝產品;最後客戶端調用便可。建造者模式經過將建造代碼和表現代碼相分離,來實現組裝過程和具體部件解耦。而且對客戶端隱藏了組裝如何實現的內部細節,從而使客戶端無需關心如何組裝,只須要使用便可。

 

增長一個大話設計模式的實例:

public class Product
    {
        IList<string> parts = new List<string>();

        public void Add(string part)
        {
            parts.Add(part);
        }

        public void Show()
        {
            Console.WriteLine("\n product create");
            foreach (string part in parts)
            {
                Console.WriteLine(part);
            }
        }
    }

    public abstract class PersonBuilder
    {
        public abstract void BuildHead();
        public abstract void BuildBody();
        public abstract void BuildHand();
        public abstract void BuildFeet();
        public abstract Product GetProduct();
    }

    public class ConcreteFatBuilder : PersonBuilder
    {
        private Product product = new Product();

        public override void BuildHead()
        {
            product.Add("胖頭");
        }

        public override void BuildBody()
        {
            product.Add("胖身體");
        }

        public override void BuildHand()
        {
            product.Add("胖手");
        }

        public override void BuildFeet()
        {
            product.Add("胖腳");
        }

        public override Product GetProduct()
        {
            return product;
        }
    }

    public class Director
    {
        public void Construct(PersonBuilder builder)
        {
            builder.BuildHead();
            builder.BuildFeet();
            builder.BuildHand();
            builder.BuildBody();
        }
    }


Director director = new Director();
            PersonBuilder builder = new ConcreteFatBuilder();
            director.Construct(builder);
            Product product = builder.GetProduct();
            product.Show();
View Code
相關文章
相關標籤/搜索