第13章 好菜每回味不同--建造者模式@H_403_5@
13.2 炒面没放盐@H_403_5@
“今天我就吃了两盘垃圾,其实这里面最关键的就在于我们是吃的爽还是吃的难受都要依赖于厨师。你再想想我们设计模式的原则?”@H_403_5@
“啊,你的意思是依赖倒转原则?抽象不应该依赖细节,细节应该依赖于抽象,由于我们要吃的菜都依赖于厨师这样的细节,所以我们很被动。”@H_403_5@
“老麦老肯他们的产品的,味道是由什么决定的?”“那是由她们的工作流程决定的......”@H_403_5@
@H_403_5@
13.2 建造小人一@H_403_5@
Pen p = new Pen(Color.Yellow);@H_403_5@
Graphics gThin = pictureBox1.CreateGraphics();@H_403_5@
gThin.DrawEllipse(p,50,20,30,30);//头@H_403_5@
gThin.DrawRectangle(p,60,10,50);//身体@H_403_5@
gThin.DrawLine(p,40,100);//左手@H_403_5@
gThin.DrawLine(p,70,90,100);//右手@H_403_5@
gThin.DrawLine(p,100,45,150);//左脚@H_403_5@
gThin.DrawLine(p,85,150);//右脚@H_403_5@
@H_403_5@
如果想画一个身体比较胖的小人呢?@H_403_5@
Graphics gFat = pictureBox2.CreateGraphics();@H_403_5@
@H_403_5@
gFat.DrawEllipse(p,30);@H_403_5@
gFat.DrawEllipse(p,50);@H_403_5@
gFat.DrawLine(p,100);@H_403_5@
gFat.DrawLine(p,80,150);@H_403_5@
注意看,新建造的小人少了一部分@H_403_5@
@H_403_5@
流程依赖于程序员自身,显然是不稳定的,在建造过程中很有可能出现不稳定。@H_403_5@
@H_403_5@
13.3 建造小人二@H_403_5@
//廋人类@H_403_5@
class PersonThinBuilder@H_403_5@
{@H_403_5@
private Graphics g;@H_403_5@
private Pen p;@H_403_5@
@H_403_5@
public PersonThinBuilder(Graphics g,Pen p)@H_403_5@
{@H_403_5@
this.g = g;@H_403_5@
this.p = p;@H_403_5@
}@H_403_5@
@H_403_5@
public void Build()@H_403_5@
{@H_403_5@
g.DrawEllipse(p,30);//头@H_403_5@
g.DrawRectangle(p,50);//身体@H_403_5@
g.DrawLine(p,100);//左手@H_403_5@
g.DrawLine(p,100);//右手@H_403_5@
g.DrawLine(p,150);//左脚@H_403_5@
g.DrawLine(p,150);//右脚@H_403_5@
}@H_403_5@
}@H_403_5@
@H_403_5@
胖人的类也是类似的。@H_403_5@
Pen p = new Pen(Color.Yellow);@H_403_5@
@H_403_5@
Graphics gThin = pictureBox1.CreateGraphics();@H_403_5@
PersonThinBuilder ptb = new PersonThinBuilder(gThin,p);@H_403_5@
ptb.Build();@H_403_5@
@H_403_5@
Graphics gFat = pictureBox2.CreateGraphics();@H_403_5@
PersonThinBuilder pfb = new PersonThinBuilder(gFat,p);@H_403_5@
pfb.Build();@H_403_5@
@H_403_5@
@H_403_5@
13.4 建造者模式@H_403_5@
“仔细分析你会发现,这里建造小人的‘过程’是稳定的,而具体建造的‘细节’是不同的,但对于用户来说,这些都不重要,需要的时候给用户建造一个就行了”。@H_403_5@
如果你需要将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示的意图时,我们需要应用于一个设计模式,‘建造者(Builder)模式’,又叫生成器模式。@H_403_5@
建造者模式可以将一个产品的内部表象与产品的生产过程分割开来,从而可以使一个建造过程生成具有不同的内部表象的产品对象。如果我们用了建造者模式,那么用户就只需指定需要建造的类型就可以得到他们,而具体的建造的过程和细节就不需要知道了。@H_403_5@
建造者模式(Builder),讲一个复杂对象的构建与它的表示分离,使得同样的构建过程创建不同的表示。@H_403_5@
@H_403_5@
//建造人的抽象类@H_403_5@
abstract class PersonBuilder@H_403_5@
{@H_403_5@
protected Graphics g;@H_403_5@
protected Pen p;@H_403_5@
@H_403_5@
public PersonBuilder(Graphics g,Pen p)@H_403_5@
{@H_403_5@
this.g = g;@H_403_5@
this.p = p;@H_403_5@
}@H_403_5@
@H_403_5@
public abstract void BuildHead();@H_403_5@
public abstract void BuildBody();@H_403_5@
public abstract void BuildArmLeft();@H_403_5@
public abstract void BuildArmRight();@H_403_5@
public abstract void BuildLegLeft();@H_403_5@
public abstract void BuildLegRight();@H_403_5@
}@H_403_5@
@H_403_5@
//瘦子类@H_403_5@
class PersonThisBuilder : PersonBuilder@H_403_5@
{@H_403_5@
public PersonThisBuilder(Graphics g,Pen p) : base(g,p)@H_403_5@
{ }@H_403_5@
@H_403_5@
public override void BuilderHead()@H_403_5@
{@H_403_5@
g.DrawEllipse(p,30);@H_403_5@
}@H_403_5@
@H_403_5@
public override void BuildBody()@H_403_5@
{@H_403_5@
g.DrawRectangle(p,50);@H_403_5@
}@H_403_5@
@H_403_5@
public override void BuildArmLeft()@H_403_5@
{@H_403_5@
g.DrawLine(p,100);@H_403_5@
}@H_403_5@
@H_403_5@
public override void BuildArmRight()@H_403_5@
{@H_403_5@
g.DrawLine(p,100);@H_403_5@
}@H_403_5@
@H_403_5@
public override void BuildLegLeft()@H_403_5@
{@H_403_5@
g.DrawLine(p,190,150);@H_403_5@
}@H_403_5@
@H_403_5@
public override void BuildLegRight()@H_403_5@
{@H_403_5@
g.DrawLine(p,150);@H_403_5@
}@H_403_5@
}@H_403_5@
@H_403_5@
现在我们还缺建造者模式中一个很重要的类,指挥者(Director)类,用它来控制建造过程,也用它来隔离用户与建造过程的关联。@H_403_5@
class PersonDirector@H_403_5@
{@H_403_5@
private PersonBuilder pb;@H_403_5@
public PersonDirector(PersonBuilder pb)@H_403_5@
{@H_403_5@
this.pb = pb;@H_403_5@
}@H_403_5@
public void CreatePerson()@H_403_5@
{@H_403_5@
pb.BuildHead();@H_403_5@
pb.BuildBody();@H_403_5@
pb.BuildArmLeft();@H_403_5@
pb.BuildArmRight();@H_403_5@
pb.BuildLegLeft();@H_403_5@
pb.BuildLegRight();@H_403_5@
}@H_403_5@
}@H_403_5@
PersonDirector类的目的就是根据用户的选择来一步一步建造小人,而建造的过程在指挥者这里完成了,用户就不需要知道了,而且,由于这个过程每一步都是一定要做的,那就不会让少一只手、少一条腿的问题出现了。@H_403_5@
@H_403_5@
Pen p = new Pen(Color,Yellow);@H_403_5@
PersonThisBuilder ptb = new PersonThisBuilder(pictureBox1,CreateGraphics(),p);@H_403_5@
PersonDirector pdThin = new PersonDirector(pub);@H_403_5@
pdThin.CreatePerson();@H_403_5@
@H_403_5@
PersonFatBuilder pfb = new PersonFatBuilder(pictureBox2,p);@H_403_5@
PersonDirector pdFat = new PersonDirector(pfb);@H_403_5@
pdFat.CreatePerson();@H_403_5@
@H_403_5@
@H_403_5@
@H_403_5@
13.5 建造者模式解析@H_403_5@
@H_403_5@
@H_403_5@
Builder是什么?是为创造一个Product对象的各个部件指定的抽象接口。ConcreteBuilder是什么呢?他是具体的建造者,实现Builder接口,构造和装配各个部件。Product当然就是那些具体的产品了。@H_403_5@
Director是指挥者,它主要是用来创建一些复杂的对象,这些对象内部构建间的建造顺序通常是稳定的,但对象内部的构建通常面临着复杂的变化。@H_403_5@
建造者模式的好处就是使得建造代码与表示代码分离,由于建造者隐藏了该产品是如何组装的,所以若需要改变一个产品的内部表示,只需要在定义一个具体的建造者就可以了。@H_403_5@
@H_403_5@
Product类——产品类,由多个部件组成。@H_403_5@
class Product@H_403_5@
{@H_403_5@
IList<string> parts = new List<string>();@H_403_5@
public void Add(string part)@H_403_5@
{@H_403_5@
parts.Add(part);@H_403_5@
}@H_403_5@
public void show()@H_403_5@
{@H_403_5@
Console.WriteLine(“\n产品 创建 ----”);@H_403_5@
foreach(string part in parts)@H_403_5@
{@H_403_5@
Console.WriteLine(part);@H_403_5@
}@H_403_5@
}@H_403_5@
}@H_403_5@
@H_403_5@
Builder来--抽象建造者类,确定产品由两个部件PartA和PartB组成,并声明一个得到产品建造后果的方法GetResult。@H_403_5@
@H_403_5@
abstract class Builder@H_403_5@
{@H_403_5@
public abstract void BuildPartA();@H_403_5@
public abstract void BuildPartB();@H_403_5@
public abstract Product GetResult();@H_403_5@
}@H_403_5@
@H_403_5@
ConcreteBuilder1类—具体建造者类。@H_403_5@
class ConcreteBuilder1 : Builder@H_403_5@
{@H_403_5@
private Product product = new Product;@H_403_5@
public override void BuildPartA()@H_403_5@
{@H_403_5@
product.Add(“部件A”);@H_403_5@
}@H_403_5@
public override void BuildPartB()@H_403_5@
{@H_403_5@
product.Add(“部件B”);@H_403_5@
}@H_403_5@
@H_403_5@
public override Product GetResult()@H_403_5@
{@H_403_5@
return product;@H_403_5@
}@H_403_5@
}@H_403_5@
@H_403_5@
具体建造者类:ConcreteBuilder2类……@H_403_5@
@H_403_5@
Director类--指挥者类@H_403_5@
class Director@H_403_5@
{@H_403_5@
public void Construct(Builder builder)@H_403_5@
{@H_403_5@
builder.BuilderPartA();@H_403_5@
builder.BuilderPartB();@H_403_5@
}@H_403_5@
}@H_403_5@
//客户端代码,客户不需知道具体的建造过程。@H_403_5@
static void Main(string[] args)@H_403_5@
{@H_403_5@
Director director = new Director();@H_403_5@
Builder b1 = new ConcreteBuilder1();@H_403_5@
Builder b2 = new ConcreteBuilder2();@H_403_5@
@H_403_5@
director.Construct(b1);@H_403_5@
Product p1 = b1.GetResult();@H_403_5@
p1.Show();@H_403_5@
@H_403_5@
director.Construct(b2);@H_403_5@
Product p2 = b2.GetResult();@H_403_5@
p2.Show();@H_403_5@
@H_403_5@
Console.Read();@H_403_5@
}@H_403_5@
当创建 复杂对象的算法 应该 独立于 该对象的组成部分 以及它们的装配方式时非常使用。@H_403_5@ 原文链接:https://www.f2er.com/javaschema/283821.html