[Original-tutorial-serialization] "android big talk Design Patterns"-structural patterns of design patterns Chapter 7: Decorative patterns see the parent of mm

Source: Internet
Author: User
<Big talk design model> description and copyright notice of this tutorial

Guoshi studio is a technical team dedicated to enterprise-level application development on the Android platform. It is committed to the best Android Application in China.ProgramDevelopment institutions provide the best Android enterprise application development training services.

Official contact information for Enterprise Training and Development Cooperation:

Tel: 18610086859

Email: hiheartfirst@gmail.com

QQ: 1740415547

Guoshi studio is better for you!

L this document references and uses free images and content on the Internet, and is released in a free and open manner, hoping to contribute to the mobile Internet and the smart phone age! This document can be reproduced at will, but cannot be used for profit.

L if you have any questions or suggestions about this document, go to the official blog

Http://www.cnblogs.com/guoshiandroid/ (with the following contact information), we will carefully refer to your suggestions and modify this document as needed to benefit more developers!

L The latest and complete content of "big talk design mode" will be regularly updated on the official blog of guoshi studio. Please visit the blog of guoshi studio.

Http://www.cnblogs.com/guoshiandroid/get more updates.

Decoration Mode SeeMmParent 

Decoration ModeUse Cases: 

"K brother, my wife said she wanted me to go back with her to meet her parent. She also said her father volunteered to have a few more drinks with me. What should I do ?" Gg anxiously said to his friend K, "Dizzy, wife? You have developed too fast, but congratulations, "K said with a tone of ridicule." But to see your parents, you must be careful. "" according to the practice, since the other party offered the invitation, and her father and adults both said they would like to have a few more drinks with your kid, this is not a problem; in fact, there is nothing, it is enough to prepare for it, we believe GG!" K continued to ridicule and said, "however, pay attention to the image:-o". "What is the specific image ?" Gg asked, "Are you asking me this? Who should I ask? I just figured it out for you. "I mean serious." GG was anxious. "It mainly depends on what impression you want to give to each other." K said seriously, "What is the impression?" Gg asked, "in general, the first thing is to let the other parents know that you love their daughters very much. Second, young people must be energetic, cheerful, and Sunny." Again: I want to give the other parents a very progressive impression:-o "K said with a serious face," Haha, K is K brother, and it is really a big boss ", "What should I wear?" Gg then asked, "Jeans must be worn. The blue jeans you usually wear are great. As for the coat, your white shirt is very good now, don't forget to keep your hair clean and your teeth white. Keep your silly smile as much as possible ^_^ ". gg's troublesome problem was solved by K, 3, 5, and 2. gg said excitedly: "Boss, I love it!"

Description of the decoration mode: 

The decorator mode is also called the wrapper mode. Objects can be extended in a way that is transparent to the client. This is a replacement solution for the inheritance relationship.

Attach additional responsibilities to an object dynamically.
Decorators provide a flexible alternative to subclassing for extending
Functionality.

Decorative ModeUMLFigure: 

The builder mode involves the following roles:

Abstract component role: an abstract interface is the parent interface of the decoration class and decoration class.

The concrete component role is the implementation class of the abstract component.

Decorator: contains a reference to a component and defines an interface consistent with the abstract component.

Concrete decorator role: the Implementation class of the abstract decoration role. Responsible for specific decoration.

The UML diagram of the decoration mode is as follows:

In-depth analysis of decorative patterns:

The decoration mode dynamically adds functions to objects in a way that is transparent to the client. It is a good alternative to inheritance.

Inheritance is a unique and abuse-prone method of reuse and extension of object-oriented languages. The inheritance relationship must first conform to the gratitude of the base class and sub-class in the classification sense, and the inherited sub-class must expand the attributes or behaviors of the base class. Inheritance makes it easier to modify or extend the base class. However, inheritance also has many shortcomings. First, inheritance destroys encapsulation, because inheritance exposes the implementation details of the base class to the subclass. Second, if the implementation of the base class changes, the subclass will follow. At this time, we have to change the behavior of the subclass to adapt to the changes of the base class. Finally, the implementation inherited from the base class is static and cannot be changed during runtime, which makes the corresponding system lack sufficient flexibility.

For many of the above reasons, we generally try not to use inheritance to add features to objects. At this time, the decoration mode is a better choice, because: first, the decoration mode is transparent to the client, and the client does not feel the original object or the decorated object at all. That is to say, the decoration mode is transparent to the client; second, the decorator and the decorated object share the same interface, and the decorator uses the decorated object to reference the decorated object, this allows the decoration object to be decorated with unlimited dynamic decoration objects. The last decoration object does not know whether the decorated object has been decorated. This makes it possible to face any decorated objects, decorator can use the same method for handling.

Analysis andCodeImplementation: 

In the above application scenarios, GG needs to be checked by the MM parents and consider how to dress up. First, GG itself is a specific component. Secondly, GG wear jeans is still GG, but it is decorated by jeans and becomes a cowboy. Finally, wear a shirt on the shirt again. This time, it was further decorated on the basis of a cowboy. At this time, a handsome man was born, but in essence, it is still the GG ^_^

The UML Model diagram is as follows:

 

Create a component interface:

PackageCom. diermeng. designpattern. decorator;

/*

* Component Interface

*/

Public InterfacePerson {

 

Public VoidShow ();

 

Public VoidNodecorator ();

}

 

Create a base class for the basic decorative abstract class:

PackageCom. diermeng. designpattern. decorator;

 

/*

* Basic decorative abstract class base class

*/

