Using the Decorator Pattern for a Real Web Site - Hybrid car classes concrete component (Page 2 of 6 )
The four hybrid autos are placed into separate concrete component classes that are all subclassed from the Auto class. The constructor function assigns a value to the information property, and the price() function returns a value representing the car’s price. In Examples 4-39 to 4-42, the captions are the filenames to use for saving each class.
Example 4-39. Prius.as
package { public class Prius extends Auto { public function Prius() { information = "Toyota Prius Hybrid~\n"; } override public function price():Number { return 21725.00; } } }
Example 4-40. Mariner.as
package { public class Mariner extends Auto { public function Mariner() { information = "Mercury Mariner Hybrid~\n"; } override public function price():Number { return 29225.00; } } }
Example 4-41. Accord.as
package { public class Accord extends Auto { public function Accord() { information = "Accord Hybrid~\n"; } override public function price():Number { return 30990.00; } } }
Example 4-42. Escape.as
package { public class Escape extends Auto { public function Escape() { information = "Ford Escape Hybrid\n~"; } override public function price():Number { return 26240.00; } } }
The prices here are based on prices found on the Internet, but they may or may not reflect prices at a later date. If you want to update prices or even use different autos, feel free to do so.
The\nescape character and the tilde (~) characters are used for formatting purposes. The\nis a line break, and the tilde (~) helps in separating out all the different models that become a big string as the decorator merges them altogether. Without these, the output would be a mess.