[Reserved] AS2 Practice Design Mode (1): Abstract Factory Mode

xiaoxiao2021-03-06  57

The initial motivation still saw a few articles written by Jimlee. Just finished the test, now take the time to learn the design model, so use the AS2 to practice the 23 models in the design model. .

AS2 can now be an object-oriented, but it supports the object-oriented characteristics is still relatively simple, and some like SmallTalk, the dynamicity is much stronger than C , which is used to practice design mode or more than enough.

According to the book's arrangement, the first mode is Abstract Factory, the personal understanding is that customers only need to publish orders for the factory, and the specific implementation is implemented by the specific factory, the key, the products of various factories, this is an application. The need is also her characteristics. Abstract Factory is well packaged in a series of products creation consistency.

In my example, the examples of fast food restaurants, McDonald and KFC provide burgers, cola, fries and ice cream, so they are used as a factory.

The first thing is the Snacketeria snack shop interface, which defines the unified interface functions of McDonald and KFC:

Snacketeria.as

interface snacketeria {public function makeHamburg (): Hamburg; public function makeFry (): Fry; public function makeCola (): Cola; public function makeIceCream (): IceCream; public function getShopName (): String;} Next define the McDonald and KFC class, implement the fast food store interface:

McDonald.as

class McDonald implements snacketeria {private var _name: String; public function makeHamburg (): Hamburg {return new McHamburg ();} public function makeFry (): Fry {return new Fry ();} public function makeCola (): Cola {return both implementations}}; String {return _name: new McCola ();} public function makeIceCream (): IceCream {return new IceCream ();;} function McDonald () {_ name = "McDonald"} public function getShopName () Almost, the most important thing is to define the type of product returned by the excuse.

Kfc.as

class KFC implements snacketeria {private var _name: String; public function makeHamburg (): Hamburg {return new KFCHamburg ();} public function makeFry (): Fry {return new Fry ();} public function makeCola (): Cola {return new KFCCola ();} public function makeIceCream (): IceCream {return new IceCream ();} function KFC () {_ name = "KFC";} public function getShopName (): String {return _name;}} while for a snack We have a set of products that we have defined a series of products.

The first is a Food class, it is the base class of all foods.

Food.as

class food {private var _name: String; private var _price: Number; private var _discount: Number; private var _type: String; public function set name (Name: String): Void {_name = Name;} public function set price (Price : Number): Void {_price = Price;} public function set discount (Discount: Number): Void {_discount = Discount;} public function set type (type: String): Void {_type = type;} public function get name ( ): String {return _name;} public function get price (): Number {return _price;} public function get discount (): Number {return _discount;} public function get type (): String {return _type;} function food ( ) {_name = "general food"; _ type = "food"; _ price = 0; _discount = 1;} public function getprice (): Number {return_price * _discount;} public function getInfo (): void {trace (_Type "Details:"); Trace ("Name:" _name); Trace ("Price:" _ price); Trace ("Discount:" _ discount);}} (now there is an idea, think of Private in the class Attributes, there is no need to implement their setter / getter, because it is used as a private type, they can't be directly operated outside the class, so getter / setter can say that there is no use of martial arts, or directly define the setProperty and getProperty functions.

Or I know the user of the setter / getter function? )

They are then Hamburg Hamburg, Mchamburg McDonald's Hamburg, Kfchamburg, Cola, McCola, Kfccola, Frya, Icecream These foods

Hamburg.as

Class Hamburg Extends Food {_ name = "burger"; _price = 0; _discount = 1; _Type = "hamburg";}} mchamburg.as

Class Mchamburg Extends Hamburg {Function Mchamburg () {_ Name = "Super Burger"; _price = 5.5; _discount = 0.8; _Type = "McDonald Hamburg";}} Kfchamburg.as

