c# - Controlling read/write access to fields -


suppose separate out read , write access in interface pattern below.

namespace accesspattern {     namespace readonly     {         public interface ia { double get_a(); }     }     namespace writable     {         public interface ia : readonly.ia { void set_a(double value); }     } } 

this easy implement:

namespace accesspattern {     namespace readonly     {         public class : ia         {             protected double a;             public double get_a() { return a; }         }     }     namespace writable     {         public class : readonly.a, ia         {             public void set_a(double value) { base.a = value; }         }      } } 

suppose need class inherits , go ahead , define interface it:

namespace accesspattern {     namespace readonly     {         public interface ib : readonly.ia { int get_b(); }      }     namespace writable     {         public interface ib : readonly.ib, writable.ia { void set_b(int value); }     } } 

implementing not easy. 1 feels writable.b should inherit 2 base classes, writable.a , readonly.b, avoid repeated code.

is there recommended design pattern use? aim able return "read access only" , "read write access" objects separately (decided @ compile time) depending on requirements. nice if solution pattern makes easy add more layers of inheritance, classes c, d...

i know issue of multiple inheritance crops here , has been discussed @ length elsewhere in many, many, places. question not "how implement interfaces defined inside namespace accesspattern without using multiple inheritance" (although learn best way that) rather, how can define readonly/writable versions of class separately , support inheritance without getting very, very, messy?

for worth here 1 (messy) solution [see below better implementation]:

    namespace accesspattern     {         namespace readonly         {             public class : ia             {                 protected double a;                 public double get_a() { return a; }             }             public class b : ib             {                 protected int b;                 public int get_b() { return b; }             }         }         namespace writable         {             public class : readonly.a, ia             {                 public void set_a(double value) { base.a = value; }             }             public class b : readonly.b, ib             {                 private ia aobj;                 public double get_a() { return aobj.get_a(); }                 public void set_a(double value) { aobj.set_a(value); }                 public void set_b(int value) { base.b = value; }                 public b() { aobj = new a(); }             }         }     } } 

update: think (below) eugene talking about. implementation pattern pretty good, think. passing around "writeprotected" views of classes 1 can implement algorithms require state of class not change , use "writeenabled" views meant function will/could cause change in state avoiding.

namespace access {      // usual usage @ least readable     public interface ia { double get_a(); }     public interface ib : ia { int get_b(); }      // special usage writable     namespace writable     {         public interface ia : access.ia { void set_a(double value);  }         public interface ib : access.ib, ia { void set_b(int value);}     }      // implement whole of in 1 place     public class : writable.ia     {         private double a;         public double get_a() { return a; }         public void set_a(double value) { = value; }         public a() { }          //support write-protection         public static ia writeprotected() { return new a(); }         public static writable.ia writable() { return new a(); }     }     // implement whole of b in 1 place , no issue using base class     public class b : a, writable.ib     {         private int b;         public double get_b() { return b; }         public void set_b(int value) { b = value; }         public b() : base() { }          // support write protection          public static ib writeprotected() { return new b(); }         public static writable.ib writable() { return new b(); }     }      public static class test     {         static void dosomething(ia a)         {             // read-only         }         static void alterstate(writable.ib b)         {             // b writable         }         static void example()         {             // write protected             ia = access.a.writeprotected();             ib b = access.b.writeprotected();              // write enabled             writable.ia = access.a.writable();             writable.ib b = access.b.writable();              console.writeline(a.get_a());             b.set_b(68);              dosomething(a); // passed writeprotected             alterstate(b); // passed writable         }     } } 

i know thread 1 year old, i'm wondering if make sense have this:

 interface readonlya  {     object { get; }  }   interface writeablea : readonlya  {    new object {get; set;}  } 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -