Design Patterns in ActionScript-Builder

本文通过一个在线定制电脑的例子,详细介绍了建造者模式的概念及其应用场景。该模式将复杂对象的构造与其表示分离,使得同样的构建过程可以创建不同的表示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Have you ever buy a computer online, maybe from dell? In dell’s website, we just need to follow its process to order the accessories you need, then you can get your own computer configuration. Of course, you can’t get the real computer until you pay it :)

Here, you direct the producer to produce your own computer through the dell website. Ok, three roles here, you, dell website and the real producer.

 

clip_image001

If we change the You to Director, DellWebsite to Builder and RealProducer to ConcreteBuilder, then this class diagram will be totally like that in a pattern called builder, and this is the topic today.

The intent of this pattern is as follows.

Separate the construction of a complex object from its representation so that the same construction process can create different representations.

–By GOF BOOK

 

In our example, the computer is a complex object, and we use the accessories’ description for its representation, and the construction process is the same.

So, our class diagram will be as follows.

clip_image002

Here, we use the description for the return value of getProduct() method in ComputerBuilder.

Now, the Director controls how to produce the computer in orderComputer() method, and the ComputerBuilder is the real action taker, it produces the real computer you want. If you want to get another kind of computer, you need to add another concrete builder class to build your dream computer :)

In our code, we can call produce the computer in this way.

  1. var builder : Builder = new ComputerBuilder () ;
  2. var   director : Director = new Director ( builder ) ;
  3. director . orderComputer () ;

Further more, you can compare this pattern with the template method. When you join the Director and the Builder interface together, it looks like the template method, isn’t it?
DownloadDownload Full Project

Enjoy!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值