Wednesday, February 2, 2005

Interfaces and why MM needs to take the tutorial

OK I don't have the time for a long blog entry and you don't have time to read it lol. So this is the first in a series of thoughts on why and how Flash needs to improve as a development platform.

No its not another MM components bash, anyway as a maker of Flash components ourselves we wouldn't have much credibility if all we did was bash them.

What I hope this is, is some serious, reasoned and constructive arguments about ways in which things could be improved for all of us.

As I have said before, with FMX2004 MM wear two hats, developers of an application platform and developers of a component set. If its easier to see it this way they are like MS, developer of Windows and developer of Office.

Many people have complained about the bloat of MM Components and a number of third party developers have stepped up to provide lighter alternatives but as some one commented recently its not difficult to make a lighter component, the question is not is it lighter but what functionality did you leave out to make it lighter.

In almost every case the answer includes Focus and Depth Management. To be honest I don't believe Focus or Depth management SHOULD be part of a component developers task, its a platform level task not component level.

Focus and Depth management in the player are broken or lack the features needed for real application development. To their credit MM realized this and fixed it in FMX2004. Unfortunately they didn't fix it in the player but instead fixed it in actionscript code but by itself that's not really so bad.

So why don't third party components make use of these fixes provided by MM?

Well the answer to that lies in the implementation. Taking FocusManager as an example, FocusManager imports the UIObect, UIComponent and SimpleButton classes directly and many other classes indirectly. These classes are basically the "core" of the component framework that you are trying to replace so you have basically shot yourself in the foot as your slim component is now even more bloated then the original component you are trying to replace! Not a good idea.

So here we see we have a key element at the platform level bringing in most of the component level classes. The question is why does it need to do that?

If you go through the code you can break the reasons down into two categories first as an identifying "tag" i.e. the FocusManager wants to only process certain object so it specifies that they must be UIComponents for example and secondly so it can call methods on objects, for example to call setFocus.

Ok fair enough that is needed and probably your first reaction is well ok how else could they have done that?

So the platform level is inextricably linked to the component level, you cant have one without the other and therefore third party developers have to leave it out.

But there is an alternative way that this could have been designed so that the two levels aren't linked;

By using Interfaces.

Using interfaces is almost second nature in Java, it is very common to find Interfaces being defined and then implemented in Java. Incredibly, with FMX2004, MM gave us Interfaces so we could do just that, and then basically threw them out the window and ignored them when it came to writing there own code.

So how might they be used?

Well first where a "tag" is needed, lets say you want to identify those objects that will come under your Focus Mangement, you can define an IFocusable interface. For a tag interface it needs not define any methods at all a class just implements it to sort of put its hand up and say "me to". In Java you can see this with things like the Serializable interface, its just says, ok save me.

Now the second part was where we need to actually execute methods but this is also straightforward we could simply add those methods to our IFocusable interface. So when I implement IFocusable I am saying I want to be managed and I know how to react to a setFocus call.

The FocusManager then doesn't care about what type of object you are just that you implement that interface. FocusManager has no link to the component level other than a small IFocusable interface class so now it happily can mange both the MMComponents and third party components without being bound directly to either of them.

As I mentioned FocusManger also includes the SimpleButton class (which derives from UIComponent). It does this because it has to implement a system default push button functionality and it needs to be able to execute an action when some user action results in a default "click". It might seem harder to follow but again here an interface solves the problem. We just define an IDefaultClick interface which defines the signatures for methods that the FocusManger needs to use, any class can implement that interface not just a SimpleButton and again everything works without mandating that only certain specific classes are acceptable.

Now before you worry that with the framework being already bloated the last thing we need is more classes, lets understand that interfaces don't contain code or method implementations, they simply contain definitions or signatures of methods. They are very light weight and probably would only add a few hundred bytes to your movie but in return you now have a way to integrate a wider range of components into your clips, ones that might indeed be lighter or have functionality that the standard set don't have and yet they could still provide the basic platform level functionality that any real application needs.


This only starts to scratch the surface of where interface's could be used to improve the current architecture and provide us all with a trully open development platform.

I hope also you can maybe start to see that interfaces could be a way to start to break down the "framework" as a single monolithic block and start to make at a series of interchangable services.

Anyway maybe more thoughts later when I get some time.

