通用编程:当一组对象由另一个对象管理时,您将其称为什么?
我正在寻求帮助来识别这种设计模式并学习它使用的“典型”词汇:
我正在使用 PHP 开发一个项目,并且我创建了一个薄 ORM 层,用于在数据库中保存通用对象。有两个类可以完成这项工作:
- “my_object”基本上是各种数据的容器。创建后,该对象可以将自身保存到数据库中。
- “my_object_manager”用于管理一组对象,例如,如果您想检索其中许多对象并迭代它们。
作为一个简化的示例,您可以执行以下操作:
$post = new my_object('post');
$post->title = 'foo';
$post->body = 'bar';
$post->author = 'baz';
...如果您想加载一堆帖子,您可以执行以下操作:
$posts = new my_object_manager('post');
$somePosts = $posts->getBy('author','baz');
foreach( $somePosts as $aPost ) {
...loop stuff here...
}
所以,我的问题是这样的:在“my_object_manager”的类定义中,我需要存储标识正在管理的对象类型的属性。它看起来像这样:
class my_object_manager {
protected $theKindOfObjectThatThisManages;
function __construct($whatToManage) {
$this->theKindOfObjectThisManages = $whatToManage;
}
}
现在,请原谅我不知道这种基本的东西,但我是自学的,并且编程词汇量非常有限。我确信这种设计模式很常见,但我一生都无法弄清楚它叫什么。
我正在尝试编写其他程序员可以阅读和理解的代码,所以,我真正的问题是,如果您正在阅读这段代码,您希望调用“$theKindOfObjectThatThisManages”什么?这种程序设计模式叫什么?如果你想让其他程序员知道它在做什么,你怎么称呼这种对象?
最后,问题编辑器弹出并告诉我这个问题看起来很主观,很可能会被关闭。事实上,我希望这个问题对于 Stack Overflow 来说是可以的 - 但如果不是,我可以在哪里提出这个问题并得到答案?
谢谢你!
I'm looking for help identifying this design pattern and learning the "typical" vocabulary it uses:
I'm working on a project in PHP, and I've created a thin ORM layer that saves generic objects to and from the database. There are two classes that do the work:
- "my_object" is basically a container for various kinds of data. After being created this object can save itself to the db.
- "my_object_manager" is used to manage a set of objects, for instance if you wanted to retrieve many of them and iterate through them.
As a simplified example, you could do something like:
$post = new my_object('post');
$post->title = 'foo';
$post->body = 'bar';
$post->author = 'baz';
...and you if you wanted to load a bunch of posts you could do something like:
$posts = new my_object_manager('post');
$somePosts = $posts->getBy('author','baz');
foreach( $somePosts as $aPost ) {
...loop stuff here...
}
So, my question is this: In the class definition for "my_object_manager" I need to store a property that identifies what kind of object is being managed. It looks something like this:
class my_object_manager {
protected $theKindOfObjectThatThisManages;
function __construct($whatToManage) {
$this->theKindOfObjectThisManages = $whatToManage;
}
}
Now, forgive me for not knowing this kind of basic stuff, but I'm self-taught and have a pretty limited programming vocabulary. I'm sure this kind of design pattern is common, but for the life of me I haven't been able to figure out what it's called.
I'm trying to write code that other programmers can read and understand, SO, my real question is if you were reading this code what would you expect "$theKindOfObjectThatThisManages" to be called? What is this program design pattern called, and what do you call this kind of an object if you want other programmers to know what it's doing?
Lastly, the question editor popped up and told me that this question looks subjective and is likely to be closed. I hope that this question is, in fact, ok for Stack Overflow - but if not, where could I ask this question and get an answer?
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于您的代码示例,我将使用
现在,您似乎正在密切关注 Active Record 模式,其中许多实现 存在,你可以去看看他们在实践中是怎么做的:)
通常,你不提供对 _manager() 对象的访问,而是让 my_object() 继承从它。所以,你会有类似的东西
For your code samples, I would use
Now, you seem to be following closely the Active Record pattern, of which many implementations exist, you can go see how they do it in practice :)
Usually, you don't provide access to _manager() objects, but make my_object()s inherit from it. So, you'd have something like
查看:
http://en.wikipedia.org/wiki/Design_pattern_(computer_science)
你的对象管理器的设计,取决于它的复杂程度,可能属于几种模式。例如:
复合:也许您的对象管理器允许您使用相同的接口同时对多个对象运行更新(典型的数据库场景)。您可以像更改集合一样更改单个对象。
外观:如果您的对象管理器提供了将系统的各个部分组合成一个更易于使用的界面的方法,那么它就使用了外观模式。例如,也许您的对象管理器创建一个“用户”对象,并通过单个 API 调用自动生成他们的第一个“帖子”。也许您可以自己在 post 和 user 对象上使用 create() 函数来完成此操作,但 Facade 模式将它们(也许还有助于处理并发问题)组合成一个更简单的 API,因为这些操作通常一起调用。
中介者/观察者:您的对象管理器可能负责观察对象的更改并处理它们之间的“中介”。您的对象管理器可能提供一种对帖子发表评论的方法。但是当调用此方法时,可能需要通过电子邮件通知帖子的作者。因此,您的对象管理器可以负责相关对象之间的通信,并通过发送事件或类似消息来通知侦听电子邮件服务。
Check out:
http://en.wikipedia.org/wiki/Design_pattern_(computer_science)
Your object manager's design, depending on how complex it gets, may fall under several patterns. For example:
Composite: Maybe your object manager allows you to run updates on multiple objects at once (typical database scenario) using the same interface. You can alter individual objects in the same way you'd alter collections.
Facade: If your object manager provides methods to combine various parts of your system into a single, easier to use interface then it's using the Facade pattern. For example, maybe your object manager creates a 'user' object and automatically generates their first 'post' via a single API call. Perhaps you could have used create() functions on the post and user objects to do this yourself, but the Facade pattern combines them (and perhaps helps to deal with concurrency issues as well) into an easier API since these operations are commonly called together.
Mediator/Observer: Your object manager might be responsible for observing changes to objects and handling the "mediation" between them. Your object manager might provide a method for commenting on a post. But when this method is called, the author of the post might need to be notified via email. So your object manager could be responsible for communicating between the relevant objects and notifying a listening email service by sending an event or similar message.