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
查看全文
相关阅读:
Servlet的数据库访问
Servlet 网页重定向
Intellij idea创建javaWeb以及Servlet简单实现
Tomcat
QQ简易版
单例
centos7 jdk安装
centos7 allure安装
centos中执行apt-get命令提示apt-get command not found
centos mysql使用踩过的坑
原文地址:https://www.cnblogs.com/Dabay/p/364762.html
最新文章
面向对象
pytest
常见第三方库安装失败解决方法
Python中调用其他程序的方式
整理-----内置函数
整理------函数
整理--数据类型,文件操作
el-load上传
Docker配置文件启动 MongoDB单点集群 MongoShake增量同步
SpringBoot+Redis 为何自动使用Redis
热门文章
vue打包 部署到TomCat刷新404解决方法
java连接 MongoDB
MongoDB入门
docker常用命令
自定义注解-并判断方法或字段是否设置了注解
java存储区域
网络协议/网络协议的所在层
大数据性能优化
参数获取
过滤器
Copyright © 2011-2022 走看看