关于数据封装的C#设计问题
我有四门课:
1:拥有数据的人
2:另一个更新数据
3:第三个由第一个通知有关数据的某些变化
4:最后一个从第一个类读取某些属性
我不想要任何其他类,但第二个类能够更新数据。
那么这里使用的最佳设计模式是什么?
有关问题的更多信息:
第一类称为 Schema,它包含一个计数器,用于显示有多少实例使用该 Schema。
第二类称为 Factory,它创建/删除这些实例,因此我需要更新 Schema 实例计数器并在必要时创建新的 Schema 对象。
第三类称为 Config,它保存各种共享配置,包括有关每个新 Schema 对象的信息。
第四类称为 View,它只是查看 Schema 信息。
架构对象可以通过 ID 进行访问,因为它们保存在静态列表中。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
从过程代码切换到面向对象代码,并将前两个类合并为一个具有数据和行为的类。
Switch from procedural code to object-oriented code and merge the two first classes into a single class that has both data and behavior.
这是对您的场景的非常抽象的描述,使得很难了解您的问题和您想要做什么。
当谈论“oop”时,通常会有一个描述对象的类。就您而言,您的对象是“数据”。因此,您的第一个类应该是数据并封装对该数据的任何操作。
类描述对象——它们不是函数的集合。听起来你的第二个类只是函数的集合。
您需要进一步描述您的情况,因为目前确实无法解释太多。
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.
这里不需要设计模式。只是一些原则。
想法很简单:
对象由其行为定义
对象仅修改其自身的状态
对象保护其状态
如果第二类和第三类之间的关系不是那么直接,您可能需要使用事件来通知:
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
If relation between 2nd and 3rd class isn't that direct, You might want to use events for notifying:
不确定模式(也许这个?)名称,但是基本上你有:
1:一个拥有数据(数据更改时触发事件)
2:另一个更新数据
第三类基本上是你的“客户端代码” - 消耗其他代码的代码(在本例中为类 1)可以是订阅第一类事件的任何类
编辑:我认为您所描述的内容可能接近访客 模式。
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.