Q&A - Advanced ActionScript 3: Design Patterns, Second Edition (2015)

Advanced ActionScript 3: Design Patterns, Second Edition (2015)

Chapter 5. Q&A

It’s no surprise that everyone has their own style of learning new material. Some learn material from hearing it, some from reading it, and some by trial and error. Object-oriented programming covers a vast amount of material, and books often fail to offer a means of validation about how well you’ve learned the information. I have always found this to be frustrating, because I prefer to have immediate feedback about how well I’ve understood what I read.

This chapter provides two quizzes consisting of 50 questions each, to provide you with immediate feedback about how well you know the material covered in Chapters 1-4. The questions pertain to OOP and its use with the ActionScript 3 object-oriented language. Due to the advanced nature of the material, some questions may be answered with previous understandings before this book. Because some people learn best from explanations provided after a test, the quizzes include detailed explanations. You won’t be scored or judged on your answers, so you can use this chapter as a way to further your understanding of object-oriented thought processes in ActionScript 3.

Quiz 1

Understanding object-oriented programming is easy. True image

1. Circle the best answer.

Learning object-oriented programming is a(n) _____________ process.

Slow Iterative Cumbersome Arduous

2. Fill in the blank.

There are ____ principles of object-oriented programming.

3. What are the principles of object-oriented programming?

___________________________ ____________________________

___________________________ ____________________________

___________________________ ____________________________

4. Match each OOP principle to the correct definition.

__ ensures properties can’t be changed without your permission.

__ should possess only one behavior.

__ works well with dynamic binding.

__ allows behaviors to change.

A. Encapsulation B. Inheritance C. Polymorphism D. Data hiding

5. External definitions can have custom name spaces. True False

6. Fill in the blanks.

Object-oriented programming is the practice of creating a software architecture that enables ___________ through ___________ design.

7. Circle the best answer.

An object-oriented programmer is __________________.

more advanced in coding more strategic in developing

8. Match the characteristics with the programming practices.

Procedural programming

Faster performance
Slower performance

Object-oriented programming

Routines
Subroutines
Methods
Behaviors

9. Complete the sentence.

The nature of a subclass is to __________________________

____________________________________________________.

10.List the three directives that are understood as external definitions.

____________________

____________________

____________________

11.Fill in the blank.

_____________ classes have an internal hashtable.

12.In the following code, is ClassB sealed or dynamic?

package
{
import flash.display.Sprite;

dynamic public class ClassA extends Sprite
{
}
}
package
{
public class ClassB extends ClassA
{
}
}

13.How can you modify a behavior of a class that is marked final?

__________________________________________________________

__________________________________________________________

__________________________________________________________

__________________________________________________________

14.What are the uses of packages and the names of external definitions?

__________________________________________________________

__________________________________________________________

__________________________________________________________

__________________________________________________________

15.Why should you use function casting such as TYPE(expression) in AS3?

__________________________________________________________

__________________________________________________________

__________________________________________________________

__________________________________________________________

16.What is an example of a wrong time to use function casting such as
TYPE(expression) in AS3?

__________________________________________________________

__________________________________________________________

__________________________________________________________

__________________________________________________________

17.Circle the answer that best completes the sentence.

An interface can include __________________.

Getters and setters
Visibility modifiers
Attributes
Algorithms

18.Static is a visibility modifier. True False

19.Given the following code, which answer is correct?

package
{
public interface ISpeak
{
function speak(): void
}
}
package
{
public class Human
{
public function speak(): void
{
trace( 'hello' );
}
}
}
package
{
public class Cat implements ISpeak
{
public function speak(): void
{
trace( 'meow' );
}
}
}

A. var itSpeaks: ISpeak = ISpeak( new Human() );
B. var itSpeaks: ISpeak = new Cat();
C. var itSpeaks: ISpeak = new Human();
D. Both A and B

20.Why would you instantiate a class?

__________________________________________________________

__________________________________________________________

__________________________________________________________

__________________________________________________________

21.Fill in the blank.

Inheritance is said to have a ____________ coupled relationship.

22.How does the MovieClip from the library break encapsulation?

__________________________________________________________

__________________________________________________________

__________________________________________________________

__________________________________________________________

23.Circle the answer that best completes the sentence.

Object-oriented programming is __________________.

a structural device
the act of making objects

24.OOP has a few downsides. List as many as you can.

__________________________________________________________

__________________________________________________________

__________________________________________________________

__________________________________________________________

__________________________________________________________

25.How do design patterns help developers?

__________________________________________________________

__________________________________________________________

__________________________________________________________

__________________________________________________________

26.List the three categories of design patterns.

____________________

____________________

____________________

27.Fill in the blank.

A good rule of thumb is to never exceed more than ___ levels of inheritance.

28.What is the difference between interface inheritance and class inheritance?

__________________________________________________________

__________________________________________________________

__________________________________________________________

__________________________________________________________

29.Fill in the blanks.

One design principle is to favor ___________ over ___________.

30.Circle the answer that best completes the sentence.

Composition relies on __________________.

delegation
tight coupling
objects
IS-A relationships

31.Fill in the blank.

The relationship shown is __________________.

image

32.Match the items in the two columns.

