views:

189

answers:

2

Hello,

I have these classes in C# (.NET Framework 3.5) described below:

public class Base
{
   public int State {get; set;}

   public virtual int Method1(){}
   public virtual string Method2(){}
   ...
   public virtual void Method10(){}
}


public class B: Base
{ 
  // some implementation
}

public class Proxy: Base
{
   private B _b;
   public Proxy(B b) { _b = b; }

   public override  int Method1()
   {
      if (State == Running)
         return _b.Method1();
      else 
         return base.Method1();

   }


   public override  string Method2()
   {
      if (State == Running)
         return _b.Method2();
      else 
         return base.Method2();

   }


    public override  void Method10()
   {
      if (State == Running)
         _b.Method10();
      else 
         base.Method10();
   }
}

I want to get something this:

public Base GetStateDependentImplementation()
{ 
   if (State == Running) // may be some other rule
     return _b;
   else 
     return base; // compile error
}

and my Proxy's implementation will be:

public class Proxy: Base
{
   ...
   public override  int Method1()
   {
      return GetStateDependentImplementation().Method1();
   }


    public override string Method2()
   {
      return GetStateDependentImplementation().Method2();
   }
   ...
}

Of course, I can do this (aggregation of base implementation):

 public RepeaterOfBase: Base // no any overrides, just inheritance
    { }

public class Proxy: Base
{
   private B _b;
   private RepeaterOfBase _Base;

    public Proxy(B b, RepeaterOfBase aBase) 
    { 
      _b = b; 
      _base = aBase; 
    }
}

...
     public Base GetStateDependentImplementation()
    { 
       if (State == Running)
         return _b;
       else 
         return _Base; 
    }
...

But instance of Base class is very huge and I have to avoid to have other additional copy in memory.

So I

  • have to simplify my code
  • have to "wrap" implementation
  • have to avoid a code duplication
  • have to avoid aggregation of any additional instance of Base class (duplication)

Is it possible to reach these goals?

A: 

You can have your proxy object implicitly convert to Base like this:

public class BaseProxy
{
   private Base _base;
   private B _runningBase;

   public BaseProxy(B b)
   {
      _base = new Base();
      _runningBase = b; 
   }

   public static implicit operator Base(BaseProxy proxy)
   {
      return (State == Running) ? proxy._runningBase : proxy._base;
   }
}

This will allow your client code to get a reference to the underlying object (so it's not fully insulated), but it does allow you to access the proxy and have the methods behave as you expect.


Note: this doesn't actually allow the implicit usage like I thought.

Dan Bryant
But every time I need write ((Base)proxy).Method1().. :(or should to add property to the proxy: public Base Implicit { get { return ((Base) this); } }and should to call it:proxy.Implicit.Method1();
igor
You're right, I thought it would allow an implicit '.', but it doesn't.
Dan Bryant
+1  A: 

One solution is to extract the expensive state into its own class and share an instance of it between your concrete implementations.

Jeff Sternal
you are right! thank you! I have started it today morning.
igor
Excellent (especially after I misunderstood your problem for so long) - good luck!
Jeff Sternal
Really I have tried many variants. I need your last vote to be completely agreed. thanks. ;)
igor