开发者

How to "wrap" implementation in C#

开发者 https://www.devze.com 2022-12-24 01:03 出处:网络
I have these classes in C# (.NET Framework 3.5) described below: public class Base { public int State {get; set;}

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();

   }


    pub开发者_StackOverflow社区lic 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?


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号