I have three classes; Classes A and B both reference class C.
How can I make it so members of class C can be modified when referenced from class A but not modified when referenced from class B?
IE, the following should be possible;
classA myClassA = new classA();
myClassA.myClassC.IssueNumber = 3;
But this should not be possible;
classB myClassB = new classB();
myClassB.myClassC.IssueNumber = 3;
Making classB.classC read-only still allows properties of classC to be altered.
I’m sure this is basic stuff but can’t find a simple answer.
Thanks,
A
Pattern 1: Make a simple read-only interface
IRead. Make a simple write interfaceIWrite. Make an read-write interfaceIReadWrite : IRead, IWrite. ImplementclassC : IReadWrite. DeclaremyClassA.myClassCasIReadWrite. DeclaremyClassB.myClassCasIRead. (You don’t have to userIWriteanywhere if you don’t need it :-))Pattern 2: create a read-only wrapper for
classCcalledReadOnlyClassCand use that one inclassB.Pattern 1 is used by the IO streams to split the reader and writer implementations and then combine them in read-write streams.
Pattern 2 is used by the generic collections to provide read-only facet.