13.プロキシモード(Proxy Pattern)

4014 ワード

using System;



namespace ConsoleApplication6

{

    class Program

    {

        static void Main(string[] args)

        {

            //              

            Person proxy = new Friend();            

            proxy.BuyProduct();            

            Console.Read();

        }

    }



    //  

    public abstract class Person

    {

        public abstract void BuyProduct();

    }



    // 

    public class RealBuyPerson : Person

    {

        public override void BuyProduct()

        {

            Console.WriteLine(" IPhone ");

        }

    }



    //  

    public class Friend : Person

    {

        RealBuyPerson realSubject;



        public override void BuyProduct()

        {      

            if (realSubject == null)

            {

                realSubject = new RealBuyPerson();

            }



            realSubject.BuyProduct();

            Console.WriteLine(" ");

            this.PreBuyProduct();     

            this.PostBuyProduct();

        }



        public void PreBuyProduct()

        {

            Console.WriteLine(" IPhone ");      

        }



        public void PostBuyProduct()

        {

            Console.WriteLine(" IPhone ");

        }

    }



 

}