Public Abstract ClassPersondecoratorImplementsPerson {

PrivatePerson;

 

PublicPerson getperson (){

ReturnPerson;

}

 

Public VoidSetperson (person ){

This. Person = person;

}

 

PublicPersondecorator (person
Person ){

This. Person = person;

}

 

Public Abstract VoidShow ();

}

The specific person class here refers to the GG class:

PackageCom. diermeng. designpattern. decorator;

 

/*

* Specific person

*/

Public ClassGgImplementsPerson {

 

Public VoidNodecorator (){

System.Out. Println ("I Am Not dressed GG ");

}

 

Public VoidShow (){

This. Nodecorator ();

}

 

}

 

Blue Jeans Decoration:

PackageCom. diermeng. designpattern. decorator. impl;

 

ImportCom. diermeng. designpattern. decorator. person;

ImportCom. diermeng. designpattern. decorator. persondecorator;

/*

* Use blue jeans for decoration

*/

Public ClassJeansdecorator
ExtendsPersondecorator {

 

PublicJeansdecorator (person
Person ){

Super(Person );

}

 

Public VoidShow (){

This. Getperson (). Show ();

This. Jeansdecorator ();

}

 

Public VoidJeansdecorator (){

System.Out. Println ("I put on the Gg of the blue jeans, handsome more O (handsome _ cute) O Haha ~ ");

}

 

Public VoidNodecorator (){

 

}

 

}

White long shirt decoration class:

PackageCom. diermeng. designpattern. decorator. impl;

 

ImportCom. diermeng. designpattern. decorator. person;

ImportCom. diermeng. designpattern. decorator. persondecorator;

/*

* Decorated with a white shirt

*/

Public ClassShirtdecoratorExtendsPersondecorator {

 

PublicShirtdecorator (person
Person ){

Super(Person );

}

 

Public VoidShow (){

This. Getperson (). Show ();

This. Shirtdecorator ();

}

 

Public VoidShirtdecorator (){

System.Out. Println ("I'm a GG with a white shirt, very nice O (pretty _ cute) O Haha ~ ");

}

 

Public VoidNodecorator (){

 

}

}

Finally, we create a test client:

PackageCom. diermeng. designpattern. decorator. client;

 

ImportCom. diermeng. designpattern. decorator. GG;

ImportCom. diermeng. designpattern. decorator. person;

Import
Com. diermeng. designpattern. decorator. impl. jeansdecorator;

ImportCom. diermeng. designpattern. decorator. impl. shirtdecorator;

/*

* Decoration mode test Client

*/

Public ClassDecoratortest {

Public Static VoidMain (string [] ARGs ){

Person =NewGG ();

 

Person. Show ();

System.Out. Println ("-------------------------------------");

 

Person car =New
Jeansdecorator (person );

Swimcar. Show ();

System.Out. Println ("-------------------------------------");

 

Person flypoliccar =New
Shirtdecorator (mongocar );

Flyw.car. Show ();

}

}

The output result is as follows:

I'm not dressed up yet, GG.

-------------------------------------

I'm not dressed up yet, GG.

I put on the Gg of blue jeans, and I'm a handsome guy, O Haha ~

-------------------------------------

I'm not dressed up yet, GG.

I put on the Gg of blue jeans, and I'm a handsome guy, O Haha ~

I wear a GG with a white shirt. I am very fond of O Haha ~

 

Advantages and disadvantages of the decoration mode: 

Advantages:

First, the decoration mode can be used to expand the object functions more flexibly than the inheritance relationship, and the object functions can be added as needed. Second: through different decorative categories and the specific arrangement and combination of decorative categories, You can construct decorative results of many different types.

Disadvantages:

First, because there are many decorative categories in the decoration mode, this will make the system generate the right object and cause corresponding management problems. Secondly, it is due to the flexibility of the decoration mode, the arbitrary combination may produce many objects that can be compiled and run normally, but it does not conform to the logic. Finally, due to the flexibility, it is easy to make mistakes in inheritance, the error cannot be identified after an error occurs.

Introduction to the actual application of the decoration mode: 

The decoration mode is used for the following occasions:

When you want to add new responsibilities to objects transparently and dynamically.

Responsibilities added to objects may be increased or decreased in the future.

If the inheritance Extension function is not realistic, you should consider using a combination.

The decoration mode is well used in the design and implementation of the Java Io library. The decorator mode is used in the Java. Io package provided by JDK to encapsulate various input and output streams. The following uses Java. Io. outputstream and its subclass as an example to describe how to use the decorator mode in Io.

Let's look at a piece of code used to create an IO stream:

The following is a code snippet:

Try {

Outputstream out = new
Dataoutputstream (New fileoutputstream ("test.txt "));

} Catch (filenotfoundexception e ){

E. printstacktrace ();

}

This code is no longer familiar to those who have used JAVA input and output streams. We use dataoutputstream to encapsulate a fileoutputstream. This is a typical decorator mode. fileoutputstream is equivalent to component, and dataoutputstream is a decorator.

In the java. Io package, not only does outputstream use the decorator design mode, but also inputstream, reader, and writer use this mode. As a flexible and scalable class library, JDK uses a large number of design patterns, such as the MVC pattern in the swing package and the proxy pattern in RMI. The Research on models in JDK not only deepens the understanding of the models, but also facilitates a more thorough understanding of the structure and composition of the class libraries.

Tip: 

The key to using the decoration mode is to make different combinations based on actual business needs, it is very likely to generate a situation where compilation and running are successful but not in line with the actual logic.

In fact, for GG, what to wear is not very important. More importantly, it is GG's character and quality. Another aspect is personality. All in all, just show the true elegance.


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.