Design Patterns in ActionScript–Factory Method

In our last topic, we talk about the strategy pattern . And it helps us to encapsulate the change of algorithm. Today, we go on talking about the birds. We will consider how to generate the birds, eh, I mean the classes.

Now, we have the classes of eagle and penguin. We can use them anywhere we want. Further more, we can write down the following code to decide which class should be initialized.

 

  1. public class birdFactory {
  2. public   function birdFactory (){
  3. }
  4.  
  5. public   function getBird ( type : String ) : bird {
  6. if ( type == " eagle " )
  7. return   new eagle () ;
  8. else
  9. return   new penguin () ;
  10. }
  11. }

Now, we just need to look at the type, and we can decide which kind of birds to be initialized. This is a pattern called simple factory , it seems that everything goes all right.

I don’t want to talk too much about this pattern, because it is not our topic today. And I want to introduce you a principle of OOD, called Open-Close-Principle, which means we should open for extension, and closed for modification.

Let’s take a sight at the UML diagram of the above code. Then discussing where the problem is.

clip_image002

Eh, Here comes the problem. Look at the code I just write down, what’ll happen if I want to add a new kind of bird. It seems I need to change the method of getBird(). That’s right, this is the problem of this pattern. It doesn’t fit the O-C-P. So, we need another solution.

Remember the Open-Close-Principle. We will follow this principle to get the result.

We want a factory to produce the class, and when we also want the factory doesn’t need to be modified, if we want to add a new kind of bird.

Then we’ll need another two classes of factories, named eagleFactory and penguinFactory, paired to the birds.

The birdFactory will change into interface.

  1. public interface birdFactory {
  2. function   getBird () : bird ;
  3. }

And the concrete factory will implement the birdFactory. Here is the code of eagleFactory.

  1. public class eagleFactory implements birdFactory {
  2. public   function getBird () : bird {
  3. return   new eagle () ;
  4. }
  5. }

It looks like very simple and clean, isn’t it? Another concrete factory, penguinFactory is much the same.

Now the UML diagram will change into:

clip_image004

One factory, one kind of birds. If you want to add a new kind of birds, you need to add two more classes. It sounds a little red-tape, but you’ll get the rewards. You don’t need to change the code, and the code is easy to extension. It means when the requirement is changed, you can easily fit for it. And that’s why we need design patterns, it saves our time.

That’s all about this pattern, and we will talk about another related pattern next time.

Search-256x256Demo | DownloadDownload Full Project

Enjoy!.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值