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

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-256x256 Demo | Download Download Full Project

Enjoy!.

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics