I'm writing a game where the mouse-driven controller object clicks on a player object to have it do something.
There are 2 ways of initiating the interaction between mouse and player:
My dilemma here is that the first option seems more intuitive with how I imagine the scenario happening in the real world, but the second option seems more intuitive with proper object-oriented design because it doesn't require looking into another object's property, which violates encapsulation to some extent (the controller must look into the player to read its "clickable" property). Also, the second option seems inline with the 'Controller' design pattern.
This is always a struggle for me -- do I defy proper object-oriented design (e.g. option 1) or do I use an implementation that seems counterintuitive to the real world (e.g. option 2)?
I'm hoping there's some kind of middle ground I'm missing.
Object-Oriented Computers, or OOPs concepts with real time examples, refer to programming languages that make use of objects. Inheritance, hiding, polymorphism, and other real-world concepts are all part of object-oriented programming.
Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data and code: data in the form of fields (often known as attributes or properties), and code, in the form of procedures (often known as methods).
Abstraction is an OOPS concept to construct the structure of the real world objects.
Yes, OOP is more closer to real world problems because object oriented programming implement inheritance by allowing one class to inherit from another. Thus a model developed by languages is much closer to the real world.
This is always a struggle for me -- do I defy proper object-oriented design (e.g. option 1) or do I use an implementation that seems counterintuitive to the real world (e.g. option 2)?
I don't think that the purpose of object-orientation is to model the real world.
I think that a (the?) reason why an OO model often follows a model of the real-world is that the real-world doesn't change much: and therefore choosing the real world as the model means that the software won't change much, i.e. it will be inexpensive to maintain.
Fidelity to the real world isn't a design goal in itself: instead you should be trying for designs which maximize other metrics, e.g. simplicity.
The 'objects' in 'object orientation' are software objects, not necessarily real-world objects.
Why not go with Option 3, which is simlar to Option 1?
The second method is the decidedly more idiomatic Flash way of handing things. AS3 has the event model build right into EventDispatcher
and all DisplayObjects
inherit from it. That means any Bitmap
, Sprite
or MovieClip
immediately knows if it is clicked.
Think of the Flash Player as your controller. When I do MVC in Flash I almost never write a controller because the Flash Player does it for you. You're wasting cycles determining what was clicked on when the Flash Player already knows.
var s:Sprite = new Sprite();
s.addEventListener(MouseEvent.CLICK, handleMouseClick);
function handleMouseClick(event:MouseEvent):void
{
// do what you want when s is clicked
}
I'd probably not directly access a controller from inside a sprite (probably in the view). Instead I would dispatch an event (probably a specific custom event that matched the circumstance). Make your decisions on how many times per-frame something happens. Responding to a user interaction (e.g. mouse-click) usually gives you the freedom not to worry about the overhead in event systems.
Finally - the reason I suggest this has nothing to do with some Ivory Tower concept of OOD or OOP. Principles such as these exist to help you not constrain you. When it comes down to a matter of pragmatics, go with the easiest solution that won't cause you headaches down the line. Sometimes that means doing OOP, sometimes it means functional, sometimes it means imperative.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With