This type checking hates poor code.

Compile-time type checking

This type checking doesn’t like mismatches.

Runtime type checking

33.Explain when you would use an abstract class instead of an interface in AS3.

__________________________________________________________

__________________________________________________________

__________________________________________________________

__________________________________________________________

34.List the differences between an abstract class and an interface in AS3.

__________________________________________________________

__________________________________________________________

__________________________________________________________

__________________________________________________________

__________________________________________________________

__________________________________________________________

35.Circle the answer that best completes the sentence.

Object-oriented programming begins with __________________.

objects
implementation
collaboration
factoring

36.Given the following classes, circle the term that best describes its encapsulation.

package
{
public class CrestTartarAndCavityProtection extends ToothPaste
{
public function CrestTartarAndCavityProtection() : void
{
}

public function fightTartar() : void
{
}

public override function fightCavities() : void
{
}

public function whitenTeeth() : void
{
}
}
}

Cohesive Incohesive

37.When do you not need to perform an analysis of a system?

__________________________________________________________

__________________________________________________________

__________________________________________________________

__________________________________________________________

38.Performance can change requirements. True False

39.Consider the following code:

package
{
//Felidae is the Hierarchy of the cat family
internal class Felidae
{
protected var _canPurr : Boolean;
protected var _region : String;
protected var _speed : int;

public function run() : void
{
}

public function sleep() : void
{
}

public function eat() : void
{
}
}
}
package
{
public final class Cougar extends Felidae
{
public function Cougar()
{
_canPurr = true;
_region = "South America";
_speed = 40; // mph
}
}
}

What conclusions can you draw about the application from the use of the final keyword?

___________________________________________________________

___________________________________________________________

___________________________________________________________

___________________________________________________________

40.Why is having a broad understanding of an object-oriented language important when dealing with design patterns?

___________________________________________________________

___________________________________________________________

___________________________________________________________

___________________________________________________________

41.Assign the appropriate access modifiers to achieve the following: the classes within utils.color.type should never be set by anything other than ColorConverter, but their get values can be read outside their package.

image

42.Fill in what is needed to ensure that someObject doesn’t linger with the garbage collector when remove() is called.

image

43.Explain what the top-level function getDefinitionByName is used for.

___________________________________________________________

___________________________________________________________

___________________________________________________________

44.Rank the likelihood of change among the following, where 1 means least likely to change and 5 means most likely to change.

__ Client requirements
__ Design layout
__ Transitions/Motion
__ User flow
__ Creative assets

45.Using the following table, which displays the bytes per object, how many bytes will SomeClass occupy?

image

package
{
public class SomeClass extends Shape
{
private var _tField : TextField;
private var _obj : Object;

public function SomeClass()
{
_obj = {};
}
}
}

SomeClass takes up ______ bytes.

46.On deployment, all documents used during analysis can be tossed out in celebration.
True False

47.Why is it best to implement the disposable pattern as nothing more than a class interface?

___________________________________________________________

___________________________________________________________

___________________________________________________________

___________________________________________________________

___________________________________________________________

___________________________________________________________

___________________________________________________________

48.The time used during the build process is best spent in implementation.
True False

49.All classes are expected to have their behaviors changed. True False

50.All classes created during a project are best left in the project’s general source folders. True False

Answers to Quiz 1

Understanding object-oriented programming is easy True image

Explanation: Object-oriented programming is a very advanced concept, and is often misunderstood by some advanced programmers. Despite what some may say, understanding OOP isn’t easy. Remember this through your frustrations.

1. Circle the best answer.

Learning object-oriented programming is a(n) _____________ process.

Slow image Cumbersome Arduous

Explanation: Although learning OOP is slow, cumbersome, and arduous, only iterative truly describes the process required to understand all OOP concepts. Much of OOP is a thought process that isn’t often learned in a linear fashion.

2. Fill in the blank.

There are 4 principles of object-oriented programming.

3. What are the principles of object-oriented programming?

Encapsulation

Inheritance

Polymorphism

Data hiding

5. Match each OOP principle to the correct definition.

D ensures properties can’t be changed without your permission.

A should possess only one behavior.

C works well with dynamic binding.

B allows behaviors to change.

A. Encapsulation B. Inheritance C. Polymorphism D. Data hiding

6. External definitions can have custom name spaces. True image

Explanation: The Dynamic, Final, Public, and internal keywords are the only modifiers of a directive. Custom namespaces are allowed within external definitions as access modifiers of a property and/or behavior.

7. Fill in the blanks.

Object-oriented programming is the practice of creating a software architecture that enables flexibility through modular design.

Explanation: Through the use of objects, you can compartmentalize code, which lets you introduce polymorphism into your code more easily. The greater the separation between the system and the code that creates it, the more flexibility developers are granted.

8. Circle the best answer.

An object-oriented programmer is __________________.

more advanced in coding image

Explanation: It’s a misconception that if you do something on a daily basis, you must get to be amazing at it by the time you reach a certain age. There are plenty of cases that prove this is far from the truth. Most college basketball players, although great, don’t make it to the NBA despite having played all their lives. Being an object-oriented programmer doesn’t mean you’re more advanced because you understand OOP; rather, you’ve embraced OOP for the structure it enables.

