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
查看全文
相关阅读:
Python(九)之网络编程
Bat命令
Python(八)之函数
RedHat下安装Python开发环境
Redhat6.5安装DB2 Express-C版本
Linux下字符集的安装
Linux命令之stty
AIX查看CPU、内存等信息
stopManagedWebLogic.sh强制关闭Managed Server
Keepalived + Nginx + Tomcat 的高可用负载均衡架构搭建
原文地址:https://www.cnblogs.com/Dabay/p/364762.html
最新文章
阿里云centos7安装桌面环境
Mac或者linux下登陆到linux上的SFTP
Mac下终端使用密钥登录服务器
Mac下配置Oracle数据库客户端远程连接数据库服务器
Oracle 10.2.0.1 精简客户端配置
Oracle11g select查询时候输出未选定行
wine for MacOS
CentOS6.5安装wine
Mac下给SD卡烧录树莓派系统
ORA-00119: invalid specification for system parameter LOCAL_LISTENER
热门文章
mysql 5.6 windows 启动脚本
virtual box 安装centos min
FastCGI sent in stderr: "PHP Warning: simplexml_load_string(): Entity: line 1: parser error : Start tag expected, '<' not found in
树形展示形式的论坛
https openssl http2
持续集成
sugarCrm翻译
sugarCRM文档翻译1
nginx 403 forbidden
正则表达式, 贪婪模式
Copyright © 2011-2022 走看看