views:

62

answers:

3

I am using Unity3D for this question, but this is a general software engineering question.

Entities (interactive objects) in Unity3D are built with components or behaviors. By putting together behaviors together, you define the exact behavior of the object.

I am pondering how to have different objects react differently, when clicked, based a global state. For example, I have a tool that targets only affect circles in the scene; if I select that tool, and click on an object, and if it is a circle I will change it to a square. If it is not a circle, I will just ignore it.

Using the component-based entity design, I would define a behavior called IsCircle, and when it is clicked, I will check whether what should happen to it. However, what is the best way for it to access the global state of the entire application? Let's say I wish to avoid any switch, if-else and want the solution to be decoupled

(The problem is that the OnMouseDown() event handler does not pass in any parameters).

I would appreciate if the answer keeps in mind the environment I am using enforces the composite pattern.

A: 

Perhaps you can create a lookup table Dictionary<int, Func<int, TAction>> and when you create your objects you add the actions that are appropriate for that object. This way you avoid using a switch and do not have to modify the objects.

If you want something to manage state it self, there is a nice state machine implementation called stateless by Nicholas Blumhardt, creator of AutoFac. My answer to this SO question here describes the details.

David Robbins
+1  A: 

I am not sure if you can use this solution, but look at the State design pattern I is used to change behaviour of an object based on its state, so in you example, you would have to move the state to your objects. You can also consider Visitor design pattern and maybe some sort of multimethod.

Gabriel Ščerbák
A: 

Maybe something similar to Observer could help? All the components that needs to change their behavior based on global state register to an object like ScreenManager and whenever the global state changes you notify all the components that are registered at that moment of the change and they change their state accordingly.

derdo
No, actually he needs the state pattern, as Gabriel mentioned below. In fact the page for state pattern on Wikipedia contains an example with pens and drawing which is very similar to what you need.http://en.wikipedia.org/wiki/State_pattern
Ion Todirel
This situation is a little bit different than the Wikipedia example. Here behavior depends on the object that is being clicked too. If you inject the state into the tool, you still will end up with a lot of repeated if statements in those states to see what kind of an shape you are dealing with. With injecting the state through an observer like design into the shapes, you can collect all these if statements to one place and remove a lot of repeated if's. Whenever you change the tool you notify all the shapes and adjust their behavior at one point according to the active tool.
derdo

related questions