开发者

General Programming: When a set of objects are managed by another object, what do you call it?

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!


For your code samples, I would use

class my_object_manager {
    protected $my_object_type;

    function __construct($whatToManage) {
        $this->my_object_type = $whatToManage;
    }
}

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

$posts = new Posts(); //Where Posts() extends my_object_manager
$somePosts = $posts->getBy('author','baz');
foreach( $somePosts as $aPost ) {
    ...loop stuff here...
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