9. Match the characteristics with the programming practices.

image

10.Complete the sentence.

The nature of a subclass is to supply the behavior of an abstract method, or add a more specialized behavior to an existing behavior.

11.List the three directives that are understood as external definitions.

Interface
Namespace
Class

12.Fill in the blank.

Dynamic classes have an internal hashtable.

13.In the following code, is ClassB sealed or dynamic?

package
{
import flash.display.Sprite

dynamic public class ClassA extends Sprite
{
}
}
package
{
public class ClassB extends ClassA
{
}
}

Sealed

Explanation: Remember from Chapter 2 that classes remain sealed unless specified as dynamic.

14.How can you modify a behavior of a class that is marked final?

When a class itself is marked final, the only way to modify any behavior of the class is to open the file and modify it. But don’t do so at any cost. That would break an OOP design principle: a class should be open for extension and closed for modification.

Explanation: The final keyword ensures protection from modification by any means, short of overwriting the file’s contents. This protection may not always be for the class’s best interest but for yours. Consider a class that is broken but that will serve a key role in later development. By marking the class final, you can ensure that no team members will have any behaviors that are affected by future class modifications.

15.What are the uses of package structures and the names of external definitions?

The name of an external definition should precisely define the intended behavior the definition provides to an application. The defining package in which it resides elaborates on the associations it uses and provides additional context for the named behavior. Finally, package structures guarantee uniqueness among definitions.

16.Why should you use function casting such as TYPE(expression) in AS3?

Function casting operations are best used to acquire thrown errors when a cast is unsuccessful. This aids in debugging, which is always a good thing.

17.What is an example of a wrong time to use function casting such as TYPE(expression) in AS3?

Type(expression) is the equivalent syntax to instantiating a new object, if the constructing arguments are manipulated by the class. Because of the operation, an unexpected error may occur.

18.Circle the answer that best completes the sentence.

An interface can include __________________.

image

19.Static is a visibility modifier. True image

Explanation: The internal, public, private, protected, and custom namespaces are the only access modifiers, or visibility modifiers. Using the static keyword declares an attribute or behavior that is owned by the class itself, not the instance of the class.

20.Given the following code, which answer works best?

package
{
public interface ISpeak
{
function speak() : void
{
}
}
}
package
{
public class Human
{
public function speak() : void
{
trace( 'hello' );
}
}
}
package
{
public class Cat implements ISpeak
{
public function speak() : void
{
trace( 'meow' );
}
}
}
A. var itSpeaks :ISpeak= ISpeak( new Human() ) ;
B. image
C. var itSpeaks :ISpeak= new Human() ;
D. Both A and B

Explanation: Although it may appear that answer D is correct, the proper answer is in fact B. The interfaces are exact, but to enable typecasting, the object must have the expected type in its traits object. Cat is the only class to use the ISpeak interface, which is why only an instance of Cat can fulfill the binding of an ISpeak variable.

21.Why would you instantiate a class?

By copying a predefined template, objects can modify their attributes and/or states independently of one another, allowing for a reduction in the collective number of static classes used to achieve the same goals.

22.Fill in the blank.

Inheritance is said to have a tightly coupled relationship.

Explanation: Because a subclass inherits class attributes and behaviors, any change to the superclass directly affects the subclass. Thus a subclass, although able to inject its own specifics, is at the mercy of any errors caused by the classes from which it derives its behaviors.

23.Why does the MovieClip from the library break encapsulation?

When you use a MovieClip from the library, nested clips must be defined as public, which doesn’t allow the class to monitor all modifications made to it.

24.Circle the answer that best completes the sentence.

Object-oriented programming is __________________.

image

25.OOP has a few downsides. List as many as you can.

Object-oriented programming can add more complexity to a system.
Object-oriented programming requires memory management.
Instantiating objects takes time, which makes OOP slower than procedural programming.
OOP requires a lot of planning and strategy.
If you use OOP improperly, your code will be harder to maintain.
Learning OOP != understanding OOP.

26.How do design patterns help developers?

Design patterns introduce solutions you may not have previously seen or known existed. Beyond their generalized solutions, design patterns are like a language, because they’re a road map to your code.

27.List the three categories of design patterns.

Creational

Behavioral

Structural

28.Fill in the blank.

A good rule of thumb is to never exceed more than 3 levels of inheritance.

Explanation: The further you are from the origin of the inherited behaviors and/or attributes, the more errors can occur among the subclasses due to the slightest modification of any of the superclasses. The greater the number of levels of inheritance, the greater the impact the error may cause.

29.What is the difference between interface inheritance and class inheritance?

Interface inheritance enables an object to be bound to a reference in lieu of another object, whereas class inheritance defines an object’s behavior in terms of a superior’s implementation.

30.Fill in the blanks.

One design principle is to favor composition over inheritance.

Explanation: Whereas inheritance fails to offer a loosely coupled relationship to behavioral modification, composition promotes it. Composition is the preferred solution, but you end up with more classes if not more objects.

31.Circle the answer that best completes the sentence.

Composition relies on __________________.

o image

o tight coupling

o objects

o IS-A relationships

32.Fill in the blank.

The relationship shown is instantiation.

