会员
周边
众包
新闻
博问
闪存
所有博客
当前博客
我的博客
我的园子
账号设置
简洁模式
...
退出登录
注册
登录
小山
付出最大努力,追求最高成就,享受最佳生活,收获无悔人生
博客园
首页
新随笔
联系
订阅
管理
公告
解释器模式(Robot pattern)
namespace
DesignPattern.Interpreter
{
public
abstract
class
Expression
{
public
abstract
bool
Interpret();
}
public
class
Constant : Expression
{
private
bool
m_val;
public
Constant(
bool
val)
{
this
.m_val
=
val;
}
public
override
bool
Interpret()
{
return
this
.m_val;
}
public
override
string
ToString()
{
return
this
.m_val.ToString();
}
}
public
class
And : Expression
{
public
Expression left,right;
public
And(Expression left,Expression right)
{
this
.left
=
left;
this
.right
=
right;
}
public
override
bool
Interpret()
{
return
left.Interpret()
&&
right.Interpret();
}
public
override
string
ToString()
{
return
"
(
"
+
this
.left.ToString()
+
"
&&
"
+
this
.right.ToString()
+
"
)
"
;
}
}
public
class
Or : Expression
{
public
Expression left,right;
public
Or(Expression left,Expression right)
{
this
.left
=
left;
this
.right
=
right;
}
public
override
bool
Interpret()
{
return
left.Interpret()
||
right.Interpret();
}
public
override
string
ToString()
{
return
"
(
"
+
this
.left.ToString()
+
"
||
"
+
this
.right.ToString()
+
"
)
"
;
}
}
public
class
Not : Expression
{
private
Expression exp;
public
Not(Expression exp)
{
this
.exp
=
exp;
}
public
override
bool
Interpret()
{
return
!
exp.Interpret();
}
public
override
string
ToString()
{
return
"
(!
"
+
exp.ToString()
+
"
)
"
;
}
}
public
class
Client
{
public
static
void
Main()
{
Expression exp
=
new
Or(
new
And(
new
Constant(
true
),
new
Constant(
false
)),
new
And(
new
Constant(
true
),
new
Not(
new
Constant(
false
))));
Console.WriteLine(exp.ToString()
+
"
=
"
+
exp.Interpret());
}
}
}
posted on
2005-10-30 18:15
小山
阅读(
1032
) 评论(
2
)
编辑
收藏
举报
会员力量,点亮园子希望
刷新页面
返回顶部
Powered by:
博客园
Copyright © 2024 小山
Powered by .NET 8.0 on Kubernetes