PDA

View Full Version : OOP



Garfty
September 12th, 2008, 09:54 AM
Hi all,

Hope everyone is well, i'm looking at separating out parts of a huge document class and making things more and more re-usable.

I'm looking at having classes for things such as building a quick custom context menu and full screen mode, and many other things that i think would be beneficial.

Is it possible to extend the Document class and have a custom context menu built? also is this beneficial in any way or am i just being anally retentive with my code?

Any help is appreciated thanks

Krilnon
September 12th, 2008, 10:21 AM
It's possible to extend the document class if you've defined it in an external class file (but not by using include), but I don't think that it's possible when the document class is generated at compile time.

I doubt that there are many situations in which it would be a good idea to have any class used in your document class also be subclasses of your document class, since creating an instance of that subclass would create another instance of your document class…

Digitalosophy
September 12th, 2008, 11:34 PM
You should have your document class create a new custom menu class.

Something like this

Document Class




package {

import flash.display.*;
import CustomMenu;

public class DocClass extends Sprite {

private var _CustomMenu:CustomMenu;

public function DocClass(){

init();

}

private function init(): void {

_CustomMenu = new CustomMenu();
_CustomMenu.init();

}


}

}




CustomMenu.as




package {

import flash.display.*;

public class CustomMenu extends Sprite {


public function CustomMenu(){


}

public function init(): void {

trace("Add you custom menu in this class");

}


}

}



So the code that actually creates the custom menu should be written in the custom menu class.

Garfty
September 15th, 2008, 05:44 AM
Thank you for this help I now have this working and it seems to be making my projects more manageable.

Is this good practice or am i being obsessive?

thank you Digitalosophy

ViktorHesselbom
September 15th, 2008, 10:01 AM
Is this good practice or am i being obsessive?
THis is extremely good practice since this is basically how most other languages work. ;)

Digitalosophy
September 15th, 2008, 10:23 AM
No problem man, yup this is the best way to do it.


Thank you for this help I now have this working and it seems to be making my projects more manageable.

Is this good practice or am i being obsessive?

thank you Digitalosophy

Garfty
September 16th, 2008, 06:28 PM
Cheers,

looking at this and i'll post my working code up tomorrow when I get chance, could you also not extend the ContextMenu class??

Would this be as good, or are there more errors depending on which path you take??

Im obsessive about tidyness, so i think im striving for ultimate clean code haha

Again cheers for all your help on this, After reading Learning ActionScript 3.0 and reading the OOP section over and over I think its a wide open area for certain things, with so many questions about which path to take for certain areas of a project or a whole project.

Krilnon
September 16th, 2008, 08:02 PM
No, ContextMenu is declared as final, so you can't subclass it.

Garfty
September 17th, 2008, 03:37 AM
Surely it is possible as shown here...

