C# Design Pattern–Proxy

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Proxy
{
    // Main Entry for execution
    class MainEntry
    {
        static void Main(string[] args)
        {
            // Create an instance of Proxy Class
            Proxy _proxy = new Proxy();

            // print owner name
            Console.WriteLine(_proxy.GetOwnerName());

            // Hold the console without disappearing
            Console.ReadLine();
        }
    }

    // Anstract class to define a method which needs to be implemented
    // by inherited classes
    abstract class Owner
    {
        public abstract string GetOwnerName();
    }

    // Class which does the complete execution
    class HouseOwner : Owner
    {
        // Override abstract method
        public override string GetOwnerName()
        {
            return "Rami";
        }
    }

    // Proxy class which wraps HouseOwner Clss
    class Proxy : Owner
    {
        private HouseOwner _owner;
        public Proxy()
        {
            // Create an instance of HouseOwner Class
            _owner = new HouseOwner();
        }

        // Override abstract method
        public override string GetOwnerName()
        {
            // Call GetOwnerName() from HouseOwner class instance
            return _owner.GetOwnerName();
        }
    }
}

You may also like...