Decorator mode & amp; proxy Mode

Source: Internet
Author: User

I. decorator Mode

In the modifier mode, a class is redecorated without changing the original functions, so that it has more powerful functions. It is described as "icing on the cake" with an idiom ".

Class Structure:


Component: an abstract Component that defines a set of abstract interfaces and specifies the functions of the decorated Component.

ComponentImpl: Abstract component implementation class, complete basic function implementation

Decorator: Decorator role, which holds instance references of Component. It is a bit recursive.


Pseudocode:

Component c=new ComponentImpl();Decorator d1=new Decorator();d1.setComponent(c);Decorator d2=new Decorator();d2.setComponent(d1);Decorator d3=new Decorator();d3.setComponent(d2);Decorator d4=new Decorator();d4.setComponent(d3);d4.methodA();

The modifier mode is similar to the adapter mode. wrapper both has a class, but its contents are different. The adapter mode converts an interface to another interface to achieve matching. The modifier mode does not change the original interface. Instead, it changes the processing method of the original object and uses recursion to improve performance.


Ii. Proxy Mode

Proxy mode, which provides a proxy for other objects to control access to this object.

Class Structure:



Subject: interface class, which defines some interface methods that require proxy

RealSubject: Implementation class

ProxySubject: A proxy class that saves a Subject reference and can inject a specific subclass such as RealSubject.


The proxy mode is to introduce a certain degree of indirect property when operating the object. This indirect nature can add many additional operations. For example, permission control and parameter verification


Interface Class

/*** Personal information management ** @ author onlyone */public interface PersonManager {/*** query the salary ** @ param name: name of the person to be found * @ param operateName Operator name */ public double getSalary (String name, string operateName );}

Implementation class

/*** Specific implementation class ** @ author onlyone */public class RealPersonManager implements PersonManager {@ Override public double getSalary (String name, String operateName) {// query the database, returns the actual salary. return 1000000 ;}}

Proxy

/*** Proxy class ** @ author onlyone */public class ProxyPersonManager implements PersonManager {// interface reference PersonManager realPersonManager = new RealPersonManager (); @ Override public double getSalary (String name, string operateName) {// 1. add some permission judgments. For example, whether the operator has the permission to query a person's salary. // 2. Call return realPersonManager. getSalary (name, operateName) for a specific class );}}


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.