Class Extends ContextMenu (http://www.bigroom.co.uk/blog/custom-context-menu-with-keyboard-shortcuts-in-flash)

Krilnon
September 17th, 2008, 08:50 AM
That code is not AS3.

http://livedocs.adobe.com/flex/3/langref/flash/ui/ContextMenu.html
public final class ContextMenu

I wouldn't have posted if I hadn't looked at some reliable source (although they are not completely reliable) or tested something myself.

ViktorHesselbom
September 17th, 2008, 09:30 AM
I wouldn't have posted if I hadn't looked at some reliable source (although they are not completely reliable) or tested something myself.
What do you mean? When is it not reliable? I think I've missed seomthing here...

Krilnon
September 17th, 2008, 11:03 AM
Oh, I just meant that sometimes there are human errors like typos or incorrect statements in the documentation… Plenty of people have run into such errors. However, I think that whether or not a class shows up as final in the class signature that I quoted is probably determined from actual code somewhere, so it's fairly unlikely that that bit is incorrect (and you can try to extend ContextMenu yourself and see the error that is generated).

Garfty
September 18th, 2008, 04:35 AM
Ok, I've set up a new blog, As i think it would be good for me to share things that I find out.

Once i've got one sorted im going to post up the code for the personalised context menu and pass the link through.

If the contextMenu class is final then there is no chance of extending it, is this a new thing in AS3?

Digitalosophy
September 18th, 2008, 03:38 PM
What exactly do you need to do Garfty? I'm just wondering why you need to extend that class?

Garfty
September 22nd, 2008, 06:39 AM
It's not specifically that I need to do anything, its just generally for good practice and learning.

Beats having a huge document class...

JonnyR
September 22nd, 2008, 09:03 AM
Garfty, consider using composition over inheritance. (http://brighton.ncsa.uiuc.edu/prajlich/T/node14.html)

Garfty
September 22nd, 2008, 10:27 AM
Thanks JonnyR i'll take a look at this tonight,

I also had a look at your profile and blog, some nice work :)

If I was to pass you on my inheritance version would you be able to help explaining a composition version??

This way i can get to learn features of both

JonnyR
September 22nd, 2008, 04:38 PM
Hi Garfty,

I'm not sure that me examining your code will necessarily help as Composition is a very simple concept to grasp (infact, you probably use it already!) The main thing to remember is the type of relationship between two classes:

IS-A: Inheritance, ie: A Car is A Vehicle in the same way a MotorBike is A Vehicle
HAS-A: Composition, ie: A Car HAS An engine, in the same way a MotorBike HAS An Engine

An in this case, you would probably use both inheritance AND composition, you would probably make the Vehicle Base Class make use of an Engine Object (composition) which both the Motorbike and Car Classes can make use of by extending the vehicle baseclass (inheritance).

Engine.as


class Engine
{
private var horsePower : uint;

// Constructor.
public function Engine(horsePower : uint)
{
this.horsePower = horsePower;
}

// Makes the engine work, returns the amount of power generated.
public function work(amountOfThrotteApplied : uint) : uint
{
var work : uint = (amountOfThrotteApplied * this.horsePower);
trace ("BRUUUUMMM!!!!!! Created: " + work + " joules!");
return work;
}
}


Vehicle.as


class Vehicle
{
// This is composition in effect!
protected var engine : Engine;

// this is another bit of composition - each vehicle has a petrol tank
// (Class not shown).
protected var petrolTank : PetrolTank

// and this is a boring memeber variable.
protected var numWheels : uint;


// Constructor.
public function Vehicle(numWheels, horsePower : uint, tankSize : uint)
{
this.numWheels = numWheels;

this.engine = new Engine(horsePower);
this.petrolTank = new PetrolTank(capactiy);
}


// All vehicles can accelerate
public function accelerate(amountOfThrotteApplied : uint) : void
{
// Get our engine to do some work
var joules : uint = this.engine.work(amountOfThrotteApplied);

// and now pass that info onto the Petrol Tank
this.petrolTank.burnFuel(joules);

// Notice how this method does some pretty complex stuff, but all the
// functionality is encapsualted away in it's own respective class - this
// is the true benefit of Composition!
}
}


Car.as


class Car extends vehicle
{
// Now our Car class is very simple as it gain both a Petrol Tank and an
// Engine from the Car Base class - we can also hard-code how many wheels
// a car will have - this is a good use of Inheritance!
public function Car(horsePower : uint, tankSize)
{
// cars ALWAYS have 4 wheels!
super(4, horsePower, tankSize);
}

// And we get to make sue of the accelerate method without even doing anything!
}

Krilnon
September 22nd, 2008, 04:50 PM
Composition is a very simple concept to grasp (infact, you probably use it already!)

I agree. I remember that senocular mentioned it in one of my threads once and it's only confusing if you haven't heard the term before.

Garfty
September 23rd, 2008, 04:42 AM
Thanks, after looking I guess I do use Composition a lot, however for making things like a generic context menu class I think Inheritance would be a better option?? ( please feel free to correct me, im just learning )

Thank you to both of you for being such good help.

hobbbz
September 23rd, 2008, 04:43 PM
You don't need to extend it but you could build a helper class that sets all the values you want more efficiently