实例说明:某公司欲开发一款儿童玩具汽车,为了更好地吸引小朋友的注意力,该玩具汽车在移动过程中伴随着灯光闪烁和声音提示。在该公司以往的产品中已经实现了控制灯光闪烁(例如警灯闪烁)和声音提示(例如警笛音效)的程序,为了重用先前的代码并且使得汽车控制软件具有更好的灵活性和扩展性。
1)代码:
PoliceSound类:
public class PoliceSound{
public void alarmSound() {
System.out.println("发出警笛声音!");
}
}
PoliceLamp类:
public class PoliceLamp{
public void alarmLamp(){
System.out.println("呈现警灯闪烁!");
}
}
CarController类:
public abstract class CarController{
public void move(){
System.out.println("玩具汽车移动!");
}
public abstract void phonate();//发出声音
public abstract void twinkle();//灯光闪烁
}
PoliceCarAdapter类:
public class PoliceCarAdapter extends CarController{
private PoliceSound sound;//定义适配者PoliceSound对象
private PoliceLamp lamp;//定义适配者PoliceLamp对象
public PoliceCarAdapter(){
sound = new PoliceSound();
lamp = new PoliceLamp();
}
//发出警笛声音
public void phonate(){
sound.alarmSound();//调用适配者类PoliceSound的方法
}
//呈现警灯闪烁
public void twinkle(){
lamp.alarmLamp();//调用适配者类PoliceLamp的方法
}
}
Client类:
public class Client{
public static void main(String args[]){
PoliceSound sound;
PoliceLamp lamp;
CarController car;
sound = new PoliceSound();
lamp = new PoliceLamp();
car = new PoliceCarAdapter();
car.move();
car.phonate();
car.twinkle();
}
}
运行结果:
实例说明:假如李先生用1个红色小袋子装了2包婺源特产(单价7.9元)、1张婺源地图(单价9.9元);用1个白色小袋子装了2包韶关香菇(单价68元)和3包韶关红茶(单价180元);用1个中袋子装了前面的红色小袋子和1个景德镇瓷器(单价380元);用1个大袋子装了前面的中袋子、白色小袋子和1双李宁牌运动鞋(单价198元),现在要求编程显示李先生放在大袋子中的所以商品信息并计算要支付的总价。本实例可按安全组合模式设计。
1)代码:
ShoppingTest类
package com.sise.work2;
import java.util.ArrayList;
public class ShoppingTest
{
public static void main(String[] args)
{
float s=0;
Bags BigBag,mediumBag,smallRedBag,smallWhiteBag;
Goods sp;
BigBag=new Bags("大袋子");
mediumBag=new Bags("中袋子");
smallRedBag=new Bags("红色小袋子");
smallWhiteBag=new Bags("白色小袋子");
sp=new Goods("婺源特产",2,7.9f);
smallRedBag.add(sp);
sp=new Goods("婺源地图",1,9.9f);
smallRedBag.add(sp);
sp=new Goods("韶关香菇",2,68);
smallWhiteBag.add(sp);
sp=new Goods("韶关红茶",3,180);
smallWhiteBag.add(sp);
sp=new Goods("景德镇瓷器",1,380);
mediumBag.add(sp);
mediumBag.add(smallRedBag);
sp=new Goods("李宁牌运动鞋",1,198);
BigBag.add(sp);
BigBag.add(smallWhiteBag);
BigBag.add(mediumBag);
System.out.println("您选购的商品有:");
BigBag.show();
s=BigBag.calculation();
System.out.println("要支付的总价是:"+s+"元");
}
}
//抽象构件:物品
interface Articles
{
public float calculation(); //计算
public void show();
}
//树叶构件:商品
class Goods implements Articles
{
private String name; //名字
private int quantity; //数量
private float unitPrice; //单价
public Goods(String name,int quantity,float unitPrice)
{
this.name=name;
this.quantity=quantity;
this.unitPrice=unitPrice;
}
public float calculation()
{
return quantity*unitPrice;
}
public void show()
{
System.out.println(name+"(数量:"+quantity+",单价:"+unitPrice+"元,合计:"+calculation()+"元)");
}
}
//树枝构件:袋子
class Bags implements Articles
{
private String name; //名字
private ArrayList bags=new ArrayList();
public Bags(String name)
{
this.name=name;
}
public void add(Articles c)
{
bags.add(c);
}
public void remove(Articles c)
{
bags.remove(c);
}
public Articles getChild(int i)
{
return bags.get(i);
}
public float calculation()
{
float s=0;
for(Object obj:bags)
{
s+=((Articles)obj).calculation();
}
return s;
}
public void show()
{
for(Object obj:bags)
{
((Articles)obj).show();
}
}
}
2)运行结果:



