10.12.2012 Views

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Example 9-1. AbstractClass.as (continued)<br />

}<br />

fromTemplate( );<br />

}<br />

protected function primitiveA( ):void<br />

{<br />

//Awaiting instructions<br />

}<br />

protected function primitiveB( ):void<br />

{<br />

//Awaiting instructions<br />

}<br />

private final function fromTemplate( ):void<br />

{<br />

trace("Hello everybody!");<br />

}<br />

};<br />

Example 9-2. ConcreteClass.as<br />

package<br />

{<br />

//Any concrete class<br />

public class ConcreteClass extends AbstractClass<br />

{<br />

trace("Concrete class");<br />

override protected function primitiveA( ):void<br />

{<br />

trace("Special A");<br />

}<br />

override protected function primitiveB( ):void<br />

{<br />

trace("Special B");<br />

}<br />

}<br />

}<br />

Looking at the AbstractClass you can see that the main algorithm is encapsulated in<br />

the templateMethod( ) function. The final statement indicates that the method is<br />

locked up and cannot be changed by the subclasses. However, all that really means is<br />

that the order cannot be changed by the subclasses. Both the primitiveA( ) and<br />

primitiveB( ) functions, which make up the templateMethod( ), are abstract. They<br />

are awaiting the details from the subclasses. So while the templateMethod( ) is locked,<br />

the operations that go into it don’t have to be, and can be changed by the subclasses.<br />

At the same time, if you want any of the operations that make up the template<br />

method to be locked, you can do that as well. The function fromTemplate( ) is locked<br />

in the same way as the main template method. As part of the templateMethod( ) function,<br />

the fromTemplate( ) operation cannot be changed by an override statement as<br />

the two abstract function can. So you can have the best of both worlds. Those opera-<br />

Minimalist Example: Abstract Template Method | 339

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!