image

33.Match the items in the two columns.

This type checking hates poor code.

Compile-time type checking

This type checking doesn’t like mismatches.

Runtime type checking

34.Explain when you would use an abstract class instead of an interface in AS3.

When you’re dealing with inheritance, or IS-A scenarios, it’s best to use an abstract class so that defined methods and/or variables can be inherited by the subclass, reducing duplicate code. Although an interface can provide a type for an object, it doesn’t localize the commonalities that objects share, because it only concerns itself with public methods. An abstract class, on the other hand, is an object; and an object is a group of code encapsulated within a definition. This localizes states and behaviors so they can be used by multiple objects known as subclasses. Using an abstract class also ensures that minor fixes are reflected in all subclasses. And finally, it separates the specifics of behaviors from the interface, enabling a more flexible means of extension.

35.List the differences between an abstract class and an interface in AS3.

An abstract class can contain code but is never instantiated.

An interface can’t contain code.

An abstract class’s attributes can include visible modifiers.

An interface can only declare public methods.

You can use multiple interfaces, but you can extend only one class.

36.Circle the answer that best completes the sentence.

Object-oriented programming begins with __________________.

o objects

o implementation

o collaboration

o image

37.Given the following classes, circle the term that best describes its encapsulation.

package
{
public class CrestTartarAndCavityProtection extends ToothPaste
{
public function CrestTartarAndCavityProtection() : void
{
}

public function fightTartar() : void
{
}

public override function fightCavities() : void
{
}

public function whitenTeeth() : void
{
}
}
}

o Cohesive image

Explanation: Unless you’re a dental junkie and have witnessed that CrestTartar&Cavity protection does in fact whiten teeth, the proper answer is incohesive. The chosen class name alludes to a behavior that prevents tartar and cavities but offers an additional 'margin-top:6.0pt;margin-right:41.0pt;margin-bottom: 6.0pt;margin-left:35.0pt;text-indent:-18.0pt;line-height:normal'>38.When do you not need to perform an analysis of a system?

Proper analysis is always required, and therefore the only time it doesn’t need to be performed is when it has already been done.

39.Performance can change requirements. True image

Explanation: Performance is a reason to change a design and/or technical requirements, but never client requirements. Only technical limitations should modify what a client wants.

40.Consider the following code:

package
{
internal class Felidae
{
protected var _canPurr : Boolean;
protected var _region : String;
protected var _speed : int;

public function run()
{
}

public function sleep()
{
}

public function eat()
{
}
}
}
package
{
final public class Cougar extends Felidae
{
public function Cougar()
{
_canPurr = true;
_region = "South America";
_speed = 40;
// mph
}
}
}

What conclusions can you draw about the application from the use of the final keyword?

The application doesn’t care about subcategories of Cougar. Also, because Cougar doesn’t modify behaviors of the Felidae class, any subclass that intends to modify Felidae should be done by subclassing Felidae directly.

41.Why is having a vast understanding of an object-oriented language important when dealing with design patterns?

design patterns are absent of any language specifics in efforts for developers to apply them to any object-oriented Lanaguage. Having a vast knowledge of both the classes, and syntax used by the OOL, you can be best assured to implement a pattern in the most efficient manner possible.

42.Assign the appropriate access modifiers to achieve the following: the classes within utils.color.type should never be set by anything other than ColorConverter, but their get values can be read outside their package.

image

43.Fill in what is needed to ensure that someObject doesn’t linger with the garbage collector when remove() is called.

image

44.Explain what the top-level function getDefinitionByName is used for.

getDefinitionByName is a means to retrieve a Class object belonging to a fully qualified class name from the application’s current domain. The returned object can create new instances or be used to view its class members.

45.Rank the likelihood of change among the following, where 1 means least likely to change and 5 means most likely to change.

1 Client requirements

2 Design layout

4 Transitions/Motion

3 User flow

5 Creative assets

Explanation: The least likely to change is always the client’s requirements because clients know what they want—that’s why they approached your company. Before moving into the development phase, the requirements need to be approved, most often by the client: that’s the design layout. A user flow is the backbone representing the path the user takes to fulfill client requirements as expressed by the designer’s layout. A user flow is more likely to be enhanced than changed significantly. Transitions, behaviors, and creative assets are very specific, and specifics are always likely to change. Transitions and behaviors are concepts that designers already have in mind, whereas assets tend to get worked out along the way.

46.Using the following table, how many bytes will SomeClass occupy?

image

package
{
public class SomeClass extends Shape
{
private var _tField : TextField;
private var _obj : Object;

public function SomeClass()
{
_obj = {};
}
}
}

SomeClass takes up 232 bytes.

47.On deployment, all documents used during analysis can be tossed out in celebration.
True image

Explanation: Similar to the way City Hall keep building blueprints on file, all forms of analysis should be stored to serve as the blueprints for any remodeling in the future.

48.Why is it best to implement the disposable pattern as nothing more than a class interface?

For memory management purposes, almost every class requires the dispose method. You can’t inject the method into top-level classes, so you must develop a new subtype for every class you need to extend. This can be problematic if you forget to use your new subtypes and may lead to new issues later when you try to correct any mistakes. By adding the behavior as an interface, you add a protocol to upcast to IDisposable and bypass any issues that may arise when you try to add a subclass.

