Design Pattern _ adapter Pattern
Adapter PatternConverl the interface of a class into another interface clients CT. adapter lets classes work together that couldn't otherwise because of incompatible interfaces. (The interface of a class is transformed into another interface that the client expects, so that the two classes that cannot work together due to interface mismatch can work together)
Unfamiliar WordsIncompatible is incompatible and contradictory
ExampleThe two human resource management systems are designed with different interfaces. What should we do when the consumer needs to interact?
The company's interface is as follows:
public interface IUserInfo { public String getUserName(); public String getHomeAddress(); public String getMobileNumber(); public String getOfficeTel(); public String getJobPosition(); public String getHomeTel();}
// Public class UserInfo implements IUserInfo {public String getHomeAddress () {System. out. println (here is the employee's home address); return null;} public String getHomeTel () {System. out. println (the employee's home phone number is ...); return null;} public String getJobPosition () {System. out. println (employee position); return null;} public String getMobileNumber () {System. out. println (employee's mobile phone number); return null;} public String getOfficeTel () {System. out. println (employee's office number); return null;} public String getUserName () {System. out. println (employee name); return null ;}}
The intermediary company interface is as follows:
@SuppressWarnings(unchecked)public interface IOuterUser { public Map getUserBaseInfo(); public Map getUserOfficeInfo(); public Map getUserHomeInfo();}
// Another system public class OuterUser implements IOuterUser {@ SuppressWarnings (unchecked) public Map getUserBaseInfo () {HashMap baseInfoMap = new HashMap (); baseInfoMap. put (userName, this employee is Jack); baseInfoMap. put (mobileNumber, the employee's phone number is ....); return baseInfoMap;} @ SuppressWarnings (unchecked) public Map getUserHomeInfo () {HashMap homeInfoMap = new HashMap (); homeInfoMap. put (homeTelNumber, the employee's home number is); homeInfoMap. put (homeAddress, the home address of this employee is ....); return homeInfoMap;} @ SuppressWarnings (unchecked) public Map getUserOfficeInfo () {HashMap userOfficeInfoMap = new HashMap (); userOfficeInfoMap. put (jobPosition, this employee position is); userOfficeInfoMap. put (officeTelNumber, the employee's office number is ....); return userOfficeInfoMap ;}}
RequirementThe company employs an intermediary company to store the information in the system of the intermediary company. If the information of the borrower is changed, the system of the company needs to be updated immediately.
AnalysisDifferent systems, you need to convert the external system to the cost system, get the external system data, and then convert the cost system data. You can use RMI to treat remote objects as local objects.
/** Adapter * OuterUser is the information management of another system * IUserInfo is its own system interface * so. The problem is to add another one on the basis of the system to complete packaging of external systems, this allows the system to use the * focus or its own interface implementation to introduce external source data provider OuterUser */public class OuterUserInfo extends OuterUser implements IUserInfo {private Map baseInfo = super. getUserBaseInfo (); private Map homeInfo = super. getUserHomeInfo (); private Map officeInfo = super. getUserOfficeInfo (); public String getHomeAddress () {String homeAddress = (String) this. homeInfo. get (homeAddress); System. out. println (homeAddress); return homeAddress;} // public String getHomeTel () {return null ;}// public String getJobPosition () {return null ;} // public String getMobileNumber () {return null;} // public String getOfficeTel () {return null () {return null ;}}
In this way, an external personnel management class OuterUserInfo is added on the basis of the original system, and the method of the system interface is implemented, thus implementing the System Conversion. It also follows the principle of opening and closing. Note: remote objects in external systems can be transmitted through constructor methods or inheritance relationships. In any case, remote objects must be members of this new class. Otherwise, they cannot be transferred.
WuCore: implement the system interface, add each template block of the external system, and call the method in the template block of the External System in the method to implement the conversion interface, the system has a selective call.
AdvantagesThe adapter allows two unrelated classes to run together. This increases the transparency of the classes and improves reusability.
Use CasesModifies an operation that has been put into production, such as system expansion.
NotesIt is to solve the problems of projects that are already in service, which is generally not considered during the design stage. This is a remedy.