简体   繁体   中英

C# Design Problem Regarding Data Encapsulation

I have four classes:
1: one that owns the data
2: another that updates the data
3: third that is informed by the first about certain changes of the data
4: last that reads certain properties from the first class

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

More on the problem:
1st class is called Schema and it holds a counter of how many instances using that Schema there is.
2nd class is called Factory and it creates/deletes these instances, hence I need to update Schema instance counters and create new Schema objects when necessary.
3rd class is called Config and it holds various shared configurations including information about each new Schema object.
4th class is called View and it simply views Schema information.
Schema objects can be accessed by ID as they are held in a static list.

从过程代码切换到面向对象的代码,并将两个第一类合并为一个同时具有数据和行为的类。

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

When talking about 'oop', you would typically have a single class that describes an object. In your case, your object is 'the data'. Therefore your first class should be the data and encapsulate any operations on that data.

Classes describe objects - they are not collections of functions. It sounds like your second class is simply a collection of functions.

You will need to further describe your situation because it really doesn't explain much at the moment.

Not sure for the pattern (maybe this ?) name but basically you have:

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

2: another that updates the data

The 3rd class is basically your "client code" - code which consumes other code (in this case class 1) can be any class which subscribes to the events of the first class

EDIT : I think what you are describing might be close to the Visitor pattern.

You don't need a design pattern here. Just some principles.

Idea is simple:

object is defined by its behavior
object modifies only state of itself
object guards 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 relation between 2nd and 3rd class isn't that direct, You might want to use events for notifying:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM