设计模式(9)---建造者模式

一、定义

建造者模式:将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。

解释:使用建造者模式,用户只需要指定需要构造的类型就可以得到它们,而具体建造的过程和细节就不需要知道。

 

二、UML类图

三、基本代码

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,从而完成具体产品的创建。而在客户端代码中,首先实例化指挥者、具体建造者,指挥者指挥具体建造者对产品进行创建,从而完成生产产品的功能。

 

四、具体实例

   具体需求:采购员(客户端)需要采购一批电脑,他会到电脑城找到老板(指挥者)说明要求,老板会安排人员按照客户的要求进行电脑组装。整个过程,采购员不会涉及到电脑组装的复杂工程中去,他只负责提需求,然后采购到电脑即可。

产品类电脑:

    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();

 

五、简化示例:

在具体应用中,指挥者和抽象建造者可以省略一个或者都省略,本例中只有产品类和建造者:

产品类:

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();

 

六、总结

  建造者模式实现步骤: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

 

posted @ 2014-11-05 14:43  小项目笔记  阅读(1101)  评论(0编辑  收藏  举报

更多文章请关注公众号:小项目笔记

小项目笔记