`
pouyang
  • 浏览: 313410 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Design Patterns 结构模式 之 Composite 模式

 
阅读更多
Design Patterns  结构模式 之 Composite 模式
组合模式:将对象以树形结构组织起来,以达成“部分-整体” 的层次结构,使得客户端对单个对象和组合对象的使用具有一致性.
1),安全式

2),透明式

组合(Composite)模式主要角色:
1.Component角色
2.Leaf角色
3.Composite角色
/**  
 * 组件抽象角色  
 */  
public interface Component {   
       
    public void run();   
  
}  

/**  
 * 叶子角色  
 */  
public class Leaf implements Component{   
       
    public void run(){   
        System.out.println(" run invoked...");   
    }   
}
 

//组合角色 
实现组件角色接口,并且添加,删除的也是组件角色 
Composite本来可以再包含Leaf和Composite的。而 Leaf和Composite 又都属于 Component,所以增加,删除 的是Component 
组合模式主要分为2种 

1 透明式 

    添加Component对象的操作定义在Component角色中,这样的话不仅Composite需要实现这些方法,Leaf也需要实现这些方法, 
    而这些方法对于Leaf来说没有任何意义,不过将系统实现统一起来了,因此对用户来说透明(用户无需区分Composite还是Leaf) 
    ,因为这些角色中都具备这些方法。 


2 安全式 


  添加Component对象的操作定义在Composite角色中,这样的话Leaf就无需实现这些方法(因为Leaf本身根本不需要实现这些方法)
  
public class Composite  implements Component{   
      
    List<Component> list = new ArrayList<Component>();   
       
    //添加   
    public void addComponent(Component component){   
        list.add(component);   
    }   
    //删除   
    public void removeComponent(Component component){   
        list.remove(component);   
    }   
       
    //get   
    public List<Component> getAll(){   
        return list;   
    }  
    public void run(){   
        for(Component component : list){   
            component.run();   
        }   
    }   
}  



public class Client {   
  
    public static void main(String[] args){   
        Leaf leaf1 = new Leaf();   
        Leaf leaf2 = new Leaf();   
        Leaf leaf3 = new Leaf();   
           
        Composite composite = new Composite();   
           
        composite.addComponent(leaf1);   
        composite.addComponent(leaf2);   
           
        Composite composite1 = new Composite();   
        composite1.addComponent(composite);   
        composite1.addComponent(leaf3);   
           
        composite1.run();   
           
    }   
}
 



分享到:
评论

相关推荐

    Design.Patterns.Explained.Simply

    We've tried hard to avoid both of these categories with Design Patterns Explained Simply. This book is fast and simple way to get the idea behind each of the 29 popular design patterns. The book is ...

    Apress.Pro.Design.Patterns.in.Swift

    Pro Design Patterns in Swift shows you how to harness the power and flexibility of Swift to apply the most important and enduring design patterns to your applications, taking your development ...

    Head First Design Patterns 英文版 Head First设计模式

    《Head First Design Patterns》共有14章,每章都介绍了几个设计模式,完整地涵盖了四人组版本全部23个设计模式。前言先介绍《Head First Design Patterns》的用法;第1章到第11章陆续介绍的设计模式为Strategy、...

    《Java Design Patterns》高清完整英文PDF版

    Learn how to implement design patterns in Java: each pattern in Java Design Patterns is a complete implementation and the output is generated using Eclipse, making the code accessible to all....

    Beginning SOLID Principles and Design Patterns for ASP.NET Developers.pdf

    Beginning SOLID Principles and Design Patterns for ASP.NET Developers ■Chapter 1: Overview of SOLID Principles and Design Patterns ■Chapter 2: SOLID Principles ■Chapter 3: Creational Patterns: ...

    Head First Design Patterns

    第1章到第11章陆续介绍的设计模式为Strategy、Observer、Decorator、Abstract Factory、Factory Method、Singleton、Command、Adapter、Facade、Templat Method、Iterator、Composite、 State、Proxy。最后三章比较...

    Head First Design Patterns 高清英文版

    第1章到第11章陆续介绍的设计模式为Strategy、Observer、Decorator、Abstract Factory、Factory Method、Singleton,Command、Adapter、Facade、TemplateMethod、Iterator、Composite、State、Proxy。最后三章比较...

    Design Patterns Elements of Reusable Object-Oriented Software

    • How Design Patterns Solve Design Problems • How to Select a Design Pattern • How to Use a Design Pattern A Case Study: Designing a Document Editor • Design Problems • Document Structure ...

    Head First Design Patterns(英文,无水印,完整版)

    第1章到第11章陆续介绍的设计模式为Strategy、Observer、Decorator、Abstract Factory、Factory Method、Singleton、Command、Adapter、Facade、Templat Method、Iterator、Composite、State、Proxy。*后三章比较...

    Packt.Go.Design.Patterns.2017

    Structural Patterns - Composite, Adapter, and Bridge Design Patterns Chapter 4. Structural Patterns - Proxy, Facade, Decorator, and Flyweight Design Patterns Chapter 5. Behavioral Patterns - Strategy...

    C++设计模式(Design Pattern)范例源代码

    23种设计模式(Design Pattern)的C++实现范例,包括下面列出的各种模式,代码包含较详细注释。另外附上“设计模式迷你手册.chm”供参考。 注:项目在 VS2008 下使用。 创建型: 抽象工厂模式(Abstract Factory) 生成...

    design_patterns_in_typescript:TypeScript中的设计模式实现

    结构模式 行为模式 编译项目 $ git clone https://github.com/torokmark/design_patterns_in_typescript.git $ cd design_patterns_in_typescript $ tsc 根目录中有一个tsconfig.json文件,负责编译器选项。 设置后...

    设计模式(Design.Patterns.CHN)

    4.3 Composite(组成)—对象结构型 模式 107 4.4 Decorator(装饰)—对象结构型 模式 115 4.5 FACADE(外观)—对象结构型 模式 121 4.6 Flyweight(享元)—对象结构型 模式 128 4.7 Proxy(代理)—对象结构型 ...

    [Java设计模式(第2版)(Design.Patterns.in.Java).John.Metsker

    《java设计模式(第2版)》通过一个完整的java项目对经典著作design patterns一书介绍的23种设计模式进行了深入分析与讲解,实践性强,却又不失对模式本质的探讨。本书创造性地将这些模式分为5大类别,以充分展现各个...

    java餐饮管理系统源码6-design_patterns:设计模式

    Patterns)、结构型模式(Structural Patterns)、 行为型模式(Behavioral Patterns) 创建型模式 工厂模式(Factory Pattern) 抽象工厂模式(Abstruct Factory Pattern) 单例模式(Singleton Pattern) 建造者...

    精通Objective-C设计模式 源代码

    Pro Objective-C Design Patterns for iOS will teach you those design patterns that have always been present at some level in your code, but were never recognized, acknowledged, or fully utilized....

    head first design patterns 英文原版高清

    第1章到第11章陆续介绍的设计模式为Strategy、Observer、Decorator、AbstractFactory、FactoryMethod、Singleton,Command、Adapter、Facade、TemplateMethod、Iterator、Composite、State、Proxy。最后三章比较特别...

    Java.Design.Patterns.1537192353

    Java design patterns with the Simplest real world examples which are easy to understand & remember as well. Table of Contents PREFACE ABOUT DESIGN PATTERNS SINGLETON PATTERN FACTORY PATTERN ABSTRACT ...

    design patterns elements of reusable object-oriented software

    ★第1章至第11章陆续介绍了设计模式:Strategy、Observer、Decorator、Abstract Factory、Factory Method、Singleton、Command、Adapter、Facade、TemplatMethod、Iterator、Composite、State、Proxy。 ★第12章介绍...

Global site tag (gtag.js) - Google Analytics