Decorator mode-Easy to remember IO class relationships and APIs

Source: Internet
Author: User
Tags system log

First, Directory

II. Overview and model 1. Overview

Meaning: Controls access to an object.

Function: The method of controlling an object in detail, pre-processing before invocation, and post-processing after invocation, thus enabling the processing of the uniform Process code in the proxy class.

Advantages:

Disadvantages:

2. Model

  

Component (abstract component): A public external method that defines a proxy role and a real role.

Concretecomponent (real component): Implements an abstract role that defines the real business logic to be implemented by the real role for invocation by the proxy role.

Decorator (abstract decorator): the implementation of abstract role, is the real role of the agent, through the real role of the business logic method to implement the abstract method, and can attach their own operations.

Concretedecorator (real decorator):

Ultimate goal: Put Unified Process Control into agent role processing.

Iii. description of the application scenario

1, security agent: Shielding the real role of direct access, by the role of the agent control, such as the real needs of the business when the proxy object calls the Real object method.

2. Remote Proxy: Handles remote method invocation (RMI) through the proxy class.

3, delay the agent: Load the Lightweight proxy object first, really need to load the real object again. For example, loading pictures, video, first by the proxy object in the background to open the Stream object, to really need to browse pictures, video, the real object to load the picture, and finally closed by the proxy object stream object.

4, Business: To solve the overview of the content, a layer of unchanging content is assumed by the proxy object, and the real business logic is implemented by the real object method.

5, the real system log processing, JDBC transaction, connection open and close.

Code Demo Scenario Description: If a star is invited to make an advertisement or film, then it is important to have these processes: interview----drafting contract, arranging tickets, tickets, and attendance at the end of payment. If you make one or two commercials a year, it's entirely up to a star to solve it. In fact, each star has a broker, while the star is only responsible for attending the event, and other matters are handled by the broker.

  

Four, Code demonstration

1. Abstract Components

 Public Abstract class Component {    publicabstract  void  display ();}

2. Real Components

 Public class extends component{    publicvoid  display () {        System.out.println (" I'm a dirt wall! " );    }}

3. Abstract Decorator

 Public classDecoratorextendscomponent{protectedComponent Component;  PublicDecorator (Component Component) { This. Component =component; }     PublicComponent getcomponent () {returncomponent; }     Public voidsetcomponent (Component Component) { This. Component =component; }     Public voiddisplay () {component.display (); }}

4. Real Decorator

Decorator A: Dirt wall
publicclassextends Decorator { public Concretedecoratora ( Component Component) { super(Component); } @Override publicvoid display () { this. Component.display (); System.out.println ("add cement on the dirt wall!") "); }}

Decorator B: Wall with wallpaper
publicclassextends Decorator { public Concretedecoratorb ( Component Component) { super(Component); } @Override publicvoid display () { this. Component.display (); System.out.println ("Add wallpaper on Peeling");} }

5, the Client

 Public classClient { Public Static voidmain (String [] args) {//Create a new dirt Wall: Original Component ObjectComponent wall=Newconcretecomponent (); //decorator A: adding cement to the soil wallConcretedecoratora cementwall=NewConcretedecoratora (wall);        Cementwall.display (); System.out.println ("-----------Split Line-------------"); //decoration B: Add wallpaper to the wall of the EarthConcretedecoratorb paperwall=NewConcretedecoratorb (wall);        Paperwall.display (); System.out.println ("-----------Split Line-------------"); //Decorator B: Add wallpaper to the cement wall (decorator a)Concretedecoratorb mixedwall=NewConcretedecoratorb (Cementwall);    Mixedwall.display (); }}

6. Test results

Decorator mode-Easy to remember IO class relationships and APIs

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.