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-256x256Demo | DownloadDownload Full Project

Enjoy!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值