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 10-21-2007, 05:39 AM   #1
Flash Gordon
rather be programming
 
Flash Gordon's Avatar
 
Join Date: Feb 2005
Location: City of Angels
Posts: 10,000
Default I got code duplication...




So let me explain my situation. I have to build 4 sites that will use very similar features. Within those 4 sites, there will be several galleries for each site, say 8 galleries. Each gallery will have a linkage, and will look and function slightly different. However, within a single site each image transition will be the same for each gallery. The image transition is only different from site to site. So I have 4 sites, and each site has 1 transition associated. Each site as 8 galleries that use the 1 transition.

Ok so my problem is, I have a bunch of code duplication. Each GalleryModuleX is a linkage class that basically sets the specific properties for the superclass. GalleryModuleX is specific to each site, where as the other class provide a framework. The problem is each GalleryModuleX classes are using the same code over and over and I don't know how to get rid of it. I keep copying and pasting this stuff.
ActionScript Code:
protected function imageClose(e:Event):void {   // performs an image effect    }                 /**  * Override the initial loading of the first image  */ override protected function mainLoaded(e:Event=null):void {    // here I'm overriding the default behavior for the BasicGallery    // so that the specific effect is set up }         override protected function center(obj:DisplayObject):void {    // center the effect }                 /**  * Override the onComplete method to make the new picture wait for the new animation to be complete  */ override protected function onComplete(e:PreloaderEvent):void {    // do the opening transition when picture loads }

Since I'm overriding method, how do I encapsulated this code so I only have to write it once?
__________________
I'm old enough to know better and young enough to do it anyway. -- maskedman

Last edited by Flash Gordon; 10-21-2007 at 05:45 AM..
Flash Gordon is offline   Reply With Quote
Old 10-21-2007, 06:07 PM   #2
Assertnfailure
as[org].addListener(this)
 
Assertnfailure's Avatar
 
Join Date: Dec 2005
Location: LA, California
Posts: 838
Default

So the only reason you're subclassing is to apply transition in/out effects?

If you want to template recurring logic, you should just use an abstract class as the super class.

Last edited by Assertnfailure; 10-21-2007 at 06:09 PM..
Assertnfailure is offline   Reply With Quote
Old 10-21-2007, 08:20 PM   #3
Flash Gordon
rather be programming
 
Flash Gordon's Avatar
 
Join Date: Feb 2005
Location: City of Angels
Posts: 10,000
Default

Thanks for the reply. What is happening is BasicGallery and GalleryX are class that take care of all the gallery functionality for each site. It has about 10 properties that are instantiated but undefined. "GalleryModuleX" just defines the properties and "GalleryModuleX" gets linked to in the Flash GUI. Does that make sense?

So I have BasicModule, BasicGallery, and GalleryX all in a com.framework.modules.galleries package and they are all abstracts. Where GalleryModuleX subclasses the framework but is specific to each site. mysite.modules.galleries package.

I'm not sure where in the chain I could add the "abstract class as the super class" that you are referring to.
__________________
I'm old enough to know better and young enough to do it anyway. -- maskedman

Last edited by Flash Gordon; 10-21-2007 at 08:31 PM..
Flash Gordon is offline   Reply With Quote
Old 11-07-2007, 09:17 PM   #4
Flash Gordon
rather be programming
 
Flash Gordon's Avatar
 
Join Date: Feb 2005
Location: City of Angels
Posts: 10,000
Default

anymore thoughts out there?
__________________
I'm old enough to know better and young enough to do it anyway. -- maskedman
Flash Gordon is offline   Reply With Quote
Old 11-24-2007, 07:45 PM   #5
Assertnfailure
as[org].addListener(this)
 
Assertnfailure's Avatar
 
Join Date: Dec 2005
Location: LA, California
Posts: 838
Default

I know this is a little late, but.....

From what it sounds like to me, you are already using abstract classes in a template pattern arrangement (BasicGallery). Why not insert the common logic into BasicGallery, or insert another abstract class into the inheritance change?
Assertnfailure is offline   Reply With Quote
Old 11-24-2007, 10:06 PM   #6
Flash Gordon
rather be programming
 
Flash Gordon's Avatar
 
Join Date: Feb 2005
Location: City of Angels
Posts: 10,000
Default

it's never late to learn

I actually haven't done much with it since then, but I think today I'm going to try and redo the structure to make it use composition rather than inheritance.

The reason why I can't I can't "insert the common logic into BasicGallery" (at least in my mind) is that BasicGallery belongs to the "framework" as where "GalleryModule1" belong to the specific site. Each site has it's own GalleryModule1 but all sites share the BasicGallery. Since GalleryModule1 is inheriting from BasicGallery I can't make it inheritant at the site level since it is inheriting at the framework level....tough to explain without see it.

I'll give it a shot with composition and post back my results.

Thanks again for your valuable input. I always learn something from you posts!
__________________
I'm old enough to know better and young enough to do it anyway. -- maskedman
Flash Gordon is offline   Reply With Quote
Old 11-30-2007, 11:05 PM   #7
Flash Gordon
rather be programming
 
Flash Gordon's Avatar
 
Join Date: Feb 2005
Location: City of Angels
Posts: 10,000
Default

update:

It nearly a complete restructure. In stead of templating the classes, I'm using composition and the Gallery1, GalleryX have been a storage facility for the data and doesn't direct the displaying of the data. That is handled at the GalleryModule level. This looks very promising, and it allows me to have inhertiance where i need it.
__________________
I'm old enough to know better and young enough to do it anyway. -- maskedman
Flash Gordon is offline   Reply With Quote
Old 12-05-2007, 12:47 AM   #8
Flash Gordon
rather be programming
 
Flash Gordon's Avatar
 
Join Date: Feb 2005
Location: City of Angels
Posts: 10,000
Default

It actually worked out really well. I wish I could subclass a "template" class and have that superclass have acess to my linkage movieclip.
__________________
I'm old enough to know better and young enough to do it anyway. -- maskedman
Flash Gordon is offline   Reply With Quote
Old 12-10-2007, 08:21 PM   #9
Assertnfailure
as[org].addListener(this)
 
Assertnfailure's Avatar
 
Join Date: Dec 2005
Location: LA, California
Posts: 838
Default

Quote:
Originally Posted by Flash Gordon View Post
It actually worked out really well. I wish I could subclass a "template" class and have that superclass have acess to my linkage movieclip.
Why not?

ActionScript Code:
class SuperClass{ private var instance:SuperClass; public function SuperClass(symbol:Class){     instance = new symbol(); } }

ActionScript Code:
class SubClass extends SuperClass{ public function SubClass(){     super(SubClassSymbol); } }

Something like that? I haven't really worked in Flash CS3 in a while, so I might not be thinking the same thing.
Assertnfailure is offline   Reply With Quote
Old 12-10-2007, 08:33 PM   #10
Flash Gordon
rather be programming
 
Flash Gordon's Avatar
 
Join Date: Feb 2005
Location: City of Angels
Posts: 10,000
Default

Some ma na b**ch....

Thanks dude. This is extacly what I needed.
ActionScript Code:
package {     import flash.display.MovieClip;         public class SuperClass extends MovieClip     {         private var _subclass:MovieClip;                 public function SuperClass(subclass:MovieClip)         {             _subclass = subclass;         }                 public function hideOval():void         {             _subclass.oval.visible = false; // where oval is a displayobject inside of subclass and subclass linkage is set in library         }     } }
ActionScript Code:
package {     import flash.display.MovieClip;         public class SubClass extends SuperClass     {         public function SubClass()         {             super(this);         }     } }
ActionScript Code:
// in fla scene.addEventListener(MouseEvent.CLICK, onClick); function onClick(e:MouseEvent):void {     scene.hideOval(); }

I think that right there has been my problem all a long!!!!
Attached Files
File Type: zip template SuperClass.zip (6.3 KB, 138 views)
__________________
I'm old enough to know better and young enough to do it anyway. -- maskedman

Last edited by Flash Gordon; 12-10-2007 at 08:50 PM..
Flash Gordon 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
help me not duplicate code on Seanx ActionScript 2.0 10 07-19-2006 04:23 PM
Persistant Code Problem CisTrans ActionScript 1.0 (and below) 0 06-11-2006 06:25 AM
GREATEST CODE EVER! (maybe) - Always get desired FPS in browser jasonoda ActionScript 1.0 (and below) 9 06-01-2006 11:57 PM
Moving a cursor - Need help with my code e39m5 ActionScript 1.0 (and below) 2 01-11-2006 09:20 PM
code not working jaredly ActionScript 1.0 (and below) 6 06-05-2002 08:56 PM


All times are GMT. The time now is 10:28 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.