proxy,pattern

Erhan 8/7/2016 0

Proxy Pattern

C#
Challenge
You are working on automating Excel COM objects. The business logic has to think
too much about instantiating COM, doing ground works before calling the actual
functionalities etc.
The same code could get replaced by DCOM tomorrow. So the Excel access code is
spread throughout your application. How to do a better design?
Definition
GoF Definition: "Provide a surrogate or placeholder for another object to control
access to it"
Implementation
We can solve the above problem by using a Proxy pattern. As the definition says, we
will have to create a placeholder or wrapper around the original object to control
access to it. In this way we can make the following advantages:
. Talk to COM or DCOM Excel object by changing configuration in one place
. Give application a simple interface to talk with Excel COM/DCOM
. Feel application think like it is talking to a local object

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

namespace PrototypePattern
{
    public class Settings : ICloneable
    {
        public Settings()
        {
            // Load ApplicationSettings from Configuration
            // Load ThemeSettings from Database
            // Load UserSettings from Database
            Thread.Sleep(3000); // Simulate a delay for the above operations

            ApplicationSettings = "ApplicationSettings1";
            ThemeSettings = "ThemeSettings1";
            UserSettings = "UserSettings1";
        }

        public string ApplicationSettings
        {
            get;
            set;
        }

        public string ThemeSettings
        {
            get;
            set;
        }

        public string UserSettings
        {
            get;
            set;
        }

        public object Clone()
        {
            return this .MemberwiseClone();
        }

        public override string ToString()
        {
            return this .ApplicationSettings   " "   this.ThemeSettings   " "   this.UserSettings;
        }
    }
}

*****************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PrototypePattern
{
    class Program
    {
        static void Main(string[] args)
        {
            Settings settings = new Settings();
            Settings settingsClone = (Settings )settings.Clone();

            settingsClone.ApplicationSettings = "NewSettings";

            Console.WriteLine("INSTANCE 1: "   settings.ToString());
            Console.WriteLine("INSTANCE 2: "   settingsClone.ToString());

            Console.ReadKey(false );
        }
    }
}

  
 

Report Bug

Please Login to Report Bug

Reported Bugs

Comments

Please Login to Comment

Comments