会员
周边
众包
新闻
博问
闪存
所有博客
当前博客
我的博客
我的园子
账号设置
简洁模式
...
退出登录
注册
登录
alonestarsz
博客园
首页
新随笔
联系
订阅
管理
公告
在DotNet中使用Adapter模式
using
System;
namespace
Adapter.TextShape
{
/**/
///
<summary>
///
将两个不兼容的类纠合在一起使用,属于结构型模式,
///
需要有Adaptee(被适配者)和Adaptor(适配器)两个身份.
///
实现方法:组合(composition)和继承(inheritance).
///
</summary>
public
interface
Shape
{
void
Draw();
void
Border();
}
/**/
///
<summary>
///
TextShape 的摘要说明。
///
</summary>
public
class
TextShape
{
private
string
content;
public
TextShape()
{
}
public
void
SetContent(
string
str)
{
content
=
str;
}
public
string
GetContent()
{
return
content;
}
}
/**/
///
<summary>
///
TextShapeObject 的摘要说明。
///
</summary>
public
class
TextShapeObject:Shape
{
private
TextShape text ;
/**/
///
<summary>
///
构造函数
///
</summary>
///
<param name="text"></param>
public
TextShapeObject(TextShape text)
{
this
.text
=
text;
}
public
void
Draw()
{
Console.WriteLine(
"
Draw a shap ! Impelement Shape interface !
"
);
}
public
void
Border()
{
Console.WriteLine(
"
Set the border of the shap ! Impelement Shape interface !
"
);
}
public
void
SetContent(
string
str)
{
text.SetContent(str);
}
public
string
GetContent()
{
return
text.GetContent();
}
//
[STAThread]
//
public static void Main(string[] args)
//
{
//
TextShape myText = new TextShape();
//
TextShapeObject myTextShapeObject = new TextShapeObject(myText);
//
myTextShapeObject.Draw();
//
myTextShapeObject.Border();
//
myTextShapeObject.SetContent("A test text !");
//
Console.WriteLine("The content in Text Shape is :" + myTextShapeObject.GetContent());
//
Console.ReadLine();
//
//
}
}
class
TextShapeClass:Adapter.TextShape.TextShape,Shape
{
/**/
///
<summary>
///
以下为测试代码
///
</summary>
///
public
TextShapeClass()
{
}
public
void
Draw()
{
Console.WriteLine(
"
Draw a shap ! Impelement Shape interface !
"
);
}
public
void
Border()
{
Console.WriteLine(
"
Set the border of the shap ! Impelement Shape interface !
"
);
}
[STAThread]
static
void
Main(
string
[] args)
{
//
//
TODO: 在此处添加代码以启动应用程序
//
TextShapeClass myTextShapeClass
=
new
TextShapeClass();
myTextShapeClass.Draw();
myTextShapeClass.Border();
myTextShapeClass.SetContent(
"
A test text !
"
);
Console.ReadLine();
}
}
}
Adapter: 将两个不兼容的类纠合在一起使用,属于结构型模式,需要有Adaptee(被适配者)和Adaptor(适配器)两个身份.
posted on
2004-11-19 16:39
天煞孤星
阅读(
123
) 评论(
0
)
编辑
收藏
举报
会员力量,点亮园子希望
刷新页面
返回顶部
Powered by:
博客园
Copyright © 2024 天煞孤星
Powered by .NET 8.0 on Kubernetes