中科院云平台架构师,专注于数字化、智能化,技术方向:云、Linux内核、AI、MES/ERP/CRM/OA、物联网、传感器、大数据、ML、微服务。
分类: Java
2013-12-06 16:58:56
将物品(类)任意组合和,迅速获得组合后的数据。以下只是简单应用。
package hiya.composite;
public class CompositePatternTest {
static {
// chf@tsinghua.org.cn
System.out.println("这是hiya的组合模式程序!");
}
/**
* [メソッドの説明を書きましょう]
* @param args
*/
public static void main(String[] args) {
Cabinet cabinet=new Cabinet("Tower");
Chassis chassis=new Chassis("PC Chassis");
Disk disk = new Disk("10 GB");
//将PC Chassis装到Tower中 (将盘盒装到箱子里)
cabinet.add(chassis);
//将一个10GB的硬盘装到 PC Chassis (将硬盘装到盘盒里)
chassis.add(disk);
//调用 netPrice()方法;
// Disk's price.
System.out.println("disk.netPrice="+disk.netPrice());
System.out.println("disk.discountPrice="+disk.discountPrice());
// Disk+chassis's price.
System.out.println("chassis.netPrice="+chassis.netPrice());
System.out.println("chassis.discountPrice="+chassis.discountPrice());
// Disk+chassis+cabinet's price.
System.out.println("cabinet.netPrice="+cabinet.netPrice());
System.out.println("cabinet.discountPrice="+cabinet.discountPrice());
}
}
package hiya.composite;
import java.util.Iterator;
public abstract class Equipment {
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;
}
// 注意这里,这里就提供一种用于访问组合体类的部件方法。
public Iterator iter() {
return null;
}
public Equipment(final String name) {
this.name = name;
}
}
package hiya.composite;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class CompositeEquipment extends Equipment {
private int i = 0;
// 定义一个Vector 用来存放'儿子'
private List equipments = new ArrayList();
public CompositeEquipment(String name) {
super(name);
}
public boolean add(Equipment equipment) {
this.equipments.add(equipment);
return true;
}
@Override
public double discountPrice() {
double discountPrice = 0.;
Iterator iter = equipments.iterator();
while (iter.hasNext()) {
discountPrice += ((Equipment) iter.next()).discountPrice();
}
return discountPrice;
}
@Override
public double netPrice() {
double netPrice = 0.;
Iterator iter = equipments.iterator();
while (iter.hasNext())
netPrice += ((Equipment) iter.next()).netPrice();
return netPrice;
}
}
package hiya.composite;
public class Disk extends Equipment {
public Disk(String name) {
super(name);
}
// 定义Disk实价为1
public double netPrice() {
return 100;
}
// 定义了disk折扣价格是0.5 对折。
public double discountPrice() {
return 80;
}
}
package hiya.composite;
public class Chassis extends CompositeEquipment {
public Chassis(String name) {
super(name);
}
public double netPrice() {
return 15 + super.netPrice();
}
public double discountPrice() {
return 12 + super.discountPrice();
}
}
package hiya.composite;
public class Cabinet extends CompositeEquipment {
public Cabinet(String name) {
super(name);
}
public double netPrice() {
return 4 + super.netPrice();
}
public double discountPrice() {
return 3 + super.discountPrice();
}
}