Home Tutorials Forums Articles Blogs Movies Library Employment Press Buy templates

Go Back   ActionScript.org Forums > General > Best Practices

Reply
 
Thread Tools Rate Thread Display Modes
Old 09-24-2008, 10:15 AM   #1
worthyashes
Registered User
 
Join Date: Jul 2006
Posts: 70
Default the ideal way of programming is to program to interfaces

Hi,

I've recently been doing some reading and have read about the ideal way of programming is to program to interfaces. Now this all sounds well and good, but I have to admit that I'm finding it quite hard. Even with good planning in advance I won't know all of the methods I'm going to use in my classes and I'm finding that as well as adding the methods to the class I'm amending the interface too. This all seems a bit backward to me so my quesiton is - am I getting the wrong end of the stick here or am I just not spending enough time planning to work out exactly what I need to achieve and therefore all of the required public methods for each class?

Cheers,

tim
worthyashes is offline   Reply With Quote
Old 09-24-2008, 03:28 PM   #2
newblack
dondeEstanMisPantalones?
 
newblack's Avatar
 
Join Date: Nov 2005
Location: New York Proper
Posts: 1,173
Send a message via AIM to newblack
Default

imho, interfaces are only worthwhile when you're needing to generalize something that may be implemented radically different from one type to another. if you're just writing out interfaces for data types that you end up simply subclassing, it become superfluous- the implementor's type wholly describes that API.
__________________
i am gibreel farishta
general relativity
jellytanks alpha redux
newblack is offline   Reply With Quote
Old 09-24-2008, 03:54 PM   #3
worthyashes
Registered User
 
Join Date: Jul 2006
Posts: 70
Default

Hi newblack,

I think I agree with you here. The only time I've used interfaces before has been to abstract a type where you may have a few different types of concrete versions of the classes that implement it, but you are using them in the same classes of your app, for example a MenuButton and a ImageButton could implement IMenuItem and then both be used in the Menu class.

Where I'm working at the moment they seem really keen on programing to interfaces though and whilst I understand that this exposes the API (if you don't have IDE that has code hints) in as much as you can read the Interface to see the public methods, and also that this could be a way of creating code where one developer may be working on one bit and another on a related part and they need to know their classes will be able to work together - so you could use and interface to create a 'contract' for the classes to adhere to.

But as a way of programing it feels a bit too prescriptive, I'd rather have good communication with the team as the development process goes on to talk about any tweaks or changes to the API as you go.

Anyway, cheers for your opinion - I think I agree with it.
worthyashes is offline   Reply With Quote
Old 09-24-2008, 05:15 PM   #4
maskedMan
Obfuscated Coder
 
maskedMan's Avatar
 
Join Date: Apr 2008
Posts: 698
Default

I know where you're coming from with this. I think that newblack has the right idea, judging from my experience.

Note however that you can "Program to Interfaces" without using the implements keyword. You could instead simply use an abstract class (in as much as a class can be abstract in AS3), and always type your interchangable objects as the abstract class. This way you always have a consistent interface as long as you aren't adding new public methods to the subclasses that the superclass protests against executing because it doesn't know of them.

For the most part, it seems to be just a difference of style. I think the only time I've found it truly helpful to implement Interface is when for example you desperately need to subclass something that does not already have a certain set of public functions and you need to ensure that those functions are available to your set of subclasses, whether they're implemented beyond empty curly braces in any particular subclass or not.
__________________
man.mask = mask_mc;

Sigh. The AS3 version just doesn't look at nice as
'man.setMask(mask_mc);'
maskedMan is offline   Reply With Quote
Old 09-24-2008, 10:19 PM   #5
worthyashes
Registered User
 
Join Date: Jul 2006
Posts: 70
Default

Arrrgh! Now you're raising the Inheritance / composition row too!!!!



I agree with you though - it's a style question rather than a right or wrong one......however as a freelancer you need to bend your style to the people you're working with, and lots of time you learn techniques that you've never tried or thought of so it's interesting to consider the different styles and see what 'works' best for you.

Thanks for the really intersting replies!
worthyashes is offline   Reply With Quote
Old 09-25-2008, 10:01 PM   #6
Assertnfailure
as[org].addListener(this)
 
Assertnfailure's Avatar
 
Join Date: Dec 2005
Location: LA, California
Posts: 838
Default

Yeah....it can be a little presumptuous to assume you wouldnt need an interface for something though. I've had projects where I polymorphed to abstract classes, and then found myself later needing to switch everything to an interface. Depending on the type of interactions you're looking for, it may or may not make sense.

For example, if you need to be able to add the object to the display list, then obviously an interface won't give you any advantages.

Edit: Oh yeah, forgot another huge plus for interfaces...
Interfaces are lighter weight than abstract classes, which comes in real handy when you need to abstract the document classes of external swf modules.

Last edited by Assertnfailure; 09-25-2008 at 10:30 PM..
Assertnfailure is offline   Reply With Quote
Old 09-26-2008, 04:46 PM   #7
maskedMan
Obfuscated Coder
 
maskedMan's Avatar
 
Join Date: Apr 2008
Posts: 698
Default

