java-design pattern (structural type)-"Decorative mode"

Source: Internet
Author: User
Tags getcolor

1. Illustrations

2. Usage Scenarios

When a child class is decorated or a subclass needs too much decoration, an abstract class can be carved out of the abstract parent class,

The subclass of the abstract adornment class is decorated with sub-classes. For example, when spraying paint for automobiles.

(http://itlab.idcquan.com/Java/special/patterns/Index.html)

3. Code implementation

Create a new abstract car parent class:

Abstract parent Auto Public abstract class Car {/* * Abstract Parent Car basic information: * Production address, color, speed. * Car name is determined by subclass name, no need to define * *    /public String getaddress () {return address;} public void setaddress (String address) {this.address = address;} public float GetSpeed () {return speed;} public void Setspeed (float speed) {this.speed = speed;} private string Color;public string GetColor () {return Color;} public void SetColor (String color) {color = color;} Private String address;    private float speed;    public abstract  void Printcolor ();}

Then create a new Audi car sub-category:

Public class Audi extends car {     /* * Automotive Sub Audi     * In the construction of Audi car configuration related information     * */public Audi () {Super.setcolor ("white");} @Overridepublic void Printcolor () {//TODO auto-generated method StubSystem.out.println ("Audi Car Color:" +super.getcolor ());}}

Then create a new decorator parent class:

Public abstract class Decorate_car extends car  {    /     * * Abstract decorate two actions of the parent class:     * 1. Car entry [Setcar method]     * 2. Get information about the factory car [C.printcolor () method]     * */protected  car c=null;//loading factory public void Setcar (car c) {this.c=c;} Get information about the factory vehicle @Overridepublic void Printcolor () {//Learn the color C.printcolor () of the Factory car    ;}}

Then new decorator subclass: Red Decorator class:

public class Decorate_red_car extends decorate_car{     /* * Corresponding decoration of the car     * */@Overridepublic void Printcolor () { TODO auto-generated Method Stubsuper.printcolor (); This.c.setcolor (": Red");        SYSTEM.OUT.PRINTLN ("Red decoration factory on Audi car spray red Paint:" +this.c.getcolor ()); }}

Then new decorator subclass: Blue Decorator class:

public class Decorate_blue_car extends decorate_car{     /* * Corresponding decoration of the car     * */@Overridepublic void Printcolor () {//To Do auto-generated method Stubsuper.printcolor (); This.c.setcolor (": Blue");    SYSTEM.OUT.PRINTLN ("Blue Decoration factory on Audi car spray blue Paint:" +this.c.getcolor ()); }}

Test:

public class Test {public static void main (string[] args) {//TODO auto-generated method stub        Audi audi=new Audi (); 
   decorate_blue_car b_car=new Decorate_blue_car ();        Decorate_red_car  r_car=new Decorate_red_car ();        Audi car into the factory        B_car.setcar (Audi);        Will decorate Audi's blue decorative sub-category into the plant        R_car.setcar (b_car);        Print color        r_car.printcolor ();}}

Operation Result:

Audi car color: white blue decoration factory on Audi car Spray blue Paint:: Blue red decoration factory on Audi car Spray red paint:: Red

  

  

java-design pattern (structural type)-"Decorative mode"

Related Article

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.