java--01--static Proxy

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.