Design pattern (ii): Decorative mode

Source: Internet
Author: User
Tags chop

2018 National Day Golden Week, just worth the National Day Golden Week holiday, I want to high-speed on a certain car mountain cha Haeju, fortunately, I chose the HSR, or you will definitely need to find project partners or in high-speed eating entrepreneurial people fried rice noodles.
National Day 7 days long vacation, the weather is so good, so-called sunny, so idyllic scenes represent, home you will find the road to get married team is coming and going, especially the lead wedding car, flowers and balloons to decorate is the atmosphere, luxury. Of course, the bride in the wedding car is also well-dressed, it is, the hair, double ring knot, Lily of the side skillfully decorate. White wedding dress, such as floating smoke, the beauty of new makeup than Flower Yan. Even the glory of Kings took advantage of this holiday. A set of skin preferential activities and new let "pesticide" chop hand "fat Da Rongrong" national treasure skin, is to attract you to chop your hands to decorate your favorite heroes. There are similar new house decoration, blog update skin and so on, these are decorated. What about the decorating mode of our program world?

Introduced

Decorative mode is a design pattern that dynamically adds new behavior to a class in the field of object-oriented programming. In terms of functionality, decorating mode is more flexible than generating subclasses, which can add functionality to an object rather than to the entire class.
Intent : To dynamically add some additional responsibilities and additions to an object.
Main Solution : In general, we often use inheritance in order to extend a class, because inheritance introduces static characteristics to the class, and as the expansion function increases, the subclass expands.
when to use : Extend a class without adding a lot of subclasses.
How to solve : Divide the specific function responsibilities and inherit the decorator mode.
Key Code : 1, the Component class acts as an abstract role, should not be implemented specifically. 2, the modification class references and inherits the Component class, the concrete extension class overrides the parent class method.

Realize

Take the wedding car as an example, define the car interface

public interface Car {    void drive();}

To create an entity class that implements the interface, BMW Germany is supposed to be the most famous brand of the wedding car.

public class BMW implements Car {    @Override    public void drive() {        System.out.println("宝马汽车,风驰电掣。");    }}

Create an abstract adornment class that implements the Car interface. We need a wedding car to pick up the bride

public abstract class WeddingCarDecorator implements Car{    private Car decoratorCar;    public WeddingCarDecorator(Car decoratorCar) {        this.decoratorCar = decoratorCar;    }    @Override    public void drive() {        decoratorCar.drive();    }}

Create a body decoration class that extends the Weddingcardecorator class-BMW wedding car.

public class BMWWeddingCar extends WeddingCarDecorator {    public BMWWeddingCar(Car decoratorCar) {        super(decoratorCar);    }    @Override    public void drive() {        beautify();        super.drive();    }    private void beautify() {        System.out.println("结婚婚车,铺上鲜花,系上气球。");    }}

Use Weddingcardecorator to decorate car objects.

public class DecoratorDemoMain {    public static void main(String[] args) {        System.out.println("------正常的德系宝马------");        BMW bmw = new BMW();        bmw.drive();        System.out.println("------作为婚车的德系宝马------");        BMWWeddingCar bmwWeddingCar = new BMWWeddingCar(bmw);        bmwWeddingCar.drive();    }}

Execute program, output result:

------正常的德系宝马------宝马汽车,风驰电掣。------作为婚车的德系宝马------结婚婚车,铺上鲜花,系上气球。宝马汽车,风驰电掣。
Summarize

Advantages : Decorative and decorative categories can be independent development, not mutual coupling, decoration mode is an alternative mode of inheritance, decoration mode can dynamically extend the function of an implementation class.
disadvantage : Multilayer decoration is more complicated.
usage Scenario : 1. Extend the functionality of a class. 2, dynamic increase function, dynamic revocation.
Caveats: Can be substituted for inheritance.

Design pattern (ii): Decorative mode

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.