博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
设计模式--Composite
阅读量:7032 次
发布时间:2019-06-28

本文共 2502 字,大约阅读时间需要 8 分钟。

hot3.png

结构型模式3:

合成模式:合成模式将对象组织到树结构中,可以用来描述整体与部分的关系。合成模式就是一个处理对象的树结构的模式。合成模式把部分与整体的关系用树结构表示出来。合成模式使得客户端把一个个单独的成分对象和由他们复合而成的合成对象同等看待。

追MM比喻:COMPOSITE―Mary今天过生日。“我过生日,你要送我一件礼物。”“嗯,好吧,去商店,你自己挑。”“这件T恤挺漂亮,买,这条裙子好看,买,这个包也不错,买。”“喂,买了三件了呀,我只答应送一件礼物的哦。”“什么呀,T恤加裙子加包包,正好配成一套呀,小姐,麻烦你包起来。”“……”,MM都会用Composite模式了,你会了没有?

将“客户代码与复杂的对象容器结构”解耦是Composite模式的核心思想。解耦之后,客户代码将与纯粹的抽象接口(而非对象容器复杂内部实现结构)发生依赖关系,从而更能“应对变化”。

r_composite1.jpg

首先定义一个接口或抽象类Component:

package composite;

None.gifimport java.util.Iterator;
ExpandedBlockStart.gif
public abstract class Equipment{
InBlock.gif    
//名称
InBlock.gif
    private String name;
InBlock.gif    
//价格
InBlock.gif
    public abstract double netPrice();
InBlock.gif    
//增加一个部件
ExpandedSubBlockStart.gif
    public boolean add(Equipment equ){
InBlock.gif        
return false;
ExpandedSubBlockEnd.gif    }
InBlock.gif    
//删除一个部件
ExpandedSubBlockStart.gif
    public boolean remove(Equipment equ){
InBlock.gif        
return false;
ExpandedSubBlockEnd.gif    }
InBlock.gif    
//迭代指针
ExpandedSubBlockStart.gif
    public Iterator iter(){
InBlock.gif        
return null;
ExpandedSubBlockEnd.gif    }
InBlock.gif    
//构造方法
ExpandedSubBlockStart.gif
    public Equipment(String name){
InBlock.gif        
this.name=name;
ExpandedSubBlockEnd.gif    }
ExpandedBlockEnd.gif}

Disk类Leaf角色:

package composite;

ExpandedBlockStart.gifpublic class Disk extends Equipment{
InBlock.gif    
//构造方法
ExpandedSubBlockStart.gif
    public Disk(String name){
InBlock.gif        
super(name);
ExpandedSubBlockEnd.gif    }
InBlock.gif    
//定价
ExpandedSubBlockStart.gif
    public double netPrice(){
InBlock.gif        
return 1.2;
ExpandedSubBlockEnd.gif    }
ExpandedBlockEnd.gif}

 CompositeEquipment 类Composite角色:

   package composite;

None.gifimport java.util.*;
ExpandedBlockStart.gif
public abstract class CompositeEquipment  extends Equipment{
InBlock.gif    
private List equipment=new ArrayList();
ExpandedSubBlockStart.gif    
public CompositeEquipment(String name){
InBlock.gif        
super(name);
ExpandedSubBlockEnd.gif    }
ExpandedSubBlockStart.gif    
public boolean add(Equipment equ){
InBlock.gif        equipment.add(equ);
InBlock.gif        
return true;
ExpandedSubBlockEnd.gif    }
ExpandedSubBlockStart.gif    
public boolean remove(Equipment equ){
InBlock.gif        equipment.remove(equ);
InBlock.gif        
return true;
ExpandedSubBlockEnd.gif    }
ExpandedSubBlockStart.gif    
public Iterator iter(){
InBlock.gif        
return equipment.iterator();
ExpandedSubBlockEnd.gif    }
InBlock.gif    
ExpandedSubBlockStart.gif    
public double netPrice(){
InBlock.gif        
double netprice=0;
InBlock.gif        Iterator it
=equipment.iterator();
ExpandedSubBlockStart.gif        
while(it.hasNext()){
InBlock.gif            netprice
+=((Equipment)it.next()).netPrice();
ExpandedSubBlockEnd.gif        }
InBlock.gif        
return netprice;
ExpandedSubBlockEnd.gif    }
InBlock.gif    
ExpandedBlockEnd.gif}
CompositeEquipment的两个具体类:

   package composite;

