Adobe Max 2007 – Chicago
Peter Elst – OOP for AS3
Myth – Do you need to use OOP if you are a lone programmer? YES
Concepts of OOP
- Classes
- Inheritence
- Encapsulation
- Polymorphism
- Interfaces
Introducing Design Patterns
What is OOP?
OOP structures code into reusable units that provide a blueprint for any number of instances that are created from it.
In OOP there are
Class = Blueprint
Object = House
What’s new in AS3?
- Display List API
- DOM3 Event Model
- ECMScript for XML(E4X) – easy way to query XML
- Runtime Error Checking – Prevents you from making major mistakes
- Regular Expressions –
- Method Closures
- Protected Scope
- etc…
Classes
- Classes describe an object using properties and methods
- Anynumber of instnace can be created from
Inheritance
- Allows you to extend classes, propeties of a super class(i.e. the clas which to extend
- AS3 classes require the override keyword to override methods or por
- The final keywords disallows for any extending of classes
Inheritance vs. Composiion
- Composition does not extend a class but instantiates it at runtime.
- Inheritance is used for an ‘is a’ relationship, whereas composition typically implies a ‘has a’ relationship
- Composition gives control over creation and destruction and garbage collections
Encapsulation
- Allows you to protect the inner workings of a class through the use of access modifiers
- Provide an API to interact with your class through getter/setter methods
- Keywords
- public – accessible everywhere
- private -only accessible within class
- protected -within class and derivative classes
- internal – accessible within same package
Polymorphism – (the easiest concept in OOP)
- Allows different classes to respond to same method names with their own implementation.
- Common mehtod names make classes interchangeable at runtime.
- Uses override to change the function at runtime
Interfaces
- Allows you to specify a set f methods that classes are required to implement
- Classes can implement multiple interfaces, interfaces can extend each other
- Interfaces can be seen as contracts to be developed against.
- Interfaces just implements a list of code
public interface Cat implements (IAnimal, ICuteness)
Patterns
Observer Pattern
- Don’t make any references to the classes that you listening to.
Singleton
- Eables a class to have just one instanceaccessible through a static method
- Singleton classes provide a global point of access
- AS3 doesn’t yet allow constructors to be private so you must do an if statement to check if it has already been called.