Perhaps I'm overlooking something glaringly obvious, but I've just encountered an instance where I find IInterface style programming useful but it is currently extremely cumbersome. Here's what I mean:

I have several subclasses of MovieClip that need to share an interface, but who also need to retain all the normal functionality of MovieClip. But IInterface style programming doesn't allow me to do this...

ActionScript Code:
var interface:IInterface = new InterfacedMovieClip();   this.addChild(interface);   interface.x = 100;   interface.y = 100;   interface.play();

Because this.addChild doesn't recognize my IInterface as a valid display object. Now I could solve that by casting IInterface as MovieClip like this...

ActionScript Code:
var interface:IInterface = new InterfacedMovieClip();   this.addChild(MovieClip(interface));   interface.x = 100;   interface.y = 100;   interface.play();

But then I still can't call .x, .y, or .play() on interface unless I declare those items specifically in the interface like so...

ActionScript Code:
public interface IInterface{     function get x():Number;     function set x(value:Number):void;     function get y():Number ;     function set y(value:Number):void;     function play():void;        //other IInterface functions below...   }

This is very irritating. Do you really have to explicitly declare in your interface all the movieclip functionality required to make your interface do movieclip-like things? Moreover, can you somehow make your interface compatible with the displayObject such that you can just simply add IInterfaces to the display list without casting as a MovieClip, or some other display object first?
__________________
man.mask = mask_mc;

Sigh. The AS3 version just doesn't look at nice as
'man.setMask(mask_mc);'
maskedMan is offline   Reply With Quote
Old 09-26-2008, 11:03 PM   #8
acolyte
>> RRRRAWR !! <<
 
acolyte's Avatar
 
Join Date: Feb 2005
Posts: 489
Default

Yo Masked Man ,

well i am relay not shure if i know what you mean but i think you mean Factory aka Virtual Constructor / where you are launching constructor via IFactory ?!?!?

i compiled an example

From Wiki :

The Factory Method pattern offers a solution. It encapsulates the knowledge of which Document subclass to create and moves this knowledge out of the framework.

http://en.wikipedia.org/wiki/Factory_method_pattern

http://en.wikipedia.org/wiki/Builder_pattern

sofar i only used interfaces like for example :
If i have a webservice building up on reflection (as/php) i am using my
interface for implementing the public function Methods reflecting to the php classes

Well its not necessary but its much more organized codestyle in my opinion i can send my Command.as Interface to my backendGuy and he knows exactly what methods to implement on the phpside and how many parameters the Methodes require.
ActionScript Code:
interface Command {     function get_folders(nr,nr0,nr1) : Void;     function get_content(nr) : Void;     function updateUser(arr,id) : Void; }
Attached Files
File Type: zip factory.zip (5.9 KB, 50 views)
__________________
>>I know my english sucks, ... but I speak german.
(this Signature was stolen from some french guy)
BTW i am no religiouse Buddy i just played to much Warcraft

Last edited by acolyte; 09-27-2008 at 02:27 PM..
acolyte is offline   Reply With Quote
Old 09-28-2008, 02:24 AM   #9
maskedMan
Obfuscated Coder
 
maskedMan's Avatar
 
Join Date: Apr 2008
Posts: 698
Default

Hi Acolyte,

The fla doesn't compile. It looks like you left out a "Factorys" class. But I think I get what you're going for here. I'm not sure it would solve my problem though, since the objects returned aren't DisplayObjects, nor do they implement any kind of DisplayObject interface that would let you use addChild to add them to the display list.
__________________
man.mask = mask_mc;

Sigh. The AS3 version just doesn't look at nice as
'man.setMask(mask_mc);'
maskedMan is offline   Reply With Quote
Old 09-29-2008, 09:41 AM   #10
acolyte
>> RRRRAWR !! <<
 
acolyte's Avatar
 
Join Date: Feb 2005
Posts: 489
Default

Quote:
Originally Posted by maskedMan View Post
Hi Acolyte,

The fla doesn't compile............
here is the factorys sorry :

ActionScript Code:
package {     import flash.display.Sprite;     import mat3d.dp.factory.*;     public class Factorys extends Sprite {         public function Factorys() {                         var factory : IFactory = new ChildFactory();             var newFactory : IFactory = factory.newInstance();             trace(newFactory.className);                         factory = new AnotherChildFactory();             newFactory = factory.newInstance();             trace(factory.className);         }     } }

ahh and yes thats why Factorys extends Sprite
__________________
>>I know my english sucks, ... but I speak german.
(this Signature was stolen from some french guy)
BTW i am no religiouse Buddy i just played to much Warcraft

Last edited by acolyte; 09-29-2008 at 11:48 AM..
acolyte is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Programming to interfaces rawmantick Best Practices 3 12-29-2008 04:40 PM
Programmer-Oriented Programming MichaelxxOA Best Practices 43 02-22-2008 09:54 AM
Senior Flash programmer in Quebec City Bettye Projects and Positions 1 01-30-2008 08:00 PM
Program to Interfaces over Implementations Flash Gordon Best Practices 8 09-03-2007 10:12 AM


All times are GMT. The time now is 10:20 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2009 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.