forked from AmazingCode/DesignModel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path13、建造者模式.cs
67 lines (65 loc) · 1.93 KB
/
13、建造者模式.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
/// <summary>
/// 建造者模式:用于创建一些复杂的对象,这些对象内部构建间的建造顺序较稳定,但是内部的构建过程是复杂变化的。
/// 建造者模式的好处就是使得建造代码和具体的表示代码分离,由于建造者隐藏了该产品的具体实现,如果需要改变一个产品的内部表示,只需要重新再定义一个建造者就行了,或者在同一个建造者内部重新写一个方法
/// 建造者Builder更像是一种对现有方法的有顺序整合
/// </summary>
class _13_建造者模式
{
}
public abstract class Person
{
public abstract void BuildHead();
public abstract void BuildBody();
public abstract void BuildLag();
}
public class TinPerson:Person
{
public override void BuildHead()
{
Console.WriteLine("TinHead");
}
public override void BuildBody()
{
Console.WriteLine("TinBody");
}
public override void BuildLag()
{
Console.WriteLine("TinLag");
}
}
public class FatPerson:Person
{
public override void BuildHead()
{
throw new NotImplementedException();
}
public override void BuildBody()
{
throw new NotImplementedException();
}
public override void BuildLag()
{
throw new NotImplementedException();
}
}
public class Builder
{
public Person Person {get; private set; }
public Builder(Person person)
{
this.Person = person;
}
public void Build(Person person)
{
person.BuildHead();
Person.BuildBody();
person.BuildLag();
}
}
}