2007-09-19

利用ColdFusion组件实现状态模式

来源: 开发者 作者: 评论 0 条
 
Status '#getStatus()#' marking content as ready for publish approval...<br/>
</cfoutput>
<cfset setStatus('publishApproval') />
<cfelseif getStatus() eq 'publishApproval'>
<cfoutput>
Status '#getStatus()#' Marking content as deployed...<br/>
Status '#getStatus()#' Pushing live and updating content cache...<br/>
</cfoutput>
<cfset setStatus('published') />
</cfif>
</cffunction>
<cffunction name="reject" access="public" returntype="void" output="true" hint="">
<cfset var local = structNew() />
<cfif getStatus() eq 'review'>
<cfoutput>
Status '#getStatus()#' alerting content author that content is rejected...<br/>
Status '#getStatus()#' setting back to draft mode...<br/>
</cfoutput>
<cfset setStatus('draft') />
<cfelseif getStatus() eq 'publishApproval'>
<cfoutput>
Status '#getStatus()#' alerting reviewer that publishing is rejected...<br/>
Status '#getStatus()#' setting back to review mode...<br/>
</cfoutput>
</cfif>
</cffunction>
<cffunction name="getStatus" access="private" returntype="string" output="false" roles="" hint="I return the status.">
<cfreturn variables.instance.status />
</cffunction>
<cffunction name="setStatus" access="private" returntype="void" output="false" roles="" hint="I set the status.">
<cfargument name="status" type="string" required="true" hint="status" />
<cfset variables.instance.status = arguments.status />
</cffunction>
</cfcomponent>

这种方法可能比第一种方法好,但是我还是认为有太多的缺点。这就是为什么我们转向状态模式,看看是否能够有所帮助。

状态模式定义

状态模式的概念在由Gamma, Helms, Johnson,和 Vlissides(“四人帮”)所著的《设计模式》一书中首次提出。在这本书中,作者指出状态模式的意图是“当对象的内部状态改变时,答应改变它的行为。对象将看起来能够改变它的类”。

即使不了解状态模式,仅上面的描述就可能使你惊异。“当对象的内部状态改变时,答应改变它的行为”听起来像是我们想要内容项完成很多功能。

应用状态模式解决问题

实质上,状态模式所做的就是答应我们将不同的状态和每个状态需要的行为封装到CFCs中,每个状态知道如何对不同的行为作出反应,且根据不同的环境设定内容项的状态。我们给每个内容项(为了清楚起见,在这种解决方案中我将它称为ContentItemContext)提供一个简单的接口,但是避免了上面例子中的条件爆炸问题。ContentItemContext见列表E,状态CFC代码如列表F,运行测试如列表G。

列表 E

<cfcomponent name="ContentItemContext" hint="I am a per-reqeust CFC that represents a Content Item. All instance data about the item would go here.">
共9页: 上一页 [1] [2] [3] 4 [5] [6] [7] [8] [9] 下一页

(本文仅表明作者个人观点,不代表本站及其管理员立场.) 推荐 收藏 投稿 打印 返回 关闭
上一篇:Python介绍  
下一篇:Java语言中常见的十大误解
    评论加载中…
 推荐文章
     

网站首页  -  网站地图 -   站长论坛  -  网站投稿  -    -  网站管理
Copyright © 2008 芜湖站长站 All Rights Reserved 皖ICP备07500611号