Design Pattern-modifier pattern (study notes)

Source: Internet
Author: User


First, seeDecorator mode definition:Dynamically attaches the responsibility to the object. If you want to expand your capabilities, the decorations provide more flexible alternatives than inheritance!

Let's take a look at the class diagram given in "big talk Design Patterns:


Actually,Decoration ModeThat is, the modifier object is treated as a "wrapper". In other words, the object to be decorated is passed as a parameter to the decoration object and then operated. (If you do not understand it, you want to correct it.) the following code is used to understand the class diagram:

This is the common interface between the decorator and the specific objects to be decorated:

public abstract  class Component {    abstract void Operation();}
Object
Public class ConcreteComponment extends Component {@ Override void Operation () {System. out. println ("actions on specific objects ");}}
Decorated with base classes
public class Decorator extends Component {    private Component component;    public void setComponent(Component component) {        this.component = component;    }    @Override    void Operation() {    if (component!=null){        component.Operation();    }    }}
Decorator
Public class ConcreateDecoratorA extends Decorator {private String addState; @ Override void Operation () {super. operation (); addState = "New state"; System. out. println ("ConcreateDecoratorA operations ");}}
Decorator B
Public class ConcreateDecoratorB extends Decorator {@ Override void Operation () {AddedBehavior (); super. Operation ();/**

The decorator Can add his/her own behavior before and after the behavior of the entrusted decorator to achieve a specific purpose.

**/System. out. println ("ConcreateDecoratorB operation");} public void AddedBehavior () {System. out. println ("AddedBehavior ");}}

Test class:
public class Main {    public static void main(String[] args){ConcreteComponment con=new ConcreteComponment();        ConcreateDecoratorA a=new ConcreateDecoratorA();        a.setComponent(con);        a.Operation();        ConcreateDecoratorB b=new ConcreateDecoratorB();        b.setComponent(con);        b.Operation();    }}

Test results:

Operation of a specific object ConcreateDecoratorA operation of AddedBehavior operations of a specific object ConcreateDecoratorB



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.