java 设计模式学习笔记八 composite组合模式

composite组合模式
将对象以树形式组织起来和chain of responsibility模式类似


好处:
可以一致使用组合结构和单个对象
不必关心组合体内是否加入新部件


示例代码:
/**
* 组合体接口
*
* @time 下午09:05:03
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
public abstract class Equipment {
@SuppressWarnings("unused")
private String name;


// 实际价格
public abstract double netPrice();


// 折扣价格
public abstract double discountPrice();


// 添加部件
public boolean add(Equipment equipment) {
return false;
}


// 删除部件
public boolean remove(Equipment equipment) {
return false;
}


// 访问组合体的方法
@SuppressWarnings("rawtypes")
public Iterator iter() {
return null;
}


public Equipment(String name) {
this.name = name;
}


}








/**
* 部件桌子
*
* @time 下午09:12:01
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
public class Disk extends Equipment {


/**
* 构造器
*
* @param name
*/
public Disk(String name) {
super(name);
}


@Override
public double netPrice() {
return 1;
}


@Override
public double discountPrice() {
return 0.5;
}
}




/**
* 组合体部件
*
* @time 下午09:13:41
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
@SuppressWarnings("rawtypes")
public class CompositeEquipment extends Equipment {
private int i = 0;
// 用来存放子控件
private List equipment = new ArrayList();


/**
* 构造器
*
* @param name
*/
public CompositeEquipment(String name) {
super(name);
}


@SuppressWarnings("unchecked")
@Override
public boolean add(Equipment equipment) {
this.equipment.add(equipment);
return true;
}


@Override
public double netPrice() {
double netPrice = 0;
Iterator iterator = equipment.iterator();
while (iterator.hasNext()) {
netPrice += ((Equipment) iterator.next()).netPrice


();
}
return netPrice;
}


@Override
public double discountPrice() {
double discountPrice = 0;
Iterator iterator = equipment.iterator();
while (iterator.hasNext()) {
discountPrice += ((Equipment) iterator.next


()).discountPrice();
}
return discountPrice;
}


// 访问本组合体内部件的方法


public Iterator iter() {
return equipment.iterator();
}


public boolean hasNext() {
return i < equipment.size();
}


public Object next() {
if (hasNext()) {
return equipment.get(i++);
} else {
throw new NoSuchElementException();
}
}
}






/**
* 框箱
*
* @time 下午09:31:14
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
public class Cabinet extends CompositeEquipment {


public Cabinet(String name) {
super(name);
}


@Override
public double netPrice() {
return 1 + super.netPrice();
}


@Override
public double discountPrice() {
return .5 + super.discountPrice();
}


}








/**
* 盘盒
*
* @time 下午09:30:18
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
public class Chassis extends CompositeEquipment {


public Chassis(String name) {
super(name);
}


@Override
public double netPrice() {
return 1 + super.netPrice();
}


@Override
public double discountPrice() {
return .5 + super.discountPrice();
}


}








/**
* 测试组合模式
*
* @time 下午09:33:44
* @author retacn yue
* @Email zhenhuayue@sina.com
*/
public class TestComposite {
public static void main(String[] args) {
Cabinet cabinet = new Cabinet("Tower");
Chassis chassis = new Chassis("PC Chassis");


// 将chassis装到tower中
cabinet.add(chassis);
cabinet.netPrice();
cabinet.discountPrice();
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值