49.The time used during the build process is best spent in implementation. True image

Explanation: Implementation should occupy the least amount of time during development. You should spend the most time performing object-oriented analysis and design.

50.All classes are expected to have their behaviors changed. True image

Explanation: This isn’t true and shouldn’t be in your mind during class development, because it would lead to using many public and protected modifiers and potentially breaking the rule of data-hiding. During OOA and OOD, class hierarchies present themselves, and should be separated from specific behaviors rather than those that are generalized. These relationships determine which classes are expected to have their behaviors changed, and you can design those classes accordingly.

51.All classes created during a project are best left in the project’s general source folders. True image

Explanation: Although it’s wise to ensure that you download all appropriate classes from a repository, doing so doesn’t enable one of the most powerful features of the object-oriented process: reuse. During the course of your build, you develop classes with specific behaviors and classes that use those behaviors to achieve specific goals within your application. Many of these behaviors may be used outside this application, and thus should be stored in their own team library.

Quiz 2

Understanding object-oriented programming is easy. True image

1. Why is inheritance the cornerstone of OOP?

___________________________________________________________

___________________________________________________________

___________________________________________________________

___________________________________________________________

2. Consider the following figure:

image

If class B inherits class A, which already imported the definition of class C, why does class B require the definition of class C as well?

___________________________________________________________

___________________________________________________________

___________________________________________________________

___________________________________________________________

3. Composition builds systems by combining less complex parts. True False

4. Fill in the blank.

A class that doesn’t define an interface for subclasses is known as a(n) ___________________ class.

5. Circle the best answer.

someMethod(obj:Object):void is known as a _____________.

o function method signature receiver

6. Complete this design principle: Program to a(n) _____________, not to a(n) ______________.

7. Circle the best answer.

An object’s type refers to its _____________.

o superclass behaviors interface signatures

8. Explain how EventDispatcher can enable flexible architecture.

___________________________________________________________

___________________________________________________________

___________________________________________________________

___________________________________________________________

9. Explain how inheritance breaks encapsulation.

___________________________________________________________

___________________________________________________________

___________________________________________________________

___________________________________________________________

10.Why is it best to add an interface to a concrete class versus adding a method?

___________________________________________________________

___________________________________________________________

___________________________________________________________

___________________________________________________________

11.Consider the following figure:

image

var cb:Class_B = new Class_B()

Explain why this is poor OOP.

___________________________________________________________

___________________________________________________________

___________________________________________________________

___________________________________________________________

12.Given the following class, which term best describes its encapsulation?

package
{
public class WhiteningTartarCavityProtection extends ToothPaste
{
public function WhiteningTartarCavityProtection() : void
{
}

public function fightTartar() : void
{
}

public override function fightCavities() : void
{
}

public function freshenBreath() : void
{
}
}
}

o Cohesive Incohesive

13.Circle the answer that best completes the sentence.

Object-oriented programming ends with __________________.

o objects

o implementation

o collaboration

o factoring

14.Memory management is necessary in Flash. True False

15.Break this object into smaller components and define their collaborations via simple associations.

image

16.Generalize the following objects, and create a class diagram displaying their associations.

image

17.Return types should be specific. True False

18.Show the appropriate associations.

image

19.Explain why stage instances must be turned off.

__________________________________________________________

__________________________________________________________

__________________________________________________________

__________________________________________________________

20.Static code can be polymorphic. True False

21.Circle the best answer based on your opinion.

Learning object-oriented programming is a(n) _____________ process.

slow Iterative involved complicated

22.You don’t need to supply the import directive if you refer to an external definition via the fully qualified name. True False

23.Interfaces can use interface inheritance. True False

24.Show the appropriate association.

image

25.Protected constants can be overwritten by subclasses. True False

26.Match the items in the two columns.

internal

I’m seen.

private

You have to be aware of me to see me.

protected

I’m seen by those around me.

custom

I can’t be seen by those like me.

public

I’m seen by those like me.

27.Fill in the blank.

______________ is a lesser benefit of OOP due to the lifespan of an RIA.

Modularity Flexibility Scalability Maintainability

28.Complete this design principle: Closed for _______________, open for ________________.

29.Circle the answer that best completes the sentence.

A team build should only divide and conquer after __________________.

o kick-Off

o it has determined the requirements

o design patterns have been applied

o a system flow chart has been crafted

30.Class variables can’t be changed. True False

31.Explain the importance of understanding that designer and client requirements are more often than not compound requirements.

__________________________________________________________

__________________________________________________________

__________________________________________________________

__________________________________________________________

__________________________________________________________

__________________________________________________________

32.Consider the following code:

package color.utils
{
public class ColorConverter
{
public static function toARGB() : uint
{
// 32 bit unsigned integer AARRGGBB
}
}
}

Using the static class ColorConverter and its static method toARGB, you can convert any colorspace value to a 32-bit unsigned value. This is a perfect class to introduce into the color-picker API for the client. Unfortunately, the client’s application can’t use 32-bit unsigned integers—only 24-bit.

Devise a means to modify this behavior.

