Design Mode-proxy Mode

Source: Internet
Author: User

Recently, we have seen the shadows of design patterns in many occasions. We have been investing in algorithms and data structures for a long time. It is very important to find the design patterns. Sometimes code maintenance, reuse, and scalability are better than pure algorithms. So I picked up Daniel's book "big talk Design Patterns" and referred to the blogs of various big cows on the Internet to start my design patterns journey.

Today, we will introduce the proxy mode.

Concept:

Proxy: provides a Proxy for other objects to control access to the object.

Advantages:

1,Proxy mode can control the access permissions of objects; 2. smart pointer: controls access to objects in proxy mode. When no pointer or reference points to this object, the occupied memory is released; Disadvantages:Proxy mode brings about some additional overhead, such as not directly accessing real objects, but indirectly accessing through proxy objects. Comparison:

(1) Adapter mode Adapter
The Adapter provides a different interface for the object it adapts. Instead, the proxy provides the same interface as its entity. However, the proxy for access protection may reject operations performed by the entity. Therefore, its interface may actually be a subset of the object interface.

(2) Decorator Mode
Although the implementation of the Decorator is similar to that of the proxy, the purpose of the Decorator is different. The Decorator adds one or more functions to an object, while the proxy controls access to the object.

Sample Code:
Package Pattern;/** public interface */interface Subject {public void Print ();}/** real object */class RealSubject implements Subject {public void Print () {System. out. println ("Print done! ");} Public void DoSomething () {System. out. println (" complete other functions! ") ;}}/** Proxy object */class Proxy implements Subject {RealSubject realSubject; public void Print () {if (null = realSubject) realSubject = new RealSubject (); realSubject. print () ;}} public class Pattern {public static void main (String [] args) {try {Proxy myProxy = new Proxy (); myProxy. print ();} catch (Exception e) {e. printStackTrace ();}}}

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.