工厂方法模式:对象创建的优雅解决方案
立即解锁
发布时间: 2025-08-20 00:32:04 阅读量: 1 订阅数: 3 


ActionScript 3.0 设计模式精髓
### 工厂方法模式:对象创建的优雅解决方案
#### 1. 从简单工厂到工厂方法的演变
在软件开发中,对象的创建逻辑常常会给代码的维护和扩展带来挑战。以一个应用框架中的 Shell 代码为例,其中创建覆盖层(overlay)的逻辑最初包含在 500 行代码中,虽然调试 46 行代码可能不算负担,但对 500 行代码进行修改时,引入错误的风险就会增加。
以下是原始的创建覆盖层的代码示例:
```actionscript
private function createOverlay( overlayType : String , dataObject : * = null ) : void
{
header.disableNavigation();
currentSection.pause();
if (currentOverlay)
{
destroyCurrentOverlay();
}
switch(overlayType)
{
case SectionEvent.VIDEO_OVERLAY:
currentOverlay = new VideoOverlay();
break;
case SectionEvent.PHOTO_OVERLAY:
currentOverlay = new PhotoViewer();
break;
case SectionEvent.AUDIO_OVERLAY:
currentOverlay = new AudioOverlay();
break;
case SectionEvent.TWITTER_OVERLAY:
currentOverlay = new TwitterOverlay();
break;
case OverlayEvent.TWITTER_SUBMIT_OVERLAY:
currentOverlay = new AddTweetOverlay();
break;
}
currentOverlay.y = SiteConfig.HEADER_HEIGHT;
currentOverlay.addEventListener( OverlayEvent.CLOSE_OVERLAY , handleEvent );
currentOverlay.addEventListener( OverlayEvent.INTRO_COMPLETE , handleEvent );
currentOverlay.addEventListener( OverlayEvent.OUTRO_COMPLETE , handleEvent );
currentOverlay.addEventListener( OverlayEvent.PLAY , handleEvent );
currentOverlay.addEventListener( OverlayEvent.SUBMIT_TWEET , handleEvent );
currentOverlay.addEventListener( OverlayEvent.TWITTER_SUBMIT_OVERLAY , handleEvent );
addChild( currentOverlay );
currentOverlay.updateLayout( stage.stageWidth , (stage.stageHeight - SiteConfig.FOOTER_HEIGHT - SiteConfig.HEADER_HEIGHT) );
currentOverlay.intro();
}
private function destroyCurrentOverlay() : void
{
// implementation not shown
}
```
为了降低修改代码带来的风险,我们可以将创建覆盖层的逻辑提取到一个新的对象 `OverlayFactory` 中:
```actionscript
package
{
public class OverlayFactory
{
public function OverlayFactory()
{
}
public function createOverlay( overlayType : String ) : Overlay
{
var currentOverlay : Overlay;
switch(overlayType)
{
case SectionEvent.VIDEO_OVERLAY:
currentOverlay = new VideoOverlay();
break;
case SectionEvent.PHOTO_OVERLAY:
currentOverlay = new PhotoViewer();
break;
case SectionEvent.AUDIO_OVERLAY:
currentOverlay = new AudioOverlay();
break;
case SectionEvent.TWITTER_OVERLAY:
currentOverlay = new TwitterOverlay();
break;
case OverlayEvent.TWITTER_SUBMIT_OVERLAY:
currentOverlay = new AddTweetOverlay();
break;
}
return currentOverlay;
}
}
}
```
这样,我们只需要在 Shell 中引用 `OverlayFactory`,就可以继续使用覆盖层的实例,同时减轻了 Shell 创建对象的负担:
```actionscript
var factory : OverlayFactory = new OverlayFactory();
header.disableNavigation();
currentSection.pause();
if (currentOverlay)
{
destroyCurrentOverlay();
}
var currentOverlay : Overlay = factory.createOverlay( overlayString );
currentOverlay.y = SiteConfig.HEADER_HEIGHT;
currentOverlay.addEventListener( OverlayEvent.CLOSE_OVERLAY , handleEvent );
currentOverlay.addEventListener( OverlayEvent.INTRO_COMPLETE , handleEvent );
currentOverlay.addEventListener( OverlayEvent.OUTRO_COMPLETE , handleEvent );
currentOverlay.addEventListener( OverlayEvent.PLAY , handleEvent );
currentOverlay.addEventListener( OverlayEvent.SUBMIT_TWEET , handleEvent );
currentOverlay.addEventListener( OverlayEvent.TWITTER_SUBMIT_OVERLAY , handleEvent );
addChild( currentOverlay );
currentOverlay.updateLayout( stage.stageWidth , (stage.stageHeight - SiteConfig.FOOTER_H
```
0
0
复制全文
相关推荐









