I have a BoundingRectangle class, an Entity class and a World class. When an Entity 开发者_运维知识库is created, if it's a solid object, it registers a class called CollisionData that contains a BoundingRectangle and a reference to the owner Entity with the World class.
ie:
world.registerCollisionData(new CollisionData(this.boundingRectangle, this))
Is there a name for what the CollisionData is?
I think the pattern itself would be closest to Mediator Pattern. Instead of an Entity
communicating directly to another Entity
, the World acts as the mediator between those Entities
. CollisionData
is then just an abstraction of obtaining the data you'd need to handle that situation.
You could obtain a more pure result by providing a method in Entity
like:
public BoundingRectangle getBoundingRectangle();
And then, registering just the Entity
with the World
object:
world.registerEntity(this);
Then, the world simply uses entity.getBoundingRectangle();
for its work. This is also similar to the Registry Pattern. Your current way is quite acceptable, this just may make a cleaner Sequence Diagram of what's going on (no proxy object needed to tie the two together), and it couples only two classes rather than three.
I believe this is separation of concerns and simple encapsulation rather than any specific pattern. So CollisionData just encapsulates information about bounding rectangle and an underlying Entity, in this way Entity is not aware of Bound Region directly.
EDIT:
Just found something the same see ENCAPSULATED CONTEXT Pattern
It is called factory pattern.
http://en.wikipedia.org/wiki/Factory_method_pattern
I would say that CollisionData is either acting as a decorator or possibly part of a larger composite.
精彩评论