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]
其实组合模式可以说是树型模式,一个节点算是一个对象。该节点可以再有和自己相同的子节点。
这又有点是低归的意思,看上去是自己拥有自己。
我们来组装一个PC把,首先这个PC由统一定义的接口的部件组成,所以每个部件的接口都是一样的,客户可以不用管什么部件,只要往上装就是了。
先看客户端:
代码看上去很多,其实只作了两件事情,一,装入部件,二,察看部件内容。
再看部件的定义,首先定义通用的元素接口:
然后定义电脑部件的接口,它有id,name,price等属性
最后实现电脑部件:
组合模式用到迭代方法,可能粗看起来比较复杂。
参考:http://home.earthlink.net/~huston2/dp/patterns.html:
Compose objects into tree structures to represent whole-part hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly. [GoF, p163]
其实组合模式可以说是树型模式,一个节点算是一个对象。该节点可以再有和自己相同的子节点。
这又有点是低归的意思,看上去是自己拥有自己。
我们来组装一个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();
}
}
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);
}
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;
}
}
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();
}
}
}
}
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:
- GoF 23个经典的设计模式8--结构模式之 Composite组合模式(未完代续)
- GoF 23个经典的设计模式6--结构模式之适配器模式(未完代续)
- GoF 23个经典的设计模式7--结构模式之Bridge桥接模式(未完代续)
- GoF 23个经典的设计模式02--创建模式之创建者模式(未完代续)
- GoF 23个经典的设计模式04--创建模式之原型模式(未完代续)
- GoF 23个经典的设计模式05--创建模式之单例模式(未完代续)
- GoF 23个经典的设计模式13--行为模式之 责任链模式(未完代续)
- GoF 23个经典的设计模式03--创建模式之工厂方法(未完代续)
- GoF 23个经典的设计模式01--创建模式之抽象工厂(未完代续)
- GOF 23设计模式之 组合模式(composite)
- GOF-设计模式之COMPOSITE(组合)
- GoF的23个经典设计模式
- GoF 的23个经典设计模式
- 结构型设计模式之组合模式(Composite Pattern)
- 【结构型模式】组合模式(Composite)之23种java设计模式
- 23设计模式之组合模式(Composite)
- 23设计模式之组合模式(Composite)
- 【设计模式】之组合模式(Composite)
- 开源代码生成器:SmartCode
- unbound method & bound method
- UIQ模拟器上网
- Window.Open详解
- Windows蓝屏代码含义速查
- GoF 23个经典的设计模式8--结构模式之 Composite组合模式(未完代续)
- Window For JavaScript
- StringTokenizer:字符串分隔解析类型
- 自己写的一个asp.net的生成曲线图的过程
- websphere社区版简介-基于geronimo
- Vector类的使用
- 将多次查询的数据填充到同一数据源
- java数据类型转换
- 如何学习设计模式初级篇