Decoration mode in Design Mode

Source: Internet
Author: User

Decoration mode in Design Mode

Definition: to dynamically add some additional responsibilities to an object, the decoration mode is more flexible than the subclass generation.

Structure:


<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + yr7A/placement = "brush: java;"> public abstract class Component {public abstract void operation ();} public class ConcreteComponent extends Component {@ Overridepublic void operation () {// TODO Auto-generated method stubSystem. out. println ("specific object operations! ") ;}} Public class ConcreteDecoratorB extends Decorator {@ Overridepublic void operation () {// TODO Auto-generated method stubsuper. operation (); System. out. println ("actions on decoration object B");} private void AddedBehavior () {System. out. println ("B operations") ;}} public class ConcreteDecoratorA extends Decorator {private String addedState; @ Overridepublic void operation () {// TODO Auto-generated method stubsuper. operation (); addedState = "New State"; System. out. println ("actions on decoration object ");}}The client code is as follows:

public class Client {public static void main(String[] args) {ConcreteComponent c = new ConcreteComponent();ConcreteDecoratorA d1 = new ConcreteDecoratorA();ConcreteDecoratorB d2 = new ConcreteDecoratorB();d1.setComponent(c);d2.setComponent(d1);d2.operation();//d1.setComponent(d2);//d1.operation();}}
The running result is as follows:

Specific Object operations!
Operations of decoration object
Operations on decoration object B


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.