建造者模式:将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。
需求分析:
1 需要生成的产品有复杂的内部结构。
2 需要生成的产品的属性相互依赖
3 建造者模式可以强迫生成顺序
4 在对象常见过程中会使用到系统中的一些其他对象,这些对象在产品对象的创建过程中不易得到。
建造者模式是在当创建复杂对象的算法应该独立于该对象的组成部分以及他们的装配方式时使用的模式。
//建造者模式
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{//头
abstract class head//抽象方法,提出需要建造的抽象函数
{
protected string bianhaostr;
protected string datestr;
protected string geshistr;
public abstract void bianhao();
public abstract void date();
public abstract void geshi();
}
class Head : head//具体实现需要建造的函数
{
public Head(string bianhaostr, string datestr, string geshistr)
{
this.bianhaostr = bianhaostr;
this.datestr = datestr;
this.geshistr = geshistr;
}
public override void bianhao()
{
Console.WriteLine("编号:{0}", this.bianhaostr);
}
public override void date()
{
Console.WriteLine("日期:{0}", this.datestr);
;
}
public override void geshi()
{
Console.WriteLine("编号:{0}", this.geshistr);
}
}
abstract class body
{//中
protected string name;
protected string miaoshu;
public abstract void Name();
public abstract void MaioShu();
}
class Body : body
{
public Body(string name, string miaoshu)
{
this.name = name;
this.miaoshu = miaoshu;
}
public override void Name()
{
Console.WriteLine("表的名称:{0}", this.name);
throw new NotImplementedException();
}
public override void MaioShu()
{
Console.WriteLine("页面描述:{}", this.miaoshu);
throw new NotImplementedException();
}
}
abstract class tail
{//尾巴
protected string people;
public abstract void People();
}
class Tail : tail
{
public Tail(string people)
{
this.people = people;
}
public override void People()
{
Console.WriteLine("所有人:{0}");
}
}
class GetAndSet
{//将获取以及输出集成到此类,类似外观模式方法
private Head h;
private Body b;
private Tail t;
public void gethead(string bianhaostr,string datestr,string geshistr)
{
h = new Head(bianhaostr, datestr, geshistr);
}
public void getbody(string name,string miaoshu)
{
b = new Body(name, miaoshu);
}
public void gettail(string people)
{
t = new Tail(people);
}
public void sethead()
{
h.bianhao();
h.date();
h.geshi();
}
public void setbody()
{
b.Name();
b.MaioShu();
}
public void settail()
{
t.People();
}
}
class Director//指挥者,指挥建造好的函数,实现具体行为。
{
private GetAndSet f;
public Director(GetAndSet f)
{
this.f = f;
}
public void play()
{
f.sethead();
f.setbody();
f.settail();
}
}
class Program
{
static void Main(string[] args)
{
GetAndSet gas=new GetAndSet();
gas.gethead(Console.ReadLine(),Console.ReadLine(),Console.ReadLine());
gas.getbody(Console.ReadLine(),Console.ReadLine());
gas.gettail(Console.ReadLine());
Director D = new Director(gas);
D.play();
Console.ReadLine();
}
}
}