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 多线程学习(转)
自己使用python webob,paste.deploy,wsgi总结
Python中*args 和**kwargs的用法
python 数字和字符串转换问题
python socket编程
深入解读Quartz的原理
解决get方法传递URL参数中文乱码和解决tomcat下中文乱码问题
Tomcat的Manager显示403 Access Denied
mysql5.6 linux下安装笔记
Quartz应用与集群原理分析
原文地址:https://www.cnblogs.com/Dabay/p/364762.html
最新文章
android java层实现hook替换method
Android热补丁技术—dexposed原理简析(阿里Hao)
android插件化简述
Android Hook技术
Andfix热修复原理
Zabbix 使用LLD低级别发现规则监控多个CPU的CPU使用率
统计访问Nginx的前十个IP
Ansible2.9.6 Yum安装
mysql:mysql is neither service nor target!?(suse12 sp2 )
zabbix实现对磁盘性能动态监控
热门文章
Linux系统启动过程(通俗易懂)
linux下sort命令详解
shell脚本学习之实例列举
shell脚本学习之for循环
shell脚本学习之case用法及实例
Linux securecrt破解
Python 时间整理
Python 以指定概率获取元素
使用curl操作openstack swift
Python 读取excel
Copyright © 2011-2022 走看看