Adapter mode & amp; Observer Mode

Source: Internet
Author: User

I. Adapter Mode

The adapter Mode means that an interface of a class cannot be accepted by the client and needs to be converted to another interface so that two mismatched interfaces can work together.

Class Structure:


Adaptee: source interface, which must be adapted <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Release/seq907/ao6yxqcK2s/bIpbXEvdO/release/aysrF5LPJxL + release + PHA + vtm49s/WyrXA/release + PHA + release/release + release/B97mry761xM + release/ serial + 1/a1pbbAysrF5KOszazKsb3hus + Serial + PHA + pgltzybzcm9 "http://www.2cto.com/uploadfile/Collfiles/20140401/20140401093103345.jpg" alt = "\"/>


Ii. Observer Mode

The observer mode is also called the publish-subscribe mode or the event listening mode. It defines one-to-many dependencies so that multiple observer objects can listen to one topic object at the same time, if the status of the topic object changes, all observer objects are notified.

Asynchronous messages (MQ and activeMQ) are based on this mode.

Class Structure:


Subject: The topic class. It stores all the observer objects in a List set and provides addition and deletion methods and notification methods after the status changes.

Observer: the abstract interface of the Observer, which provides an abstract action method. The specific business is implemented by sub-classes.

ConcreteObserver: a specific observer responsible for implementing business operations.

ConcreteSubject: a topic class that sends a notification to all registered observers when the internal status changes.


Advantages:

1) decoupling: Both Sides of coupling depend on abstract classes, rather than on specifics. So that their changes do not affect the changes on the other side.

2) The Observer uses an abstract class. The advantage is that it can extract the same code logic of multiple subclasses and put them in the abstract 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.