设计模式之组合模式
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace bridge
{
public abstract class Equipment
{
private String name;
public Equipment(String name)
{
this.name = name;
}
// 实体价格
public abstract double netPrice();
// 折扣价格
public abstract double discountPrice();
// 增加部件的方法
public virtual bool add(Equipment equipment)
{
return false;
}
// 移除部件方法
public bool remove(Equipment equipment)
{
return false;
}
// 组合体内访问各个部件的方法.
}
public class Disk : Equipment
{
// 硬盘实体价格
public static double diskNetPrice = 2.0;
// 硬盘折扣价格
public static double diskDiscountPrice = 1.0;
public Disk(String name)
: base(name)
{
;
}
public override double netPrice()
{
return diskNetPrice;
}
public override double discountPrice()
{
return diskDiscountPrice;
}
}
public abstract class CompositeEquipment : Equipment
{
private int i = 0;
// 定义一个List,用来保存组合体内的各个子对象.
private List<Equipment> equipment = new List<Equipment>();
public CompositeEquipment(String name)
: base(name)
{
}
public override bool add(Equipment equipment)
{
if (equipment is Disk && this is Chassis)
{
Console.WriteLine("在盘盒里面放了一个硬盘");
}
else if (equipment is Chassis && this is Cabinet)
{
Console.WriteLine("在柜子里面放了一个盘盒");
}
this.equipment.Add(equipment);
return true;
}
public override double netPrice()
{
double netPrice = 0;
if (this is Cabinet)
{
Console.WriteLine("我是在柜子的组合对象里面.柜子本身价格为:"
+ Cabinet.cabinetNetPrice);
}
else if (this is Chassis)
{
Console.WriteLine("我是在盘盒的组合对象里面.盘盒本身价格为:"
+ Chassis.chassisNetPrice);
}
foreach (Equipment iter in equipment)
{
if (iter is Chassis)
{
Console.WriteLine("在柜子里面发现一个盘盒,计算它的价格");
}
else if (iter is Disk)
{
Console.WriteLine("在盘盒里面发现一个硬盘,计算它的价格");
Console.WriteLine("硬盘本身价格为:" + Disk.diskNetPrice);
}
netPrice += iter.netPrice();
}
return netPrice;
}
public override double discountPrice()
{
double discountPrice = 0;
foreach (Equipment iter in equipment)
{
discountPrice += iter.discountPrice();
}
return discountPrice;
}
// 这里提供用于访问自己组合体内的部件方法。
// 上面Disk之所以没有,是因为Disk是个单独(Primitive)的元素.
// 重载Iterator方法
}
public class Cabinet : CompositeEquipment
{
public static double cabinetNetPrice = 10.0;
public static double cabinetDiscountPrice = 5.0;
public Cabinet(String name)
: base(name)
{
}
// 柜子本身价格以及放在柜子里面盒子的价格.
public override double netPrice()
{
return cabinetNetPrice + base.netPrice();
}
public override double discountPrice()
{
return cabinetDiscountPrice + base.discountPrice();
}
}
public class Chassis : CompositeEquipment
{
public static double chassisNetPrice = 2.0;
public static double chassisDiscountPrice = 1.0;
public Chassis(String name)
: base(name)
{
}
// 盒子的价格以及盒子里面硬盘价格.
public override double netPrice()
{
return chassisNetPrice + base.netPrice();
}
//
public override double discountPrice()
{
return chassisDiscountPrice + base.discountPrice();
}
}
public class Client
{
public static void Main(String[] args)
{
Cabinet cabinet = new Cabinet("柜子");
Chassis chassis = new Chassis("盘盒");
// 将盘盒装到箱子里
cabinet.add(chassis);
// 将硬盘装到盘盒里
chassis.add(new Disk("硬盘"));
// 查询整个柜子的实体价格
Console.WriteLine("整个柜子的实体价格(包括里面的盘盒和硬盘 =" + cabinet.netPrice());
// 查询整个柜子的折扣价格
Console.WriteLine("整个柜子的折扣价格(包括里面的盘盒和硬盘 =" + cabinet.discountPrice());
Console.ReadKey();
}
}
}