One system wants to use another system's data source, but the data source format does not meet its own requirements, which can be used in adapter mode
One example: The data provided by the personnel system returns the map type, but the financial system needs a List type of data
Data sources provided by the personnel system:
1 ImportJava.util.HashMap;2 ImportJava.util.Map;3 4 Public classpeople {5 PublicMap<object, object>Getorgname () {6Map<object, object> map =NewHashmap<>();7Map.put (1, "personnel Department");8Map.put (2, "Administrative Office");9Map.put (3, "Finance Office"));Ten returnmap; One } A}
Call mode in the personnel system:
1 ImportJava.util.Map;2 3 Public classpclient {4 5 Public Static voidMain (string[] args) {6People people =Newpeople ();7Map<object, object> map =people.getorgname ();8 for(inti = 0; I < map.size (); i++) {9System.out.println (Map.get (i + 1));Ten } One A } - -}
The financial system uses the data source adapter:
1 Importjava.util.ArrayList;2 ImportJava.util.Map;3 4 Public classPeopleadapterextendsArraylist<object> {5 Privatemap<?,? >map;6 7 PublicPeopleadapter (map<?,? >map) {8 Super();9 This. Map =map;Ten } One A Public intsize () { - returnmap.size (); - } the - PublicObject Get (inti) { - returnMap.get (i); - } +}
Financial system calls:
1 Public classeclient {2 3 Public Static voidMain (string[] args) {4Peopleadapter list =NewPeopleadapter (Newpeople (). Getorgname ());5 for(inti = 0; I < list.size (); i++) {6System.out.println (List.get (i + 1));7 }8 }9 Ten}
Adapter Mode Adapter