Decorator mode for Java design patterns

Source: Internet
Author: User

Decorator mode for Java design mode

Adorner mode

Adorner mode (Decorator pattern) allows you to add new functionality to an existing object without changing its structure.

This type of design pattern belongs to the structural pattern, which is a wrapper as an existing class.

This pattern creates an adornment class that wraps the original class and dynamically adds additional functionality to an object while preserving the integrity of the class method signature.

We demonstrate the use of the adorner pattern using the following example. Simulate a person who wants to eat, to find a restaurant, to enjoy food, to end the process of eating

Code Show:

First, create a modified interface Eat

Package decorator;//Dinner Interface public interface Eat {    //meal method    void Eat ();}

Create a modified person and have a status of their own

Package Decorator;public class Person implements Eat {    @Override public    void Eat () {        System.out.println ("= = = = = I am hungry ====== ");}    }

Abstract class for creating a decorator

Package Decorator;public Abstract class Likeeat implements Eat {    private Eat Eat;        Public Likeeat (Eat Eat) {        this.eat=eat;    }        @Override public    void Eat () {        eat.eat ();    }}

Create an extended class of five decorated classes (that is, extended adornments), each with its own specific state, enriching the modified class

public class Findinmap extends Likeeat {public findinmap (Eat Eat) {super (Eat);    } public void UserMap () {System.out.println ("Open map for food");        } @Override public void Eat () {super.eat ();    UserMap ();    }}public class Gotorestaurant extends Likeeat {public gotorestaurant (Eat Eat) {super (Eat);    } public void OnWay () {System.out.println ("on the way to the hotel");        } @Override public void Eat () {super.eat ();    OnWay ();    }}public class Inrestaurant extends Likeeat {public inrestaurant (Eat Eat) {super (Eat);    } public void Selectfoot () {System.out.println ("Arrival hotel selection of food");        } @Override public void Eat () {super.eat ();    Selectfoot ();    }}public class Eatfoot extends Likeeat {public eatfoot (Eat Eat) {super (Eat);    } public void Eating () {System.out.println ("Enjoy gourmet food");        } @Override public void Eat () {super.eat (); EatING ();    }}public class Endeat extends Likeeat {public endeat (Eat Eat) {super (Eat);    } public void Aftereat () {System.out.println ("===== Gourmet End =====");        } @Override public void Eat () {super.eat ();    Aftereat (); }}

Create a test class to test the adornment effect

public class Eattest {public    static void Main (string[] args) {        Eat person = new Person ();        Likeeat likeeat = new Endeat (new Eatfoot (New Inrestaurant (new gotorestaurant (person                           ) )))) ;        Likeeat.eat ();    }}

Operation Result:

Summarize:
A,likeeat abstract class, holds the Eat interface, the method is all delegated to the interface call, the purpose is to give the interface implementation class that is the subclass to call.
B,likeeat abstract class subclass (specific decorator), there is a construction method called super (Eat), this sentence reflects the abstract class relies on the implementation of the subclass is abstract dependent on the principle of implementation. Because the structure inside the parameters are eat interface, as long as the implementation of the Eat class can be passed in, that is to show

          Likeeat likeeat = new Endeat (
New Eatfoot (
New Inrestaurant (
New Gotorestaurant (
New Findinmap (person)))))

The look of this structure. So when calling Likeeat.eat () , and because of each specific decorator class, first call super.eat (); method, and the Super has been passed by the construction and pointed to a specific decorator class (this can be replaced according to the order), then called the Decoration class method, and then call their own decorative method, that is, a decorative, chain-like behavior of the filter.

C, the specific decoration, you can define the initial state or the initial self-decoration, the following decorative behavior on the basis of a step-by-step embellishment, decoration.
D, the decorator mode design principles are:

  Open to the extension, the change is closed, this sentence is reflected in if I want to extend the behavior of the decorator class, without modifying the adorner abstract class, simply inherit the adorner abstract class, the implementation of additional decoration or call behavior can be decorated by packaging.

Transferred from: http://www.cnblogs.com/kuoAT/p/6951706.html

Decorator mode for Java design patterns

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.