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();
}
}
java 设计模式学习笔记八 composite组合模式
最新推荐文章于 2024-09-02 20:14:19 发布