6 comments:

  1. Well,
    I've been bashing V2 since they first hit our hard-drives, i've found the whole process of learning them back to front to be an uphill battle all the way.
    I think the components aren't really "bloated" per say, i think they over-did the mix-in concept a little too much. Plus naming conventions were also their downfall.. (ie who knew you had to include UIComponentExtension for your custom component to work propertly (event wise etc).
    Ontop of that Documentation, there is none and the small snippets you do find in Flash Help are just that snippets of pointless information - it was as if the folks doing the docos considered it a tedious task that had to be done.
    Now, i do like the Focusmanager and Depthmanager, i've found them to be aok to use V2 framework aside.
    You can include the key elements of the framework into your custom sets, the taxing part comes into play when you utilise their controls and what not.
    Simply drag the Label component into your library (it will bring accross the core key ingredients) and build on from there.
    I've found that to be a smooth comprimise between having my own "lightweight" component set and still adhering to some nice concepts within Macromedias (ie Focusmanager, Depthmanager, Eventmanagers, Delegates etc).
    As for Interfacing? in the end how you approach your OO for "Management" is up to you, Interface in many ways is just that a manager of multiple objects that can be treated as if it were one?
    But i agree, i think a lot of this mix-in noise could been reduced with simple Interface approaches.

    ReplyDelete
  2. Maybe MM decided not to use Interfaces in the v2 framework as Flash Player 6 deals with them differently to Flash Player 7, such as in type checking.. Flash Player 7 allows you to do an "instanceof" with a new SomeInterface(), Flash Player 6 ignores the fact that an interface is a class in this case.
    So as the v2 components had to work in FP 6 perhaps it just wasn't worth the risk. Mix-ins seem to work against all the hard work that must have gone in to the framework tho :( but you can see why for Flash in particular, it was a sensible way to go.

    ReplyDelete
  3. Richard,
    Not so sure I buy that argument or at least not in the sense that backwards compatibility made it impossible to use Interfaces and it lets them of the hook.
    But as I said I dont really want to focus on why they might have made that choice, to some extent thats irrelevant, its the past and I more wanted to concentrate on what was needed for the future.
    At first MX2004 was pitched as where they bit the bullet to get us on the right track for application developement but in reality I feel it left us still looking to the next release to "get it right".
    Although I wanted to expand on this in detail later rather in a comment what I really want is a solid base to support developing sophisticated business applications. I want a powerful, flexible IDE (jsfl is a great start but it needs more and the IDE itself like the ComponentInspector for example needs a lot more sophistication to handle the things I want to do) and I want a clean class based language to build components with, supported by a powerful VM that provides the basic application services.

    ReplyDelete
  4. Not having mix-ins makes more work for yourself that a lot of times you don't have time to do. Dynamically adding functions using patterns like Decorator are pointless in lower-level OOP implementations such as inheritance. The whole point of being able to mix-in is flexibility, and freedom. Removing that freedom for the sake of "cleaner" code concepts doesn't help me do my job better or any faster. This is Rapid Application Development, not Java... hell, look at Java's feeble attempt to implement Functional Programming patterns, and not even fully at that, that Flash has had since version 5.
    ...I don't think, however, the above benefits of mix-ins were why they implemented them in the framework. I do agree, however, for the future, Interfaces would be great. Contracts that make sense and we can all abide by vs. "we just assume you implement a setSize method....".

    ReplyDelete
  5. Jester,
    Agree with your comment about interfaces but got to completely disagree on mixins.
    I really dont buy this extra work argument its at best short sighted and at worst a fallacy.
    There are alternatives to mixins that require very little extra work and the value of clean code should not be under estimated.
    Also mixins cant be overridden in sub classes.
    Anyway I will try to cover mixins next.
    Your comment about "its not Java" is interesting, another topic I want to touch on, seems to me Flash tends to sit in between the Java and MS worlds a foot in each camp and seems to be unsure about where it really belongs. I want to cover this when talking about properties and getter/setters.

    ReplyDelete
  6. Scott,
    Well not too much I can agree upon but thats not bad the world would be boring if we all had the same view.
    Adding FocusManger to a component that doesnt inherit from UIComponent adds 16k to its foot print that can be upwards of 70% of the size of a standard component. That is significant and it isn't becuase there is 16k of FocusManagement there, we implemented our own FocusManager in about 3k.
    The thing is in providing alternate components I dont want the core classes I want a working core platform but currently I cant have one without the other. A bad analogy might be if I am writing a database program for Windows and I have to include MSOffice as its the only way I can get the fixes to Windows.
    "in the end how you approach your OO for "Management" is up to you"
    Here I cant agree with you at all.
    I would say in "manager" type classes particularly when were talking about how the basic platform is architectured it simply cant be "up to you", this is one area where interfaces should almost be mandatory.

    ReplyDelete