`
shake863
  • 浏览: 661554 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Design Patterns in ActionScript–Abstract Factory

阅读更多

Do you still remember the factory method pattern we talked about last time? If you’re already forgot it. May be you can take a look at the following intent, which was defined by the GOF book.

Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.

— By THE GOF BOOK


 

In out last example, we use eagleFactory and penguinFactory to decide which bird should be instantiated. And we can easily extend this program only by adding some new classes; we don’t need to change the main framework.

OK, now let’s do something more about the last example.

First, let’s add a new class named littleEagle, and the little eagle doesn’t have the ability to fly, and not migration. So, we can write the code as follows.

  1. class LittleEagle extends Bird {
  2. public   function LittleEagle (){
  3.  
  4. super ( new   neverFly () , new neverMigration ()) ;
  5. }
  6. }

Then, we need to add a bird related to penguin, eh, I wonder that whether there is some kind of penguin flies and not migration. It seems that god doesn’t make that kind of creature. So, we need to make it, and we can call it littlePenguin. Of course, it was faked :) The code is as below.

  1. class LittlePenguin extends Bird {
  2.  
  3. public   function LittlePenguin (){
  4.  
  5. super ( new   fly () , new neverMigration ()) ;
  6.  
  7. }
  8. }

According to the factory method pattern, we need to add two more factories to produce these two new products. And the UML diagram becomes like this.

clip_image002

How’s your feeling about this diagram?

Four factories correspond to four birds. Looking deep inside, we can found that littleEagle is much the same as eagle, and the same to the other two classes. If we want to add bigEagle and oldEagle, we’ll need another two more factories, and it sounds unreasonable. The eagles can be looked upon as series products. Maybe they should be produced by the same factory.

Now, I should show you the new pattern, named abstract factory pattern. Its intent was defined as follows.

Provide an interface for creating families of related or dependent objects without specifying their concrete classes.

– By THE GOF BOOK

Using this pattern to change our existing code, we can get the following UML diagram.

clip_image004

Of course, the code has to be changed. Let me show you the code of eagleFactory.

  1. public class EagleFactory implements BirdFactory {
  2.  
  3. public   function getBird () : bird {
  4. return   new eagle () ;
  5. }
  6.  
  7. public   function getLittleBird () : bird {
  8.  
  9. return   new littleEagle () ;
  10. }
  11. }

Abstract Factory and Factory Method are both about generating the new object, while abstract factory cares more about a family of object.

Search-256x256 Demo | Download Download Full Project

Enjoy!

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics