- 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)模式,有关系图
最新推荐文章于 2019-07-16 22:54:31 发布