Java Design Patterns

Source: Internet
Author: User

1. Observer design pattern:

Intent: define a one-to-many dependency between objects, and when the state of an object changes, all objects that depend on it are notified and automatically updated.

Main Solution: an object state changes to other object notification issues, and to consider the ease of use and low coupling, to ensure a high degree of collaboration.

when to use: the state of an object (the target object) changes, and all dependent objects (the Observer object) are notified of the broadcast notification.

How to Solve: using object-oriented technology, you can weaken this dependency.

Key code: There is a ArrayList in the abstract class that holds the observers.

2. Proxy mode:

The so-called agent is one person or institution acting on behalf of another person or institution. In some cases, a client does not want or cannot refer directly to an object, whereas a proxy object can act as an intermediary between the client and the target object.

The proxy mode class diagram is as follows:

  

Roles in proxy mode:

Abstract Object role: declares a common interface between the target object and the proxy object, so that the proxy object can be used wherever the target object can be used.

target Object role: defines the target object that the proxy object represents.

proxy Object role: the proxy object contains a reference to the target object, so that the target object can be manipulated at any time, and the proxy object provides the same interface as the target object so that the target object can be substituted at any time. Proxy objects typically perform an operation before or after a client call is passed to the target object, rather than simply passing the call to the target object.

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.