C # proxy mode in Design Mode

Source: Internet
Author: User

Introduction: I have to go to the old Wang store to buy something because I have to go to the old Wang store to buy some flowers and candy. Here, the subject is defined as an interface, and the RealSubject is itself, and the Proxy is the Fire Cloud. I and the Fire Cloud inherit this interface to implement shopping. Code: [csharp] <SPAN style = "FONT-SIZE: 18px"> interface BuySomething // interface-buy thing {void BuyFlowers (); void BuyCandys ();} class MoneySource: buySomething // payer-I {ShopKeeper laowang; public MoneySource (ShopKeeper laowang) {this. laowang = laowang;} public void BuyFlowers () {Console. writeLine (laowang. name + "I want to buy flowers");} public void BuyCandys () {Console. writeLine (laowang. name + "I want to buy candy") ;}} class Sho PKeeper // store owner-Old Wang {private string name; public string Name {get {return name;} set {name = value ;}} class Proxy: buySomething // Proxy class {MoneySource gangge; public Proxy (ShopKeeper laowang) {gangge = new MoneySource (laowang);} public void BuyFlowers () {gangge. buyFlowers ();} public void BuyCandys () {gangge. buyCandys () ;}}</SPAN> interface BuySomething // interface-buy thing {void BuyFlowers (); void BuyCandys ();} class MoneySource: BuySomething // payer-I {ShopKeeper laowang; public MoneySource (ShopKeeper laowang) {this. laowang = laowang;} public void BuyFlowers () {Console. writeLine (laowang. name + "I want to buy flowers");} public void BuyCandys () {Console. writeLine (laowang. name + "I want to buy candy") ;}} class ShopKeeper // shop owner-Old Wang {private string name; public string Name {get {return name;} set {name = valu E ;}} class Proxy: BuySomething // Proxy class {MoneySource gangge; public Proxy (ShopKeeper laowang) {gangge = new MoneySource (laowang);} public void BuyFlowers () {gangge. buyFlowers ();} public void BuyCandys () {gangge. buyCandys () ;}} in the proxy class, a reference is saved so that the proxy can access the object. That is to say, there is a way in the proxy of huoyun to let him buy things. Client code: [csharp] <SPAN style = "FONT-SIZE: 18px"> static void Main (string [] args) {ShopKeeper laowang = new ShopKeeper (); laowang. name = "Wang Jun"; Proxy huoyun = new Proxy (laowang); huoyun. buyFlowers (); huoyun. buyCandys (); Console. read () ;}</SPAN> static void Main (string [] args) {ShopKeeper laowang = new ShopKeeper (); laowang. name = "Wang Jun"; Proxy huoyun = new Proxy (laowang); huoyun. buyFlowers (); huoyun. BuyCandys (); Console. Read ();} in this way, you can make the Fire Cloud buy things for me. Proxy mode: provides a proxy for other objects to control access to this object. In some cases, an object is not suitable or cannot be directly referenced by another object, proxy objects can play a mediation role between the client and the target object. Proxy mode: 1. remote Proxy: provides a local representation of an object in different address spaces. in this way, the fact that an object exists in different address spaces can be hidden. 2. virtual Proxy: Creates objects with high overhead as needed to store real objects that take a long time for instantiation. 3. security Proxy: used to control the access permissions of real objects. 4. intelligent Guidance: when calling real objects, the proxy handles other things. the proxy mode introduces a certain degree of indirect property when accessing objects, because this indirect property can be used for multiple purposes.

Related Article

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.