I am no expert in object or application design so I would like to ask for
some advice on a particular idea that I am throwing out there.
Orthogonality, as I have learned, is that between any n-number of objects,
if a change happens in one, it will not adversely affect the others. In
other words, low-coupling.
Can using Event-Dispatching/Event-Listeners be used wherever an interface
could be used to achieve this goal?
If so, can it be possible to create entire applications that are so
modularized that abstract typing with interfaces would be useless?
In other words, make a Flash application where the objects that make up the
system do not impose methods that an interface would traditionally enforce.
The advantage of going purely event-driven would mean developers would deal
less with implementing interface specifications and do event handling
instead.
An example of event driven system is the Yahoo! Maps API which expects
developers to create event handlers.
The following un-tested code snippets are attempts to illustrate the degree
of orthogonality I am talking about.
APPROACH I: No Interfaces
//MyInvokedClass.as
class MyInvokedClass
{
public function sayIt():Void
{
trace("Hello World");
}
}
// MyInvokingClass.as
import MyInvokedClass;
class MyInvokingClass
{
public var concrete:MyInvokedClass;
// Constructor
public function MyInvokingClass()
{
concrete = new MyInvokedClass();
}
public function sayIt():Void
{
concrete.sayIt();
}
}
// _root timeline
var tmp:MyInvokingClass = new MyInvokingClass();
tmp.sayIt(); // Output: "Hello World"
COMMENTS
- Good that it works today; potentially problems in the future for
modication/change.
- MyInvokingClass is dealing with a concerete instance.
- Classes are mildly coupled because it will involve some plumbing in
extending.
APPROACH II: Using Interfaces
//Invokable.as
interface Invokable
{
public function sayIt():Void;
}
//MyInvokedClass.as
import Invokable;
class MyInvokedClass implements Invokable
{
public function sayIt():Void
{
trace("Hello World");
}
}
// MyInvokingClass.as
import Invokable;
import MyInvokedClass;
class MyInvokingClass
{
public var concrete:Invokable;
// Constructor
public function MyInvokingClass()
{
concrete = new MyInvokingClass();
}
public function sayIt():Void
{
concrete.sayIt();
}
}
// _root timeline
var tmp:MyInvokingClass = new MyInvokingClass();
tmp.sayIt(); // Output: "Hello World"
COMMENTS:
- Lower coupling than before, this code is in better position for change in
the future
- MyInvokingClass is dealing with an abstraction rather than a concerete
instance.
- MyInvokingClass can freely use any object to do a sayIt() as long as that
object implements the Invokable interface.
APPROACH III: Using Flash Event Model and No Interfaces
// MyInvokingClass.as
import mx.events.EventDispatcher;
class MyInvokingClass
{
function addEventListener(){};
function removeEventListener(){};
function dispatchEvent(){};
public function MyInvokingClass()
{
EventDispatcher.initialize(this);
}
public function sayIt()
{
dispatchEvent({type:"onSayIt"});
}
}
// MyInvokedClass.as
class MyInvokedClass
{
function onSayIt():Void
{
trace("Hello World");
}
}
// _root timeline
var tmp:MyInvokingClass = new MyInvokingClass();
var tmp2:MyInvokedClass = new MyInvokedClass();
tmp.addEventListener("onSayIt", tmp2);
tmp.sayIt(); // Output: "Hello World"
COMMENTS:
- Is this the ultimate in decoupling? No dependencies between either classes
because an interface does not dictate what methods should be enforced.
- Considerably more lines of code but it serves a particular purpose.
- Very open ended. Developers need to know what the name of the event and
the content of the event payload.
- MyInvokingClass washes it hands clean of any external implementation. If a
change is made to an external implementation, like MyInvokedClass, it will
have no adverse effects on MyInvokingClass.
I hope this question is not too convoluted. The general point here is how to
maintain/achieve low-coupling and isolate the impact of change. If I am
mucking up the usage of interfaces and events, make me straight. I am under
the impression we have both to achieve decoupling.
On a side note, I think AS3.0 is going the pure Event Dispatching route
because I am seeing in the docs alot of components that have Event
Dispatching capabilities built in at a low-level in their object design.
Thanks in advance for any insight.
Lost + Confused,
-mL
http://knowledege.lapasa.net
_______________________________________________
[email protected]
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com