行为型模式
Chain of Responsibility
//
与Decorator的实现形式相类似,
//
Decorator是在原来的
方法
快递客服问题件处理详细方法山木方法pdf计算方法pdf华与华方法下载八字理论方法下载
之上进行添加功能,而
//
Chain则是判断信号如果不是当前处理的则转交个下一个节点处理
//
我可以使用if分支来实现相同的效果,但是不够灵活,链上的每个节点是可以替换增加的,
相对
//
比较灵活,我们可以设计接口实现对节点的增删操作,而实现更方便的效果
//
这个是一个链状的结构,有没有想过使用环状结构
interface
Handler
...
{
voidhandRequest(intsignal); }
class
CHandler1
implements
Handler
...
{
privateHandlerhandler;
publicCHandler1(Handlerhandler)...{
this.handler=handler;
}
publicvoidhandRequest(intsignal)...{
if(signal==1)...{
System.out.println( handlesignal1 );
}
else...{
handler.handRequest(signal); }
}
}
class
CHandler2
implements
Handler
...
{
privateHandlerhandler;
publicCHandler2(Handlerhandler)...{
this.handler=handler;
}
publicvoidhandRequest(intsignal)...{
if(signal==2)...{
System.out.println( handlesignal2 );
}
else...{
handler.handRequest(signal); }
}
}
class
CHandler3
implements
Handler
...
{
publicvoidhandRequest(intsignal)...{
if(signal==3)...{
System.out.println( handlesignal3 );
}
else...{
thrownewError( can'thandlesignal );
}
}
}
class
ChainClient
...
{
publicstaticvoidmain(String[]args)...{
Handlerh3=newCHandler3(); Handlerh2=newCHandler2(h3); Handlerh1=newCHandler1(h2);
h1.handRequest(2);
}
}
Interpreter
//
感觉跟Composite很类似,只不过他分文终结符和非终结符
//
TemplateMethod
abstract
class
TemplateMethod
...
{
abstractvoidamd1();
abstractvoidamd2();
//此方法为一个TemplateMethod方法 publicvoidtmd()...{ amd1();
amd2();
}
}
State
//
标准
excel标准偏差excel标准偏差函数exl标准差函数国标检验抽样标准表免费下载红头文件格式标准下载
型
//
状态和操作不应该耦合在一起
class
Contexta
...
{
privateStatest;
publicContexta(intnst)...{
changeStfromNum(nst); }
publicvoidchangeStfromNum(intnst)...{
if(nst==1)...{
st=newCStatea1(); }
elseif(nst==2)...{ st=newCStatea2();
}
thrownewError( badstate ); }
voidrequest()...{
st.handle(this);
}
}
interface
State
...
{
voidhandle(Contextacontext); }
class
CStatea1
implements
State
...
{
publicvoidhandle(Contextacontext)...{
System.out.println( state1 ); //也许在一个状态的处理过程中要改变状态,例如打开之后立即关闭这种效果 //context.changeStfromNum(2); }
}
class
CStatea2
implements
State
...
{
publicvoidhandle(Contextacontext)...{
System.out.println( state2 );
}
}
//
工厂型
//
根据状态不通生成不同的state
class
StateFactory
...
{
publicstaticStategetStateInstance(intnum)...{
Statest=null;
if(num==1)...{ st=newCStatea1(); }
elseif(num==2)...{ st=newCStatea2(); }
returnst;
}
}
Visitor
//
双向引用,使用另外的一个类调用自己的方法,访问自己的数据结构
interface
Visitor
...
{
voidvisitElement(Elementdelement);
}
class
CVisitor
implements
Visitor
...
{
publicvoidvisitElement(Elementdelement)...{
element.operation(); }
}
interface
Elementd
...
{
voidaccept(Visitorvisitor);
voidoperation();
}
class
CElementd
implements
Elementd
...
{
publicvoidaccept(Visitorvisitor)...{
visitor.visitElement(this); }
publicvoidoperation()...{
//实际的操作在这里
}
}
class
Clientd
...
{
publicstaticvoidmain()...{
Elementdelm=newCElementd();
Visitorvis=newCVisitor();
vis.visitElement(elm); }
}
Iteretor
interface
Structure
...
{
interfaceIteratora...{ voidfirst();
booleanhasElement();
Objectnext();
}
}
class
Structure1
implements
Structure
...
{
Object[]objs=newObject[100];
//使用内部类是为了对Struture1的数据结构有完全的访问权 classIteratora1implementsIteratora...{
intindex=0;
publicvoidfirst()...{ index=0;
}
分享到:
JAVA中配置环境变量(设置方法大全总结) |
结构型模式
2007-03-17 19:28
浏览 34
评论(0)
相关推荐
评论
发
表
关于同志近三年现实表现材料材料类招标技术评分表图表与交易pdf视力表打印pdf用图表说话 pdf
评论
您还没有登录,请您登录后再发表评论
dp.SyntaxHighlighter.HighlightAll('code', true, true);
$$('#main .blog_content pre[name=code]').each(function(pre, index){ // blog
content
var post_id = 1083087;
var location = window.location;
source_url = location.protocol + "//" + location.host + location.pathname +
location.search;
pre.writeAttribute('codeable_id', post_id);
pre.writeAttribute('codeable_type', "Blog");
pre.writeAttribute('source_url', source_url);
pre.writeAttribute('pre_index', index);
pre.writeAttribute('title', '行为型模式');
});
fix_image_size($$('div.blog_content img'), 700);
function processComment() {
$$('#main .blog_comment > div').each(function(comment){// comment
var post_id = comment.id.substr(2);
$$("#"+comment.id+" pre[name=code]").each(function(pre, index){
var location = window.location;
source_url = location.protocol + "//" + location.host + location.pathname
+ location.search;
source_url += "#" + comment.id;
pre.writeAttribute('codeable_id', post_id);
pre.writeAttribute('codeable_type', "BlogComment");
pre.writeAttribute('source_url', source_url);
pre.writeAttribute('pre_index', index);
pre.writeAttribute('title', '行为型模式');
});
});
}
function quote_comment(id) {
new Ajax.Request('/editor/quote', {
parameters: {'id':id, 'type':'BlogComment'},
onSuccess:function(response){editor.bbcode_editor.textarea.insertAfterSelection(response.responseText);
Element.scrollTo(editor.bbcode_editor.textarea.element);}
});
}
code_favorites_init();
processComment();
new WeiboShare({share_buttons: $('share_weibo'), img_scope: $('blog_content')});