Proxy Mode C #

Source: Internet
Author: User

When we cannot or do not need direct access to a role for some reason, we can implement our specific behavior through proxy objects, which are similar to the role of matchmaker.

usingSystem;usingSystem.Collections;usingSystem.Collections.Generic;usingSystem.Linq;namespaceconsoleapplication1{classProgram {/// <summary>        ///Abstract Roles/// </summary>         Public Abstract classRole { Public Abstract voidbuyproduct (); }        /// <summary>        ///Real characters/// </summary>         Public classRealbuyrole:role { Public Override voidbuyproduct () {Console.WriteLine ("buy Something"); }        }        /// <summary>        ///Agent Role/// </summary>         Public classProxy:role {PrivateRealbuyrole Realrole;  Public Override voidbuyproduct () {if(Realrole = =NULL) {Realrole=NewRealbuyrole (); }                 This.                Prebuyproduct (); //invoke the Real role methodrealrole.buyproduct ();  This.            Postbuyproduct (); }            /// <summary>            ///the predecessor behavior of the agent role/// </summary>             Public voidprebuyproduct () {Console.WriteLine ("things to do before you buy something"); }            /// <summary>            ///Post behavior of the agent role/// </summary>             Public voidpostbuyproduct () {Console.WriteLine ("things to do after you buy something"); }        }        Static voidMain (string[] args) {Role Proxy=NewProxy (); Proxy.        Buyproduct (); }    }}

Proxy Mode C #

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.