开发者

I need a Design Pattern to solve this problem

I have a Model class that represents a 3D Player for example.

A Model consists of one or many Components for the base. For example a Component could represent a shape for the Torso. (Components are stored in a List within the Model object)

When we add components to Model we get a tree like structure of objects being formed, as each Component has its own List of child Components for example a Component that represents a Torso can have many child Components namely 2 separate upper arm Components, 2 separate upper leg Components and a neck Component. The neck component then has a reference to a head Component.

This in effect creates a hierarchy of Component objects.

Brief: The application requires the ability to edit and delete components freely. Since they can be interconnected to form a hierarchy, as discussed above, how can a parent component know about changes to any of its child components?

Anyone have an idea what Design Pattern tackles this开发者_如何学Python problem?


You can use simple observer pattern where you parent components are observing their children.

If your programming language supports event notifications you can expose an event from the Child which is raised when changes are made to it. When the child is added to the Parent, the Parent subscribes to relevant events and gets notified when changes happen. When the child is removed the parent unsubscribes.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