33.Uh oh: there’s an object-oriented mistake in Listing 5-1. Correctly instantiate the appropriate Sprites in Listing 5-2.

Listing 5-1.

package
{
import flash.display.Sprite;

import com.utils.bitmapdata.Sprite;

var topLevelSprite : Sprite = new Sprite();
var bitmapDataSprite : Sprite = new Sprite();
}

Listing 5-2.

Package
{
import flash.display.Sprite;
import com.utils.bitmapdata.Sprite;

var topLevelSprite:Sprite=__________________________
var bitmapDataSprite:Sprite =_______________________
}

34.Using nothing but the following image, explain the benefits of the symbols with the properties.

image

#protected __private $static public

__________________________________________________________

__________________________________________________________

__________________________________________________________

__________________________________________________________

35.Using the method describeType(obj:Object):xml from the flash.utils package, you get the following results from a Vector.<int> instance:

<type name="__AS3__.vec::Vector.<int>" base="Object" image
isDynamic="true" isFinal="true" isStatic="false">
<extendsClass type="Object"/>
<constructor>
<parameter index="1" type="uint" optional="true"/>
<parameter index="2" type="Boolean" optional="true"/>
</constructor>
</type>

List all possible upcasts from the XML output.

______________________

______________________

______________________

______________________

36.A package for a definition is the definition’s URI. True False

37.Given that Circle is defined in your SWF library,

ApplicationDomain.currentDomain.getDefinition('Circle') == getDefinitionByName('Circle')

True False

38.Fill in the blank.

______________ is a container of class definitions in your RIA.

39.Explain why you should avoid *, .rest, and arguments.

__________________________________________________________

__________________________________________________________

__________________________________________________________

__________________________________________________________

40.Fill in the blank.

Using the ________ keyword lets you call a method of a superclass.

41.Getters and setters should always be public. True False

42.Explicit getters and setters can be used in lieu of implicit getters and setters.

o Explicit: public function getText():String

o Implicit: public function get text():String

Explain from an OO standpoint why one may be better than the other.

__________________________________________________________

__________________________________________________________

__________________________________________________________

__________________________________________________________

43.Class A declares a property _date as being a protected variable. Class B extends class A and modifies _date through the variable. This manner of modification is acceptable practice. True False

44.What is the purpose of the top-level class named Class?

__________________________________________________________

__________________________________________________________

__________________________________________________________

__________________________________________________________

45.AS3 supports method overloading. True False

46.Class methods, or static methods, can access both instance and class variables.

True False

47.When would you use getDefinition instead of getDefinitionByName?

__________________________________________________________

__________________________________________________________

__________________________________________________________

__________________________________________________________

48.How can runtime shared libraries work with an ApplicationDomain?

__________________________________________________________

__________________________________________________________

__________________________________________________________

__________________________________________________________

49.You can specify an ApplicationDomain in the LoaderContext of a Loader.

True False

50.How do the return types differ between getDefinition and getDefinitionByName?

__________________________________________________________

__________________________________________________________

__________________________________________________________

__________________________________________________________

Answers to Quiz 2

· Understanding object-oriented programming is easy. True image

· Explanation: Object-oriented programming is a very advanced concept and is often misunderstood by experienced programmers. Despite what some may say, understanding OOP isn’t easy. Remember this when you’re frustrated.

1. Why is inheritance the cornerstone of OOP?

Inheritance enables polymorphism, which is the main reason object-oriented languages exist.

2. Consider the following figure:

image

If class B inherits class A, which already imported the definition of class C, why does class B require it as well?

Unlike previous versions of the language, in ActionScript 3 every definition must be imported if any reference is used in the class. After the import directive, you can use classes by specifying either the fully qualified identifier or the class name.

3. Composition builds systems by combining less complex parts. True image

4. Fill in the blank.

A class that doesn’t define an interface for subclasses is known as a(n) concrete class.

5. Circle the best answer.

someMethod( obj:Object ):void is known as a _____________.

function method image receiver

6. Complete this design principle: Program to a(n) interface, not to a(n) implementation.

7. Circle the best answer.

An object’s type refers to its _____________.

superclass behaviors image signatures

Explanation: Because a type defines an object’s public methods, the only possible answer is interface. Signatures and behaviors don’t specify their visibilities; you can’t presume that they’re public.

8. Explain how EventDispatcher can enable flexible architecture.

The events dispatched can bubble, allowing objects to know the event they’re listening for and not specifically who it’s coming from. This enables a very loose coupling between the dispatcher of the event and the listening object.

9. Explain how inheritance breaks encapsulation.

Encapsulation facilitates groupings among related attributes and behaviors. Although a superclass may adhere to this idea of encapsulation, subclasses merely change the behaviors of the superclass. Removing the behavior from the attributes that are related can potentially cause errors, if any changes are made to the contents of the superclass. More important, the subclass can decisions for the superclass via the protected attributes, which breaks the idea of the superclass governing its modifications.

10.Why is it best to add an interface to a concrete class versus adding a method?

Adding a method to a class doesn’t offer the availability to upcast the concrete class to the interface it possesses. Thus, you’d be programming to an implementation rather than an interface. Generalizing code is always preferred.

11.Consider the following figure:

