C # design problem regarding data encapsulation

I have four classes:
1: one that owns the data
2: another that updates data
3: the third, as reported first by some data changes
4: the last one that reads certain properties from the first class

I do not want any other class than the second to be able to update data.
So what is best for use here?

More about the problem:
The 1st class is called Schema, and it contains a counter of the number of instances using this scheme.
The second class is called Factory, and it creates / deletes these instances, so I need to update the counters of the Schema instances and create new Schema objects when necessary.
The 3rd class is called Config and contains various common configurations, including information about each new Schema object.
4th grade is called View, and it just looks at the schema information.
Schema objects can be accessed by identifier because they are stored in a static list.

+7
source share
4 answers

Switching from procedural code to object-oriented code and combining the first two classes into one class that has both data and behavior.

+3
source

This is a very abstract description of your scenario, and it is very difficult to see your problem and what you want to do.

Speaking of 'oop', you usually have one class that describes an object. In your case, your object is "data." Therefore, your first class must be data and encapsulate any operations on this data.

Classes describe objects — they are not feature sets. It looks like your second class is just a bunch of functions.

You will need to describe your situation in more detail, because at the moment this really does not explain.

0
source

Not sure about the template (maybe this ?), But basically you have:

1: one that owns the data (fire events when data changes)

2: another that updates data

The third class is basically your "client code" - the code that consumes another code (in this case class 1) can be any class that subscribes to events of the first class

EDIT . I think that what you are describing may be close to the Visitor pattern. p>

0
source

Here you do not need a design template. Just a few principles.

The idea is simple:

An object

determined by his behavior
the object changes only the state of itself the object protects its state

public class SecondClass{ public FirstClass First{get;private set;} public ThirdClass Third{get; private set;} public void DoSomething(){ First.Something++; Second.NotifySomethingHasBeenDone(); } } 

If the relationship between the second and third classes is not direct, you can use events to notify:

 public class SecondClass{ public FirstClass First{get;private set;} public void DoSomething(){ First.Something++; RaiseEvent<SomethingHasBeenDone>(this); } } public class ThirdClass:IHandles<SomethingHasBeenDone>{ public void Handle(SomethingHasBeenDone @event){ MessageBox("First has {0} something".With(@event.First.Something)); } } 
0
source

All Articles