ExpandedBlockStart.gifpublic class Chassis extends CompositeEquipment{
ExpandedSubBlockStart.gif    
public Chassis(String name){
InBlock.gif        
super(name);
ExpandedSubBlockEnd.gif    }
InBlock.gif    
ExpandedSubBlockStart.gif    
public double netPrice(){
InBlock.gif        
return 3.0+super.netPrice();
ExpandedSubBlockEnd.gif    }
ExpandedBlockEnd.gif}

   package composite;

ExpandedBlockStart.gifpublic class Cabinet extends CompositeEquipment{
ExpandedSubBlockStart.gif    
public Cabinet(String name){
InBlock.gif        
super(name);
ExpandedSubBlockEnd.gif    }
InBlock.gif    
ExpandedSubBlockStart.gif    
public double netPrice(){
InBlock.gif        
return 3.0+super.netPrice();
ExpandedSubBlockEnd.gif    }
InBlock.gif    
ExpandedBlockEnd.gif}

运行类:

package composite;

ExpandedBlockStart.gifpublic class Composite{
ExpandedSubBlockStart.gif    
public static void main(String[] args){
InBlock.gif        Cabinet cabinet
=new Cabinet("Tomer");
InBlock.gif        Chassis chassis
=new Chassis("PC chassis");
InBlock.gif        
InBlock.gif        cabinet.add(chassis);
InBlock.gif        chassis.add(
new Disk("120Gb"));
InBlock.gif        
InBlock.gif        System.out.println(cabinet.netPrice());
ExpandedSubBlockEnd.gif    }
ExpandedBlockEnd.gif}

输出:7.2    Press any key to continue...

Composite好处:

1.使客户端调用简单,客户端可以一致的使用组合结构或其中单个对象,用户就不必关系自己处理的是单个对象还是整个组合结构,这就简化了客户端代码。

2.更容易在组合体内加入对象部件. 客户端不必因为加入了新的对象部件而更改代码。

如何使用?

首先定义一个接口或抽象类,这是设计模式通用方式了,其他设计模式对接口内部定义限制不多,Composite却有个规定,那就是要在接口内部定义一个用于访问和管理Composite组合体的对象们(或称部件Component).

转载于:https://my.oschina.net/lgmcolin/blog/84069

你可能感兴趣的文章
Electron + Antd + Mobx 环境搭建
查看>>
我从来不理解JavaScript闭包,直到有人这样向我解释它...
查看>>
在CentOS7上安装RabbitMQ
查看>>
作为大众熟知的电商应用,京东如何构建风控体系架构?
查看>>
js运行机制及异步编程(二)
查看>>
typescript文档化工具——Typedoc
查看>>
JS数据结构0x004:链表
查看>>
以太坊钱包开发系列1 - 创建钱包账号
查看>>
社交系统 ThinkSNS+ V2.1.1 更新播报
查看>>
理解CPU分支预测,提高代码效率
查看>>
javascript调试接口
查看>>
Python基础系列:初识python引用计数与弱引用
查看>>
javascript继承方法以及优缺点
查看>>
tab 切换下划线跟随实现
查看>>
20+个很棒的Android开源项目
查看>>
跨域、vue双向绑定相关面试题
查看>>
Web Components(一)入门
查看>>
mpvue打包没有app.json等配置文件的解决方法
查看>>
树莓派配置swoole环境
查看>>
JavaScript 工作原理之十二-网络层探秘及如何提高其性能和安全性
查看>>