Java design mode---adapter mode

Source: Internet
Author: User

1. Links to several related models:

Adapter mode:
The interface of a class is transformed into another interface that the customer expects, and the adapter makes it possible for classes that are incompatible with the original interface to work together.

Appearance mode:
Provides a unified interface for accessing a group of interfaces for a subsystem, and the appearance defines a high-level interface that makes the subsystem easier to use. Enables loose coupling between the customer and the subsystem

The difference between the two is:
The intent of the façade mode is to simplify the interface, while the adapter's intention is to convert the interface into different interfaces.
Use an adapter when you need to use an existing class and its interface does not meet your needs; Use the appearance when you need to simplify and unify a large interface or a group of responsible interfaces.

Differences from Decorators:
The adapter wraps an object to change its interface, and the decorator wraps an object to add new behaviors and responsibilities.

2. Example:

The old Enumerator enumeration (interface) contains the following elements:
hasMoreElements ();
Nextelement ();

The new iterator iterator (interface) contains the following elements:
Hasnext ();
Next ();
Remove ();

In the face of the old legacy code, if you need to dock the new code, then the adapter is used, the implementation code is as follows:

public class Enumerationiterator implements Iterator {    enumeration enum;    Public Enumerationiterator (enumeration enum) {        this.enum = enum;    }    @Override Public    Boolean hasnext () {        return enum.hasmoreelements ();    }    @Override public    Object Next () {        return enum.nextelement ();    }    @Override public    Void Remove () {        throw new unsupportedoperationexception ();    }}

3, the adapter or the appearance mode needs to follow the "least knowledge" principle, the implementation policy is as follows:

In the case of any object, within the method of the object, we should only invoke methods that fall into the following scopes:
1) the object itself;
2) The object that is passed in as a parameter of the method;
3) Any object created or instantiated by this method;
4) Any component of the object (component <==> by instance)
That is, if an object is the return result of a call to another method, do not invoke the object's method.

Java design mode---adapter mode

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.