The five-----appearance mode and adorner mode of Java Advanced design mode

Source: Internet
Author: User

Objective

In the previous article, we learned the adapter mode and bridge mode of the structural mode. This article is to learn the structure of the pattern of the appearance of the pattern and adorner mode.

Appearance mode

Brief introduction

The appearance mode hides the complexity of the system and provides the client with an interface that the client can access the system. This type of design pattern is a structured pattern that adds an interface to an existing system to hide the complexity of the system.

The simple thing is to provide a simple interface to hide the logic of implementation. For example, the power key of the common computer, we just press the power button, we can let it start or shut down, do not need to know how it started (Start CPU, boot memory, boot hard disk), how to shut down (close the hard disk, shut down the memory, turn off the CPU);

Here we can still use the computer to play the example of the game to the appearance of a simple explanation.
There are some online games on the computer, namely DNF, lol and wow, we just double-click the icon on the computer to start and play the game, no need to care about how the game started and run.

The steps you need to implement are as follows:

    1. Establish the interface of the game;
    2. Build lol, DNF and WOW classes and implement the game interface;
    3. Defines a skin class that is provided to the client for invocation.
    4. Invokes the skin class.

code example:

interface Game{    void play();}class DNF implements Game{    @Override    public void play() {        System.out.println("正在玩DNF...");    }}class LOL implements Game{    @Override    public void play() {        System.out.println("正在玩LOL...");    }}class WOW implements Game{    @Override    public void play() {        System.out.println("正在玩WOW...");    }}class Computer{        private Game dnf;    private Game lol;    private Game wow;        public Computer() {        dnf=new DNF();        lol=new LOL();        wow=new WOW();    }        public void playDNF(){        dnf.play();    }        public void playLOL(){        lol.play();    }        public void playWOW(){        wow.play();    }   }public static void main(String[] args) {        Computer computer=new Computer();        computer.playDNF();        computer.playLOL();        computer.playWOW();    }

Operation Result:

    正在玩DNF...    正在玩LOL...    正在玩WOW...

In the code example above, when we want to play the game, we only use the instantiation of the skin class to invoke the game method, no need to care about how the game starts and runs. And each game is also independent of each other, do not affect each other, not because a game can not play the other games can not be run. Actually feel the appearance pattern and we usually use the interface is very similar, all is provides the external interface, does not need the concern is how realizes.

Advantages of the Appearance mode:

Reduced coupling and, in some ways, increased security.

Disadvantages of the Appearance mode:

does not conform to the opening and shutting principle, difficult to change.

Usage Scenarios

When there are multiple complex modules or subsystems in the system.

Adorner mode

Brief introduction

Adorner mode 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.

Decorator mode, as the name implies, is to decorate something so that it can provide some extra functionality. For example, to decorate people, do different things when wearing different clothing. For example, wearing a jersey is ready to play ball, put on a swimsuit is ready to go swimming and so on.

Adorner mode can dynamically add some additional responsibilities to an object.

Here we still use an example to illustrate.
In today's toy model, there are two models are very popular, up to (GUNDAM) model and jelly like (MRGU) model, when we stitching the model, it is generally the first stitching the model, and then to add some additional accessories, such as weapons. Here we are stitching up the GUNDAM model and the jelly like (MRGU) model and loading them with their own weapons.

The following steps are implemented:

    1. Create an abstract component of the model interface, which has assembled this method;
    2. Create concrete component Classes (Gundam classes and Mrgu classes), and implement the model interfaces described above;
    3. Define an adorner to accept the client's request and make appropriate calls based on the client's request;
    4. Defines a class that implements decorations to add the appropriate functionality to an object.

code example:

Interface model{void assemble ();}    Class GUNDAM implements model{@Override public void assemble () {System.out.println ("Assemble a Gundam model");    }}class Mrgu implements model{@Override public void assemble () {System.out.println ("assemble a tie-up model");        }}abstract class Addextra implements model{protected model model;    Public Addextra (model model) {This.model=model;    } public void Assemble () {model.assemble ();    }}class lightsaber extends addextra{public lightsaber (model model) {super model);        } public void Assemble () {model.assemble ();    Addlightsaber ();    } public void Addlightsaber () {System.out.println ("add lightsaber");    }}class Rocketlauncher extends addextra{public rocketlauncher (model model) {super model);        } public void Assemble () {model.assemble ();    Addrocketlauncher ();    } public void Addrocketlauncher () {System.out.println ("Add rocket launcher"); }}public static void Main (string[] args) {Model gundam=new Gundam ();        Model mrgu=new Mrgu ();        Gundam.assemble ();                    Mrgu.assemble ();        Model gmodel=new Lightsaber (new GUNDAM ());        Gmodel.assemble ();        Model mmodel=new Rocketlauncher (New Mrgu ()); Mmodel.assemble ();}

Operation Result:

    组装一个高达模型    组装一个扎古模型        组装一个高达模型    添加光剑    组装一个扎古模型    添加火箭筒

In the above code, if we just want to assemble up to or this ancient model, we can instantiate the model class directly and invoke the method. If you need to add a weapon when you assemble the model, simply add the appropriate functionality through the adorner's class.
With this example, we find that in the case of the adorner pattern , some classes can be extended without compromising the previous functionality and increasing flexibility.

Advantages of Adorner mode:

Decorative and decorative categories can be independent development, low coupling, easy to expand, flexible and convenient.

Disadvantages of Adorner mode:

Too many decorations on a class can add complexity.

Usage Scenarios
The prototype does not change, dynamically adds some functions when.

Other music recommendations

Original is not easy, if feel good, hope to give a recommendation! Your support is my greatest motivation for writing!
Copyright Notice:
Empty Realm
Blog Park Source: http://www.cnblogs.com/xuwujing
CSDN Source: HTTP://BLOG.CSDN.NET/QAZWSXPCM
Personal blog Source: http://www.panchengming.com

The five-----appearance mode and adorner mode of Java Advanced design 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.