1. Proxy Mode 1.1 Overview Proxy is a design pattern that provides additional access to the target object, which is accessed through the proxy. The benefit is that additional functionality can be enhanced based on the target object implementation. (extends the capabilities of the target object). Example: Star (old Division) <---Fine volunteers <------- The key point of the user target (proxy) proxy mode: The proxy object and the target object. 1.2 Static proxy static proxy, 1) proxy object, to achieve the same interface as the target object; 2) Example: save User (analog) Dao , save Daoproxy directly, To add a transactional summary to a Save method static proxy: 1) You can extend the function of the target object without modifying the function of the target object. 2) Cons: --"Because of the proxy object, you need to implement the same interface as the target object. So there will be a lot of proxy classes, too many classes. --" once the interface is incremented, both the target object and the proxy object are maintained." Solution: Agent factory? You can use dynamic proxies.
Interface: (dancing, or singing?)
1 Public Interface Iuserdao {23 void save (); 4 5 void update (); 6 }
View Code
Target object All the work is done, singing and dancing must be done
1 //target Object2 Public classUserdao implements iuserdao{3 4 @Override5 Public voidSave () {6System. out. println ("target object, insert data into DB ...");7 }8 9 @OverrideTen Public voidUpdate () { OneSystem. out. println ("target object, modify data ..."); A - } - the}View Code
The proxy object declares that Ms. Cang will sing and dance.
1 //proxy object, who's the agent? Bring in the target object. Initialize2 Public classUserdaoproxy implements iuserdao{3 4 PrivateIuserdao Userdao;5 Publicuserdaoproxy (Iuserdao Userdao) {6 This. Userdao =Userdao;7 }8 9 @OverrideTen Public voidSave () { OneSystem. out. println ("proxy object, opening transaction"); AUserdao.save ();//target Object -System. out. println ("proxy objects, committing transactions"); - the } - - @Override - Public voidUpdate () { +System. out. println ("proxy object, opening transaction"); -Userdao.update ();//target Object +System. out. println ("proxy objects, committing transactions"); A } at - -}View Code
Come on...
1 Public classProxytest {2 3 Public Static voidMain (string[] args) {4 //1. Static proxy5 /*Iuserdao target = new Userdao ();6 Iuserdao proxy = new Userdaoproxy (target);7 Proxy.save ();8 proxy.update ();9 */Ten /* One proxy object, opening transaction A target object, insert data into DB ... - proxy objects, committing transactions - the Open Transaction - target object, modify data ... - Commit a transaction - */ + - //2. Dynamic Agent +Iuserdao target =NewUserdao (); AIuserdao proxy = (Iuserdao)Newproxyfactory (target). Getproxyinstance (); at Proxy.save (); - proxy.update (); - /* - Open Transaction - target object, insert data into DB ... - Commit a transaction in - Open Transaction to target object, modify data ... + Commit a transaction - */ the } *}View Code
java--01--static proxy