image

var cb:Class_B = new Class_B()

Explain why this is poor OOP.

By typing cb to Class_B instead of Class_A, you’re programming to a concrete, which is the same as programming to an implementation and not an interface. By classifying cb as type Class_A, you can substitute other objects, if required, in place of Class_B.

12.Given the following class, which term best describes its encapsulation?

package
{
public class WhiteningTartarCavityProtection extends ToothPaste
{
public function WhiteningTartarCavityProtection() : void
{
}

public function fightTartar() : void
{
}

public override function fightCavities() : void
{
}

public function freshenBreath() : void
{
}
}
}

o Cohesive image

Explanation: The inclusion of the FreshenBreath as a behavior continues to decrease the level of cohesion. A class should have only one reason to change, thus possessing only 1 behavior.

13.Circle the answer that best completes the sentence.

Object-oriented programming ends with __________________.

o objects

o implementation

o collaboration

o image

14.Memory management is necessary in Flash. image False

15.Break this object into smaller components and define their collaborations via simple associations.

image

16.Generalize the following objects, and create a class diagram displaying their associations.

image

17.Return types should be specific. True image

Explanation: Return types should be specific only if necessary. It’s best to generalize return types because doing so facilitates reuse. This doesn’t mean everything should return Objects. The generalization should remain to the task at hand, to first and foremost ensure a working application. Return types should be those of interfaces and common types.

18.Show the appropriate associations.

image

Explanation: The associations are compositions. To be considered human, you must have a brain volume of 1800 cubic centimeters, opposable thumbs, and an upright spine.

19.Explain why stage instances must be turned off.

To ensure that all complex assets are visible in the class so that you can nullify them for garbage collection, stage instances must be turned off.

20.Static code can be polymorphic. True image

Explanation: Fixed code can be polymorphic. For example, var mc:Sprite = new MovieClip() demonstrates polymorphic behavior, because MovieClip satisfies the interface of Sprite.

21.Circle the best answer based on your opinion.

Learning object-oriented programming is a(n) _____________ process.

Slow Iteritive involved image

Explanation: In my opinion, OOP is involved. You have to be patient and dedicated. This question is in the quiz to help you understand your own issues with OOP. Be aware of them and find a way to get past them. You’ve gotten this far.

22.You don’t need to supply the import directive if you refer to an external definition via the fully qualified name. True image

23.Interfaces can use interface inheritance. True image

24.Show the appropriate associations.

image

Explanation: A receiver behavior isn’t defined by the DVD player, television, or radio. Therefore the relationships are aggregates.

25.Protected constants can be overwritten by subclasses. True image

Explanation: Once a constant has been declared, it can’t be reassigned.

26.Match the items in the two columns.

image

27.Fill in the blank.

______________ is a lesser benefit of OOP due to the life span of an RIA.

Modularity Flexibility image Maintainability

Explanation: Although OOP provides all of these benefits, many RIAs have a short life span, and thus scalability often isn’t necessary. Modularity, flexibility, and maintainability, on the other hand, offer many more opportunities during the building process and enable you to reuse code.

28.Complete this design principle: Closed for modification, open for extension.

29.Circle the answer that best completes the sentence.

A team build should only divide and conquer after __________________.

o kick-Off

o it has determined the requirements

o design patterns have been applied

o a system flow chart has been crafted

Explanation: Without knowing all the tools in your toolbox, you can't possibly choose the most appropriate device for the job. Without knowing how to diminish tightly coupled relationships and ensure proper data-hiding, a team of developers has a higher likelihood of stepping on one another’s toes.

30.Class variables can’t be changed. True image

Explanation: Class variables are nothing more than variables that are global to the many instances of the class. Only constants are unable to change after they’ve been declared.

31.Explain the importance of understanding that designer and client requirements are more often than not compound requirements.

Designers are often influenced by external factors when they conceive a design or a behavior that something exhibits. Very rarely are a designer’s thoughts so unique that this statement isn’t true. The same can be said of clients and their requirements. They’re easily swayed to need and want functionality based on what they’ve seen or have come to view as standard in the RIA realm. Because these requirements are based on past experience, every line of code you write is bound to be included in your next project. Not using proper OOP principles to devise code reuse will lead to compounded efforts on your part.

32.Consider the following code:

package color.utils{
public class ColorConverter{
static public function toARGB():uint
//32 bit unsigned integer AARRGGBB
}
}

Using the static class ColorConverter and its static method toARGB, you can convert any colorspace value to a 32-bit unsigned value. This is a perfect class to introduce into the color-picker API for the client. Unfortunately, the client’s application can’t use 32-bit unsigned integers—only 24-bit.

Devise a means to modify this behavior.

image

Explanation: By wrapping ColorConverter with your new object, ARGBToRGB, you can intercept the returned calculation and remove the Alpha value, essentially changing the object’s behavior. With the RGB interface, via interface inheritance, the client doesn’t need to program to the concrete creation but to an IRGB type.

33.Uh oh: there’s an object-oriented mistake in Listing 5-1. Correctly instantiate the appropriate Sprites in Listing 5-2.

Listing 5-1.

