Recently saw some design patterns of the article, I also have some ideas. Is the company is a love travel company, inevitably to buy a lot of train tickets. The process of buying a ticket has been changing. At first, everyone bought their own train tickets and reimbursed them when they returned to the company. Just like this:
public class Atfirst {public static void main (string[] args) { employeeabuy. Abuy (); Reimbursement Employeebbuy. Bbuy (); Reimbursement }} class employeeabuy{public static void abuy () {System. OUT.PRINTLN ("A bought one");} Class employeebbuy{public static void bbuy () {System. OUT.PRINTLN ("B bought a");}}
Adapter ModeLater, more and more people, travel more and more frequently, not only the individual tickets, the overall ticket is also a lot, so, we should improve: train tickets by the company to buy, employees to the company administrative application, but the administration is very busy, usually let the staff in the department to buy, the final is the administration department to buy the ticket, research and development department But we just asked the administration for a ticket and passed in our name. Reflected in the program is the administrative department as an adapter, suitable for the purchase of departments from the Research and development department, like this:
Administrative department-Adapter public class Adapter implements buyticket { rdbuy rdbuy=new rdbuy (); @Override public void Buy (String name) { rdbuy.buy (name);} } Interface buyticket{public void Buy (String name),} class Rdbuy implements buyticket{@Override public void Buy (String name) {System. Out.println ("R & D" +name + "bought a train ticket" ); }
That's the way we buy it:
public static void main (string[] args) { String emploeename= "Research and Development department-Lao Wang" ; Buyticket buyer= New Adapter (); Buyer. Buy (Emploeename); }
In fact, but for this process, you can also improve some:
public class Adapter implements buyticket {public static void main (string[] args) {String emploeename= "research and Development department-Lao Wang" ; Buyticket buyer= New Adapter (); Buyer. Buy (Emploeename); } private Administrationbuy administraionbuy= new Administrationbuy (); Rdbuy rdbuy=new rdbuy (); @Override public void Buy (String name) {if (Name.contains ("development" )) {rdbuy.buy (name);} if (Name.conta INS ("admin" )) {administraionbuy.buy (name);}}} Interface buyticket{public void Buy (String name); Administrative Department class Administrationbuy implements buyticket{@Override public void Buy (String name) {System. out.print ln ("admin Department" +name + "bought a train ticket" );}} Research and Development Department class Rdbuy implements buyticket{@Override public void Buy (String name) {System. OUT.PRINTLN ("Research and Development Department" + Name + "Bought a train ticket" ); }}
Proxy mode:To later, the administrative department to increase the staff, you can buy tickets for everyone, and buy tickets this piece also need some audit registration, can not be bought by everyone, so do not let all departments buy, of course, for us, nominally or company buy.
Interface buyticket{public void Buy (String name)} class Companybuy implements buyticket{ @ Override public void Buy (String name) { System. Out.println (name+ "bought a train ticket" );} }// Administrative department to audit class Administraionpoxy implements buyticket{buyticket buyticket; public Administraionpoxy (buyticket Buyticket) {this. buyticket= buyticket;} @Override public void Buy (String name) {System. OUT.PRINTLN ("Audit ..." c17>); Buyticket.buy (name); System. Out.println ("Record ..."); }
When we bought it,
public class Proxy {public static void main (string[] args) { buyticket employee= new companybuy ();
buyticket company= New administraionpoxy (employee); Company. Buy ("Employeeabuy"); Company. Buy ("Employeeabuy");}}
Dynamic Agent:Later, I perfected the OA process, we only know the application on the line, do not care how to buy, how to buy.
Interface buyticket{public void Buy (String name)} class Companybuy implements buyticket{ @ Override public void Buy (String name) { System. Out.println ("After a series of formalities ..." +name + "buy tickets" ); } Class Administraionhandler implements Invocationhandler {private Object object; public Administraionhandler (Object object) {this. object= object;} @Override public object Invoke (object proxy, Metho D method, object[] args) throws throwable {Object result= null; System. OUT.PRINTLN ("Internal audit ..."); result= Method.invoke (object, args); System. OUT.PRINTLN ("Internal records ..."); return
For the curious classmate, when call buy method, no longer see the details of the purchase.
public static void main (string[] args) { buyticket buyer= new companybuy (); Invocationhandler handler= New administraionhandler (buyer); Buyticket proxy=(buyticket) proxy. Newproxyinstance (Buyer.getclass (). getClassLoader (), Buyer.getclass (). Getinterfaces (), handler); Proxy. Buy ("Lao Wang");
The dynamic proxy is built using reflection, and I'll explain it in another article about reflection.
Buy train tickets from the company to Agent mode and adapter mode