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

Design Patterns 结构模式 之 Decorator

 
阅读更多
Design Patterns  结构模式 之 Decorator


  Decorator模式是对目标类的封装,我们并不去直接实用目标类,而是对目标类进行封装,在真正使用目标类的方法之前或之后进行一些其他方面的处理,就像给目标增加了一些装饰一样,所以称为Decorator模式。

  首先定义一个接口:
public interface Work {

                   doSomething ();

         }


         实现类:

      
  public Class ConcreteWork implements Work {

                   public vodoSomething () {

                             // do something

                   }

         }

          Decorator类

        
 public  Class DecoratorWork implements Work  {

                   private Work work;

                   public DecoratorWork (Work work) {

                           this.work = work;

                   }

                   public void doSomething () { 

                             doWork();

                   }

                   private void doWork() { 

                              System.out.println("do orther work before");

                              work.doSomething;

                              System.out.println("do orther work after");

                    }

          }

          在实际需要使用ConcreteWork类doSomething()方法的功能时,不直接使用ConcreteWork 对象,而是使用new DecoratorWork().doSomething ()来实现具体的功能。

分享到:
评论

相关推荐

    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 ...

    .NET Design Patterns [Kindle Edition]

    After reading this book, you will be able to convincingly leverage these design patterns (factory pattern, builder pattern, prototype pattern, adapter pattern, facade pattern, decorator pattern, ...

    《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....

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

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

    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 ...

    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: ...

    Design Patterns in Modern C++--2018

    Implement structural patterns such as adapter, bridge, decorator, facade and more Work with the behavioral patterns such as chain of responsibility, command, iterator, mediator and more Apply ...

    Programming.in.the.Large.with.Design.Patterns

    It starts with a general introduction to all types of programming patterns and goes on to describe 10 of the most popular design patterns in detail: Singleton, Iterator, Adapter, Decorator, State, ...

    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

    With Head First Design Patterns, you'll avoid the embarrassment of thinking Decorator is something from the 'Trading Spaces' show. Best of all, in a way that won't put you to sleep! We think your ...

    Head First Design Patterns 英文原版

    With Head First Design Patterns, you’ll avoid the embarrassment of thinking Decorator is something from the “Trading Spaces” show. Best of all, in a way that won’t put you to sleep! We think your ...

    Head First Design Patterns 高清英文版

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

    Packt.Go.Design.Patterns.2017

    Structural Patterns - Proxy, Facade, Decorator, and Flyweight Design Patterns Chapter 5. Behavioral Patterns - Strategy, Chain of Responsibility, and Command Design Patterns Chapter 6. Behavioral ...

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

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

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

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

    C++.Design.Patterns.and.Derivatives.Pricing

    Design patterns are the cutting-edge paradigm for programming in object-oriented languages. Here they are discussed, for the first time in a book, in the context of implementing financial models in ...

    设计模式(Design.Patterns.CHN)

    2.4.3 Decorator 模式 32 2.5 支持多种视感标准 32 2.5.1 对象创建的抽象 32 2.5.2 工厂类和产品类 33 2.5.3 Abstract Factory模式 35 2.6 支持多种窗口系统 35 2.6.1 我们是否可以使用Abstract Factory 模式 35 ...

    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 ...

Global site tag (gtag.js) - Google Analytics