Showing posts with label Oop. Show all posts
Showing posts with label Oop. Show all posts

Tuesday, July 14, 2009

OOP is my friend

I have been building applications using more 'object oriented' techniques. An old dog like me who grew up coding basic, keying in machine code and the sort, may have trouble adapting to new methods.

After page upon page of confusing examples of inheritance, encapsulation, polymorphism and other oop terms, i decided to take some advice of a colleague and break it all down to the simplest parts. So I did and I think the fog is clearing a little concerning OOP.

Some tips about grasping oop:

1) Everything is an Object

2) Classes are blueprints

3) Extending a class opens up all of those methods to your new class, build on that foundation

4) An object does not exist util instantiated

5) OOP is a tool, it cannot replace procedural programming

OOP programming comes in many shades of gray, for this post lets consider that oop procedures are accomplished through the creation of Actionscript 3 class packages , hence objects. Each object is responsible for one thing, being itself.

Program for me a candle.

I would package an actionscript class called Candle . Within this actioncript file I would procedurally program the functionality of a candle. The candle should react to things just like a real one. Create a function
lightIt();
You call the function the candle brightens the room. The amount of functionality is totally up to you, the "object creator". There is no limit, your candle object could react to being left in the sun or even relight like a joke birthday candle. Just be sure that you don't go to far. Make the candle as basic as possible and extend it into new child classes, like a birthday candle class that extends Candle. You can always make objects more complicated , start simple.

Now store your candle in your special package directory and call upon it any time in the future when you need an object like a candle. Build upon objects, add functionallity simply and quickly, and then interact with other objects that you have created.

Tuesday, July 7, 2009

Object stuff

Just adding some of my daily epiphanies.

The ability of As3 to just assign description strings "I call them", to objects is as handy as a four armed butler. If you need some kind of switch in your program you don't necessarily need to declare a public variable to use as a switch. It is very simple to just add a string to an object. My object is Player. just add dot syntax and:

player.state = "off";

For the newbies you can add anything you want to your object, real time, like this:

player.happynessiocity = "groovy";

Makes for an easy conditional to check a condition, like this:

if(player.state == "off")
{
}


This is a bit of a hack and it throws a swf error but runs. Maybe will work in a pinch but here is a clever solution for a switch in a case where event flow won't allow you to access a listener somewhere. Just change the name of the button.

player.name = "off";

You are effectively negating listeners of parent display object containers elsewhere until you return the name to normal:

player.name = "playbutton";

Concerning functions, it is very handy to be able to send a function an object. You can then mass produce object modifications, like alpha, scale, with dot syntax. Just pass the function the object and then change it:
changeTheSize(myObject);

function changeTheSize(obj:myObject):void
{
obj.scaleX = 1.5;
obj.scaleY = 1.5;
}


You can create factories this way and just send in anything you want to make bigger, or whatever.

Oop

The ten main terms describing Object Oriented Programming:
1) Class -Defines the abstract characteristics of a thing (object), including the thing's characteristics (its attributes, fields or properties) and the thing's behaviors (the things it can do, or methods, operations or features).
2) Object -A pattern (exemplar) of a class. The class of Dog defines all possible dogs by listing the characteristics and behaviors they can have
3) Instance -One can have an instance of a class or a particular object. The instance is the actual object created at runtime.
4) Method -An object's abilities. In language, methods (sometimes referred to as "functions") are verbs. Lassie, being a Dog, has the ability to bark. So bark() is one of Lassie's methods.
5) Message passing -The process by which an object sends data to another object or asks the other object to invoke a method. Also known to some programming languages as interfacing. For example, the object called Breeder may tell the Lassie object to sit by passing a "sit" message
6) Inheritance -"Subclasses" are more specialized versions of a class, which inherit attributes and behaviors from their parent classes, and can introduce their own.For example, the class Dog might have sub-classes called Collie, Chihuahua, and GoldenRetriever.
7) Abstraction -Abstraction is simplifying complex reality by modelling classes appropriate to the problem, and working at the most appropriate level of inheritance for a given aspect of the problem.
8) Encapsulation -Encapsulation conceals the functional details of a class from objects that send messages to it.
9) Polymorphism -Polymorphism allows the programmer to treat derived class members just like their parent class' members.
10) Decoupling -Decoupling allows for the separation of object interactions from classes and inheritance into distinct layers of abstraction.
Above terms copied from wikipedia.org