//
如下的类是框架部分,
//调度者,负责编排构造过程
class
Director
{
//抽象的构造器,可以由不同的实现,用于替换部件
public
ComputerBuilder
computerBuilder;
//构造方法,根据价格调用不同的构造器:
//
price > 5000调用NoteBookBuilder,否则调用PCBuilder
//并可以编排构造过程
public
void
Construct(Product
product,int
price)
{
if
(price > 5000) //represents
some local decision inside director
{
computerBuilder
= new
NoteBookBuilder();
}
else
{
computerBuilder
= new
PCBuilder();
}
computerBuilder._product
= product;
computerBuilder.BuildMonitor();
computerBuilder.BuildKeypad();
computerBuilder.BuildMouse();
}
}
//抽象的构造器,可以被不同的实现,进而替换部件
abstract
class
ComputerBuilder
{
protected
Product
product;
public
Product
_product
{
set
{ product = value;
}
get
{ return
product; }
}
abstract
public
void
BuildKeypad();
abstract
public
void
BuildMonitor();
abstract
public
void
BuildMouse();
}
//
如下的类是应用部分
//PCBuilde负责PC的构造
class
PCBuilder
: ComputerBuilder
{
//构建键盘
override
public
void
BuildKeypad()
{
//
Create some object here known to PCBuilder
Console.WriteLine("PCBuilder.BuildKeypad
called");
Keypad
keypad
= new
Keypad();
keypad.name = "Lenovo";
keypad.color
= "white";
product.keypad
= keypad;
}
//构建CRT显示器
override
public
void
BuildMonitor()
{
//
Create some object here known to PCBuilder
Console.WriteLine("PCBuilder.Build
...CRT called");
CRT
crt
= new
CRT();
crt.name = "冠捷";
crt.color
= "white";
product.monitor
= crt;
}
//构建鼠标
override
public
void
BuildMouse()
{
//
Create some object here known to PCBuilder
Console.WriteLine("PCBuilder.BuildMouse
called");
Mouse
mouse
= new
Mouse();
mouse.name = "Lenevo";
mouse.color
= "white";
product.mouse
= mouse;
}
}
//
如下的类是应用部分
//
负责NoteBoo的构造
class
NoteBookBuilder
: ComputerBuilder
{
//构造键盘
override
public
void
BuildKeypad()
{
//
Create some object here known to PCBuilder
Console.WriteLine("NoteBookBuilder.BuildKeypad
called");
Keypad
keypad
= new
Keypad();
keypad.name = "ThinkpadKeypad";
keypad.color
= "Black";
product.keypad
= keypad;
}
//构造LCD显示器
override
public
void
BuildMonitor()
{
//
Create some object here known to PCBuilder
Console.WriteLine("NoteBookBuilder.BuildLCD
called");
LCD
lcd
= new
LCD();
lcd.name = "Benq";
lcd.color
= "Black";
product.monitor
= lcd;
}
//构造鼠标
override
public
void
BuildMouse()
{
//
Create some object here known to PCBuilder
Console.WriteLine("NoteBookBuilder.BuildMouse
called");
Mouse
mouse
= new
Mouse();
mouse.name = "miniMouse";
mouse.color
= "black";
product.mouse
= mouse;
}
}
//键盘部件
class
Keypad
{
public
string
name;
public
string
color;
public
void
Press()
{
Console.WriteLine("Key
pad is pressed...");
}
}
//抽象的显示器
abstract
class
Monitor
{
public
string
name;
public
string
color;
abstract
public
void
Display();
}
//LCD显示器
class
LCD
: Monitor
{
public
override
void
Display()
{
Console.WriteLine("LCD
is display...");
}
}
//CRT显示器
class
CRT
:Monitor
{
public
override
void
Display()
{
Console.WriteLine("CRT
is display...");
}
}
//鼠标
class
Mouse
{
public
string
name;
public
string
color;
public
void
Move()
{
Console.WriteLine("Mouse
is moving...");
}
}
//产品,包括部件:keypad,monitor,mouse
class
Product
{
public
string
name;
public
Keypad
keypad;
public
Monitor
monitor;
public
Mouse
mouse;
//产品工作
public
void
Work()
{
keypad.Press();
monitor.Display();
mouse.Move();
}
}
///
<summary>
///
User 使用Director构造Product。
///
</summary>
public
class
User
{
public
static
void
Main(string[]
args)
{
Product
product
= new
Product();
product.name = "俎涛的电脑";
Console.WriteLine("请输入购买电脑的金额:");
int
price = Int16.Parse(Console.ReadLine());
Director
director
= new
Director();
director.Construct(product,
price);
Console.WriteLine("Keypad:{0}",product.keypad.name);
Console.WriteLine("LCD:{0}",
product.monitor.name);
Console.WriteLine("Mouse:{0}",
product.mouse.name);
product.Work();
}
}
} |