设计模式---建造者(Builder)模式,有关系图

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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
abstract class Builder
{
public abstract void SetPrice();
public abstract void SetName();
}

class TVBuilder : Builder
{
private RawMateria rm;

public TVBuilder()
{
rm = new RawMateria();
}

public override void SetPrice()
{
rm.Price = "1500";
}

public override void SetName()
{
rm.Name = "TV";
}

public RawMateria GetTV()
{
return rm;
}
}

class PCBuilder : Builder
{
private RawMateria rm;

public PCBuilder()
{
rm = new RawMateria();
}

public override void SetName()
{
rm.Name = "PC";
}

public override void SetPrice()
{
rm.Price = "2000";
}

public RawMateria GetPC()
{
return rm;
}
}

class RawMateria
{
String _name;
String _price;

public String Name
{
get { return _name; }
set { _name = value; }
}

public String Price
{
get { return _price; }
set { _price = value; }
}

public void Show()
{
Console.WriteLine(this.Name + "    " \ + this.Price );
}
}

class Director
{
private PCBuilder pcbuilder = new PCBuilder();
private TVBuilder tvbuilder = new TVBuilder();

public RawMateria GiveMeTV()
{
tvbuilder.SetName();
tvbuilder.SetPrice();
return tvbuilder.GetTV();
}

public RawMateria GiveMePC()
{
pcbuilder.SetName();
pcbuilder.SetPrice();
return pcbuilder.GetPC();
}
}

class Client
{
public static void Main()
{
Director d = new Director();
d.GiveMePC().Show();
d.GiveMeTV().Show();
Console.Read();
}
}
}

设计模式---建造者(Builder)模式,有关系图