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