GoF 23个经典的设计模式8--结构模式之 Composite组合模式(未完代续)

来源:互联网 发布:php 子类使用父类属性 编辑:程序博客网 时间:2024/04/25 18:22
 Composite组合模式:将对象以树的结构组合到一个总的层次中。组合模式让客户端以统一的格式应用独立的对象和组合体。
Compose objects into tree structures to represent whole-part hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly. [GoF, p163]

其实组合模式可以说是树型模式,一个节点算是一个对象。该节点可以再有和自己相同的子节点。

gof8.

这又有点是低归的意思,看上去是自己拥有自己。


    我们来组装一个PC把,首先这个PC由统一定义的接口的部件组成,所以每个部件的接口都是一样的,客户可以不用管什么部件,只要往上装就是了。

先看客户端:
package org.benewu.patterns.composite;

public class Client {
    
    
public static void main(String[] args) {
        
        PCComposite myPC 
= new PCComposite();
        myPC.setName(
"myPC");
        
        
//装入主板
        PCComposite mainBroad = new PCComposite();
        mainBroad.setPrice(
800);
        mainBroad.addPart(myPC);
        mainBroad.setName(
"mainBroad");
        
        
//装入硬盘
        PCComposite hardDesc = new PCComposite();
        hardDesc.setPrice(
1600);
        hardDesc.addPart(mainBroad);
        hardDesc.setName(
"hardDesc");
        
        
//装入CPU
        PCComposite cpu = new PCComposite();
        cpu.setPrice(
2000);
        cpu.addPart(mainBroad);
        cpu.setName(
"cpu");
        
        
//装入USB管理器
        PCComposite usbComposite = new PCComposite();
        usbComposite.setPrice(
100);
        usbComposite.addPart(mainBroad);
        usbComposite.setName(
"usbComposite");
        
        
//装入闪存
        PCComposite flashDesc = new PCComposite();
        flashDesc.setPrice(
200);
        flashDesc.addPart(usbComposite);
        flashDesc.setName(
"flashDesc");
        
        
//装入视频头
        PCComposite videoCam = new PCComposite();
        videoCam.setPrice(
300);
        videoCam.addPart(usbComposite);
        mainBroad.setName(
"mainBroad");
        
        
//装入新的视频头
        PCComposite newVideoCam = new PCComposite();
        newVideoCam.setPrice(
350);
        newVideoCam.addPart(usbComposite);
        newVideoCam.setName(
"newVideoCam");
        
        
//打印总价格 
        System.out.println(
"myPC price is :"+myPC.getTotalPrice());
        
        
//卸载视频头
        videoCam.removePart(usbComposite);
        System.out.println("myPC price is : +"myPC.getTotalPrice());
        
        
//打印全部部件
        myPC.printAll();
        
    }

    
}


    代码看上去很多,其实只作了两件事情,一,装入部件,二,察看部件内容。

    再看部件的定义,首先定义通用的元素接口:
package org.benewu.patterns.composite;

public interface Elements {

    
public void add(Elements e);
    
public void remove(Elements e);
}


    然后定义电脑部件的接口,它有id,name,price等属性
package org.benewu.patterns.composite;

import java.util.ArrayList;
import java.util.List;

public abstract class Component implements Elements {

    
private String partId;

    
private Integer price;

    
private String name;

    
private Component parent;

    
public void removePart(String id) {

        
for (Component component : components) {
            
if (id.equals(component.getPartId())) {
                remove(component);
            }

        }

    }


    
public void add(Elements e) {
        components.add((Component) e);
    }


    
public void remove(Elements e) {
        components.remove(e);
    }


    
public Integer getPrice() {
        
return price;
    }


    
public Integer getTotalPrice() {
        Integer totalPrice 
= null;
        
if (price == null{
            totalPrice 
= new Integer(0);
        }
 else {
            totalPrice 
= price;

        }


        
for (Component component : components) {
            totalPrice 
+= component.getTotalPrice();
        }

        
return totalPrice;
    }


    
protected List<Component> components = new ArrayList<Component>();

    
public void setPrice(Integer price) {

        
this.price = price;
    }


    
public String getPartId() {
        
return partId;
    }


    
public void setPartId(String partId) {
        
this.partId = partId;
    }


    
public List<Component> getComponents() {
        
return components;
    }


    
public abstract void printAll();

    
public String getName() {
        
return name;
    }


    
public void parentPrint() {
        
if (getParent() != null{
            System.out.print(
"--");
            getParent().parentPrint();
        }
 else {
            System.out.print(
"-");
        }

    }


    
public void setName(String name) {
        
this.name = name;
    }


    
public Component getParent() {
        
return parent;
    }


    
public void setParent(Component parent) {
        
this.parent = parent;
    }

}



    最后实现电脑部件:
package org.benewu.patterns.composite;

import java.util.List;

public class PCComposite extends Component {


    
public void addPart(PCComposite part){
        part.add(
this);
        
this.setParent(part);
    }

    
    
public void setPrice(Integer price) {
    
        
super.setPrice(price);
    }


    
public void removePart(PCComposite composite) {

        composite.remove(
this);
    }

    
    
    
public void printAll(){
        parentPrint();
        System.out.print(getName());
        
if(getPrice()!= null){
            System.out.println(
" : " +getPrice());
        }
else{
            System.out.println();
        }

        List
<Component> comps = getComponents();
        
if(comps != null && comps.size() >0){
            
for (Component component : comps) {
                component.printAll();
            }

        }

    }

}



    组合模式用到迭代方法,可能粗看起来比较复杂。



参考:http://home.earthlink.net/~huston2/dp/patterns.html:


原创粉丝点击