标签:
合成模式
package com.eekq.structure.composite.security;
/*
* 抽象构件,物品
* */
public interface IRes {
/**购物买单,示意性的商业方法*/
public void pay();
}package com.eekq.structure.composite.security;
public class SingleResImpl implements IRes {
/**物品名称*/
private String name;
/**价钱*/
private float money;
public SingleResImpl(String name, float money) {
this.name = name;
this.money = money;
}
public void pay() {
System.out.println("购买了一件物品["+getName()+"],价钱是[" + getMoney()+"]元");
}
public float getMoney() {
// TODO 自动生成方法存根
returnthis.money;
}
public String getName() {
// TODO 自动生成方法存根
return this.name;
}
/**重写equals*/
public boolean equals(Object obj){
SingleResImpl res = (SingleResImpl)obj;
return res.getName().equals(getName()) && res.getMoney()==getMoney();
}
}package com.eekq.structure.composite.security;
import java.util.Iterator;
import java.util.Vector;
/*
* 对多个物品的管理
* */
public class MultiResImpl implements IRes {
/**购物车*/
private Vector car = new Vector();
private static float totle = 0.0f;
public void pay() {
if(!car.isEmpty()){
System.out.println("名称 价格\n");
shopping();
System.out.println("\n总价:" + totle + "元");
}else{
System.out.println("您好,你没有购买任何物品,不用买单!");
}
}
public void shopping() {
if (car != null || !car.isEmpty()) {
Iterator it = car.iterator();
SingleResImpl res = null;
Object temp = null;// 临时对象
while (it.hasNext()) {
temp = it.next();
if (temp instanceof MultiResImpl) {
((MultiResImpl) temp).shopping();
} else {
res = (SingleResImpl) temp;
synchronized (this) {
totle += res.getMoney();
}
System.out.println(res.getName() + " " + res.getMoney()
+ "元");
}
}
}
}
/**加入新的物品*/
public void addRes(IRes res) {
car.add(res);
}
/**放回物品*/
public void removeRes(IRes res) {
car.remove(res);
}
}
package com.eekq.structure.composite.security;
public class Main {
/**
*@paramargs
*/
public static void main(String[] args) {
/**买支雪糕*/
IRes singleRes = new SingleResImpl("雪糕", 1.5f);
/**买单*/
singleRes.pay();
/**快过年了,我推了个购物车,多买点东西*/
IRes allRes = new MultiResImpl();
/**在一楼买的食物*/
IRes one = new MultiResImpl();
((MultiResImpl) allRes).addRes(one);//把一楼的东西装在购物车里
/**因为是安全方式的组合模式,因此不够透明,只能明确的向下转型,然后再加入购物车了*/
((MultiResImpl) one).addRes(new SingleResImpl("旺旺", 28.5f));
((MultiResImpl) one).addRes(new SingleResImpl("糖果", 38.0f));
((MultiResImpl) one).addRes(new SingleResImpl("可乐", 8.5f));
/**二楼去买的衣服和袜子*/
IRes two = new MultiResImpl();
((MultiResImpl) allRes).addRes(two);// 把二楼的东西装也装在购物车里
((MultiResImpl) two).addRes(new SingleResImpl("衣服", 130.5f));
((MultiResImpl) two).addRes(new SingleResImpl("袜子", 10f));
/**二楼再买了个手表,我放在bao里*/
IRes bao = new MultiResImpl();
((MultiResImpl) two).addRes(bao);//把购物小包装在二楼购物车里
((MultiResImpl) bao).addRes(new SingleResImpl("手表", 100f));
/**回到一楼,又买了苹果和梨*/
((MultiResImpl) one).addRes(new SingleResImpl("苹果", 10.0f));
((MultiResImpl) one).addRes(new SingleResImpl("梨", 3.0f));
/**在买单之前我把可乐退了,因为家里还有的嘛*/
((MultiResImpl) one).removeRes(new SingleResImpl("可乐", 8.5f));
/**在收银台一次性对购物车所有物品买单*/
allRes.pay();
}
}package com.eekq.structure.composite.clarity;
/*
* 抽象构件,物品
* */
public interface IRes {
/**购物买单,示意性的商业方法*/
public void pay();
/**加入新的物品*/
public void addRes(IRes res);
/**放回物品*/
public void removeRes(IRes res);
}package com.eekq.structure.composite.security;
public class SingleResImpl implements IRes {
/**物品名称*/
private String name;
/**价钱*/
private float money;
public SingleResImpl(String name, float money) {
this.name = name;
this.money = money;
}
public void pay() {
System.out.println("购买了一件物品["+getName()+"],价钱是[" + getMoney()+"]元");
}
public float getMoney() {
// TODO 自动生成方法存根
return this.money;
}
public String getName() {
// TODO 自动生成方法存根
return this.name;
}
/**重写equals*/
public boolean equals(Object obj){
SingleResImpl res = (SingleResImpl)obj;
return res.getName().equals(getName()) && res.getMoney()==getMoney();
}
}package com.eekq.structure.composite.clarity;
public class Main {
/**
*@paramargs
*/
public static void main(String[] args) {
/**买支雪糕*/
IRes singleRes = new SingleResImpl("雪糕", 1.5f);
/**买单*/
singleRes.pay();
/**快过年了,我推了个购物车,多买点东西*/
IRes allRes = new MultiResImpl();
/**在一楼买的食物*/
IRes one = new MultiResImpl();
allRes.addRes(one);// 把一楼的东西装在购物车里
/**因为是透明方式的组合模式,因此直接调用就是了*/
one.addRes(new SingleResImpl("旺旺", 28.5f));
one.addRes(new SingleResImpl("糖果", 38.0f));
one.addRes(new SingleResImpl("可乐", 8.5f));
/**二楼去买的衣服和袜子*/
IRes two = new MultiResImpl();
allRes.addRes(two);// 把二楼的东西装也装在购物车里
two.addRes(new SingleResImpl("衣服", 130.5f));
two.addRes(new SingleResImpl("袜子", 10f));
/**二楼再买了个手表,我放在bao里*/
IRes bao = new MultiResImpl();
two.addRes(bao);// 把购物小包装在二楼购物车里
bao.addRes(new SingleResImpl("手表", 100f));
/**回到一楼,又买了苹果和梨*/
one.addRes(new SingleResImpl("苹果", 10.0f));
one.addRes(new SingleResImpl("梨", 3.0f));
/**在买单之前我把可乐退了,因为家里还有的嘛*/
one.removeRes(new SingleResImpl("可乐", 8.5f));
/**在收银台一次性对购物车所有物品买单*/
allRes.pay();
}
}












版权声明:欢迎转载,希望在你转载的同时,添加原文地址,谢谢配合
标签:
原文地址:http://blog.csdn.net/u011225629/article/details/47699653