zoukankan
html css js c++ java
设计模式 单件 & 原型
Design Pattern - Singleton & Prototype
实现了单件模式的的Client, 它自身只有一个示例, 用Instance()方法得到惟一的实例
public
class
Client
{
static
private
Hashtable ht
=
new
Hashtable();
static
protected
Client c
=
null
;
protected
Client()
{
}
public
static
Client Instance()
{
if
(c
==
null
)
c
=
new
Client();
return
c;
}
public
void
Register(String name, CloneHuman ch)
{
ht.Add(name, ch);
}
public
CloneHuman BuildCloneHuman(String name)
{
CloneHuman ch
=
(CloneHuman)ht[name];
return
ch.CreateClone();
}
}
克隆人的类, 以及分别它的子类克隆的男人和女人
public
class
CloneHuman
{
public
virtual
CloneHuman CreateClone()
{
return
null
;
}
public
void
Show()
{
String s
=
this
.GetType().ToString();
Console.WriteLine(s.Substring(s.LastIndexOf(
"
.
"
)
+
1
));
}
}
public
class
CloneMale : CloneHuman
{
public
override
CloneHuman CreateClone()
{
return
(CloneHuman)
this
.MemberwiseClone();
}
}
public
class
CloneFemale : CloneHuman
{
public
override
CloneHuman CreateClone()
{
return
(CloneHuman)
this
.MemberwiseClone();
}
}
测试程序
public
static
void
Main()
{
Client c
=
Client.Instance();
CloneMale cm
=
new
CloneMale();
CloneFemale cf
=
new
CloneFemale();
c.Register(
"
CloneMale
"
, cm);
c.Register(
"
CloneFemale
"
, cf);
for
(Int32 i
=
0
; i
<
10
; i
++
)
{
String name
=
(i
%
2
==
0
)
?
"
CloneMale
"
:
"
CloneFemale
"
;
CloneHuman ch
=
c.BuildCloneHuman(name);
ch.Show();
}
Console.ReadLine();
}
Prototype Demo
查看全文
相关阅读:
FileZilla 双向传输
移动端弱网测试工具
来源IP安全分析,对IP溯源
推荐几款移动端抓包小工具
一键清除Chrome
resit阶段二
redist集群
redits04 快照配置
ridts08管理工具
redits07配置文件
原文地址:https://www.cnblogs.com/Dabay/p/364762.html
最新文章
python3.7安装
使用sql语句将数据库中的一个表里的数据导入到另一个表中
Java-Spring-replaceAll()的用法
Codeforces Round #717 (div.2)
Codeforces Round #716 (div.2)
Codeforces Round #714 (div.2)
hdu6249
hdu4719 线段树优化dp
hdu1732
hdu4389
热门文章
hdu6171 双向bfs
hdu2510
hdu4300
继续复习
今天的复习
pytest 测试报告定期删除
TEP自动化测试工具
远程linux服务器搭建 Jenkins 环境
远程linux服务器搭建 Tomcat 环境
Linux服务器 安装jdk
Copyright © 2011-2022 走看看