Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7096832
  • 博文数量: 703
  • 博客积分: 10821
  • 博客等级: 上将
  • 技术积分: 12042
  • 用 户 组: 普通用户
  • 注册时间: 2005-12-02 10:41
个人简介

中科院云平台架构师,专注于数字化、智能化,技术方向:云、Linux内核、AI、MES/ERP/CRM/OA、物联网、传感器、大数据、ML、微服务。

文章分类

全部博文(703)

分类: 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);

    }

    // Disk1

    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();

    }

}



执行结果:
这是hiya的组合模式程序!
disk.netPrice=100.0
disk.discountPrice=80.0
chassis.netPrice=115.0
chassis.discountPrice=92.0
cabinet.netPrice=119.0
cabinet.discountPrice=95.0
阅读(2254) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~