class KFCHamburg extends Hamburg {function KFCHamburg () {_ name = "invincible giant"; _price = 10; _discount = 0.9; _type = "KFC Hamburg";}} Cola.asclass Cola extends food {function Cola () {_ name = "Cola"; _price = 0; _discount = 1; _Type = "cola";}} mccola.as

Class McCola Extends Cola {Function McCola () {_ Name = "Coca Coke"; _price = 2.5; _discount = 1; _Type = "McDonald Cola";}} kfccola.as

Class Kfccola Extends Cola {Function Kfccola () {_ Name = "Pepsi"; _price = 2.5; _discount = 1; _type = "kfc cola";}} FRY.AS Sweet fries

Class Fry Extends Food {_ name = "French Fries"; _PRICE = 5; _discount = 1; _type = "fry";}} You can think that the two fries are the same, so this is defined class

Ice cream is equally like

Icecream.as

Class Icecream Extends Food {function Icecream () {_ name = "ice cream"; _price = 2; _discount = 1; _Type = "Ice cream";}} Ok, our class is already defined, below is written An example comes to the client to operate, just called Order, there is a little Composite's idea.

ORDER.AS

class order {private var _foods: Array; private var _price: Number; private var _shop: snacketeria; function order (shop: String) {_ foods = new Array (); _ price = 0; switch (shop) {case "McDonald": _shop = new mcdonald (); break; case "kfc": _ shop = new kfc (); break; default: _shop = new snacketeria ();}} public function addfood (item: food, count: number): boolean {ix (Math.Floor (count)> 0) {_ foods.push ({Item: item, count: count}); return true;} elsereturn false;} public function gettotal (): Number {var index: Number = 0; _price = 0; for (index = 0; index <_foods.length; index ) {_ price = _foods [index] .Item.getPrice () * _ foods [index] .count;} return _price;} Var index: Number = 0; Trace ("Your Orders Detail:"); Trace (_shop.getshopName ()); Trace ("Foods:"); for (INDEX = 0; Index <_foods.Length; Index ) {_foods [index] .Item.getinfo (); trace ("count:" _ foods [index] .count);} trace (""); trace ("Total Price:" gettotal ());}} Create a flash, enter the code below in the first frame: Var shop: snacketeria = new mcdonald (); // Determine which store to consume Var Lunch: O Rder = new Order (Shop.getshopName ()); // Beginned Lunch.Addfood (Shop.makehamburg (), 3); // Trim Burger Lunch.AddFood (Shop.makecola (), 2); / / Two Cups Lunch.addfood (Shop.makeicecream (), 2); // Two Icecreamlunch.Addfood (Shop.makefry (), 1); // A Sweeping French French Lunch.getInfo (); the final output should Is such that:

Your Orders Detail: McDonaldFoods: McDonald Hamburg Details: Name: Super Burger Price: 5.5Discount: 0.8Count: 3McDonald Cola Details: Name: Coca-Cola Price: 2.5Discount: 1Count: 2Ice Cream Details: Name: ice cream Price: 2Discount: 1Count : 2Fry Details: Name: French Fries Price: 5Discount: 1count: 1 Total Price: 27.2

The use of the use of Gof in their book Abstract Factory example is about the interface, when you want your program with different interface styles, such as Mac, KDE, Windows, etc. Different buttons, scroll bar style, you can use Abstract mode, set a StyleFactory for different styles to establish different button, scrollbar, respectively; this is actually a good application in the Flash program. When Flash has different interface style, this method can be used. At the same time, the disadvantage of Abstract Factory is that the extension is very troublesome. For my example, if McDonal and KFC have launched a new food, such as Pizza, then I have to build three classes, General Pizza classes, McPizza, Kfcpizza classes, of course, if the two stores are the same, one class It is enough, but also modify the method of Factory, add MAKEPIZZA's Method, and if there is an omission, it will be difficult to ensure it. ()

Article source:

AS2 Practice Design Mode (1): Abstract Factory Mode

转载请注明原文地址:https://www.9cbs.com/read-82237.html

New Post(0)