ActionScript 3.0 Design Patterns

(Chris Devlin) #1
Principles of Design Pattern Development | 43

context. The subsequent chapters use these terms a good deal, and a misunderstand-


ing at the outset can lead to confusion now and later.


Implementation


For the most part, the termimplementationis a noun referring to the details—the


actual code—in a program. So when referring to an implementation, the reference is


to the actual program as it is coded. Implementation is the internal details of a pro-


gram, but is often used to refer to a method or class itself.


You may run into some confusion when the keywordimplementsrefers to contract-


ing with aninterface structure. For example, the line:


class MyClass implements IMyInterface

has the effect of passing theinterfaceofIMyInterfacetoMyClass. To say thatMyClass


implementsIMyInterfacereally means thatMyClasspromises to use all the signatures


ofIMyInterfaceand add the coding details to them. So the phraseMyClassimple-


mentedIMyInterfacemeans thatMyClasstook all the methods inIMyInterfaceand


added the code necessary to make them do something while preserving their


signatures.


One of the two main principles of design pattern programming isprogram to an


interface, not an implementation. However, you must remember thatimplementation


is employed in different contexts, and its specific meaning depends on these con-


texts. Throughout this book, you will see references to implementation used again


and again with different design patterns, and you need to consider the other ele-


ments being discussed where the term is used.


State


The termstateis not part of the ActionScript 3.0 lexicon (likeimplementsis), but the


term is used in discussing design patterns. Essentially, state is used to refer to an


object’s current condition. For the most part you will see state used to convey the


value of a key variable. Imagine a class with a single method with a Boolean value set


in the method. The Boolean can either betrueorfalse. So its state is eithertrueor


false—which could represent on/off, allow/disallow, or any number of binary con-


ditions. The exact meaning oftrueorfalsedepends on the position of the class in


the rest of the design pattern. A more specific example would be an object that plays


and stops an MP3 file. If the MP3 is playing, it’s in a play state, while if it’s not play-


ing, it’s in a stop state.


One use of state is in the context of a state machine and the State design pattern you


will be seeing in Chapter 10. Astate engineis a data structure made up of a state net-


work where changes in state affect the entire application. The State design pattern is


centered on an object’s behavior changing when its internal state changes. The term

Free download pdf