package
{
import flash.display.Sprite;

import com.utils.bitmapdata.Sprite;

var topLevelSprite : Sprite = new Sprite();
var bitmapDataSprite : Sprite = new Sprite();
}

Listing 5-2.

Package
{
import flash.display.Sprite;
import com.utils.bitmapdata.Sprite;

var topLevelSprite:Sprite= new flash.display.Sprite();
var bitmapDataSprite:com.utils.bitmapdata.Sprite= image
new com.utils.bitmapdata.Sprite();
}

Explanation: By referring to the fully qualified path and/or supplying the Uniform Resource Identifier and Uniform Resource Name, you can prevent naming conflicts.

34.Using nothing but the following image, explain the benefits of the symbols with the properties.

image

#protected __private $static public

When classes are extended, their details are clear to the compiler but may not be as apparent to you. Using symbols as indicators can help you understand what class controls a property or behavior. Essentially, it’s a reference to scope.

35.Using the method describeType(obj:Object):xml from the flash.utils package, you get the following results from a Vector.<int> instance:

<type name="__AS3__.vec::Vector.<int>" base="Object" image
isDynamic="true" isFinal="true" isStatic="false">
<extendsClass type="Object"/>
<constructor>
<parameter index="1" type="uint" optional="true"/>
<parameter index="2" type="Boolean" optional="true"/>
</constructor>
</type>

List all possible upcasts from XML output.

Object

Explanation: As you can see, the XML output states the extendsClass to that of type Object solely. Because Object is the only protocol of Vector.<int>, it can only be upcast to Object.

36.A package for a definition is the definition’s URI. image False

Explanation: Via the import directive, you can open a namespace referring to a location, to which a className can be referred. Ultimately the package becomes a namespace.

37.Given that Circle is a Circular shape in your SWF library,

ApplicationDomain.currentDomain.getDefinition('Circle') image
==getDefinitionByName('Circle')

image False

Explanation: Because you’re trying to find Circle in the current domain of the static class ApplicationDomain, you target the same domain that would be used if we called the global function getDefinitionByName.

38.Fill in the blank.

ApplicationDomain is a container of class definitions in your RIA.

Explanation: ApplicationDomain is the partition of definitions within the current SWF.

39.Explain why you should avoid *, .rest, and arguments.

Using these declarations passes references blindly, which isn’t object-oriented behavior. Knowing which object types to work with is object-oriented. The statements listed offer no generalizations and deter proper OO coding.

40.Fill in the blank.

Use of the super keyword lets you call a method of a superclass.

Explanation: Although a subclass has access to all protected and public methods/properties in its superclasses, you can only reach the method of a superclass if you haven’t already specified the same method in your class. This method always takes precedence. To target the superclass’s method, you can use the super keyword followed by the method to message: super.superMethod();.

41.Getters and setters should always be public. image False

42.Explicit getters and setters can be used in lieu of implicit getters and setters.

o Explicit: public function getText():String

o Implicit: public function get text():String

Explain from an OO standpoint why one may be better than the other.

Implicit getters/setters are very convenient and are often generated automatically by third-party editors, which makes them prevalent. The downside is that using them is like working with the physical properties of the object. Also, implicit getters and setters are always expected to be public, and that may lead to clumsy data-hiding. Explicit getters and setters, on the other hand, demonstrate physical methods being targeted and can be as visible as necessary to hide data.

43.Class A declares a property _date as being a protected variable. Class B extends class A and modifies _date through the variable. This manner of modification is acceptable practice. True image

Explanation: Data-hiding ensures that a class has full control over modifying its own attributes to prevent error. Although a protected attribute is visible to all subclasses, it should remain in control over its own modifications. This ensures that the anticipated behavior remains among the methods in the superclass and is visible to the superclass for use.

44.What is the purpose of the top-level class named Class?

Much like the traits object associated with a class, a Class object is created for each class definition in an application. Using Class objects lets you acquire definitions from libraries and instantiate instances of those definitions.

45.AS3 supports method overloading. True image

46.Class methods, or static methods, can access both instance and class variables.

True image

Explanation: Static methods can only reference static variables. Instance methods, on the other hand, can reference both instance and class variables.

47.When would you use getDefinition instead of getDefinitionByName?

getDefinition can return an Object, whereas getDefinitionByName returns only a Class object. The difference is that a Class is only one of the three external definitions in the AS3 language. If you require a defined namespace or interface, you can get it via the getDefinition method. Another thing that distinguishes when to use one versus the other is when you’re attempting to reach a definition outside the currentDomain. getDefinitionByName is a global function that reaches only to the currentDomain, whereas getDefinition lets you supply the memory to target.

48.How can runtime shared libraries work with an ApplicationDomain?

When a SWF is loaded into an application, the definitions that accompany the SWF are partitioned from current definitions. RSL works in the same manner. Each partition has exclusive access to the definitions, unless the pointer of the currentDomain is targeted. By loading a SWF containing definitions that can be used throughout a large RIA, you can store and retrieve those definitions

49.An ApplicationDomain can be specified in the LoaderContext of a Loader.

True False

50.How do the return types differ between getDefinition and getDefinitionByName?

getDefinition returns an Object that can be a namespace, an interface, or even a class, whereas getDefinitionByName can only return a class definition.