PDA

View Full Version : Extending a basic framework for specific needs.


Flash Gordon
09-15-2007, 10:10 PM
Do you guys see anything wrong with this? Or more importantly, is it a correct OO implementation?

Basic frame work to be extended for specific needs:

package com.as.ui
{

public class Window extends Sprite
{
// define in subclass
protected var _title:TextField;
protected var _mainTxt:TextField;
protected var _mask:Sprite;
protected var _txtContainer:Sprite;
protected var _handle:Sprite;
protected var _guide:Sprite;
protected var _close:SimpleButton;
protected var _drag:SimpleButton;

// used in this class
protected var _scrollbar:CustomScrollBar = new CustomScrollBar();
protected var _starts:Dictionary = new Dictionary();


public function set title(v:String):void { _title.htmlText = v; }
public function set mainTxt(v:String):void
{
_mainTxt.condenseWhite = true;
_mainTxt.htmlText = v;
_mainTxt.autoSize = TextFieldAutoSize.LEFT;

_txtContainer.y = _starts[_txtContainer];
if (_handle != null)
{
_handle.y = _starts[_handle];
_scrollbar.showGear();
}

}

public function Window()
{
//
}

protected function init():void
{
// set up basic scene and properties needed
_txtContainer.mask = _mask;
_starts[_handle] = _handle.y;
_starts[_txtContainer] = _txtContainer.y;

// configure scene
_title.mouseEnabled = false;
if (_close != null) _close.addEventListener(MouseEvent.CLICK, closeClick);
if (_drag != null)
{
_drag.useHandCursor = false;
_drag.addEventListener(MouseEvent.MOUSE_DOWN, dragPress);
_drag.addEventListener(MouseEvent.MOUSE_UP, dragRelease);
}


// set up scrolling
if (_handle != null) _scrollbar.assets(_txtContainer, _mask, _handle);
if (_guide != null) _scrollbar.addGuide(_guide);
}

protected function closeClick(e:MouseEvent):void
{
this.visible = false;
}

protected function dragPress(e:MouseEvent):void
{
this.startDrag(false);
this.stage.addEventListener(MouseEvent.MOUSE_UP, dragRelease);
}

protected function dragRelease(e:MouseEvent):void
{
this.stopDrag();
this.stage.removeEventListener(MouseEvent.MOUSE_UP , dragRelease);
}
}
}

The specific implementation:

package template.ui
{
import com.as.ui.Window;

public class Window extends com.as.ui.Window
{

public function Window()
{
_title = title_txt;
_mainTxt = content_mc.main_txt;
_mask = mask_mc;
_txtContainer = content_mc;
_handle = handle_mc;
_guide = guide_mc;
_close = close_btn;
_drag = drag_btn

init();
}
}
}


I have a lot of site that want a window. However, each site wants its own window. I think by extending a basic window, I'm allowing for more flexibility and customization.

Am I breaking an OO principles here?

P.S. I'm doing the same thing for a lot of other components as well. So while this question is rather basic, it applies to my whole framework of sites.

Assertnfailure
09-16-2007, 07:38 AM
I hate using _ prefix for variables in any namespace besides private. It just looks kind of dirty to me when it's something that is exposed outside of your class.

Additionally, properties like title probably don't have to be listed as protected at all, because you've got a public setter for it.

You're not breaking principles, though. You're using abstract base classes that act as templates to your custom window subclasses.

Flash Gordon
09-17-2007, 12:13 AM
Great! Thanks for the suggestions too.

I was a little concerned because I'm using a lot of inheritance. Some of them get pretty deep:
Info2 -> Info1 -> BasicInfo -> BasicModule ->IModule

I appreciate your input. I would have built the whole thing unsure.

:)