zoukankan
html css js c++ java
抽象配置工厂
秉承一天写一篇的精神...今天的我又来了.....什么??前面有一天两天漏了?? 哦..那天水浸啦...
写的虽然不是很多旷世巨作,不也相信也不是什么很垃圾的东西..比较都是些程序的积累...经验吧...
今天要现的是
抽象配置工厂
的实现
配置的实现:
public
class
ProvidersHanders : IConfigurationSectionHandler
{
IConfigurationSectionHandler 成员
#region
IConfigurationSectionHandler 成员
public
object
Create(
object
parent,
object
configContext, System.Xml.XmlNode section)
{
XmlSerializer ser
=
new
XmlSerializer(
typeof
(ProvidersConfiguration));
return
ser.Deserialize(
new
XmlNodeReader(section));
}
#endregion
}
实现configurationsectionhandler接口
[Serializable()]
[XmlRoot(
"
ProvidersConfig
"
)]
public
class
ProvidersConfiguration
{
private
ProvidersCollection providers;
private
static
yansCache cache
=
yansCache.Instance;
public
ProvidersCollection Providers
{
get
{
return
providers; }
set
{ providers
=
value; }
}
public
static
ProvidersConfiguration GetConfig
{
get
{
if
(
null
==
cache[
"
ProvidersConfig
"
])
{
cache.Max(
"
ProvidersConfig
"
, ConfigurationManager.GetSection(
"
ProvidersConfig
"
));
}
return
(ProvidersConfiguration)cache[
"
ProvidersConfig
"
];
}
}
public
static
Type GetTypeByProviderName(
string
ProviderName)
{
foreach
(SingleProvider sp
in
GetConfig.Providers)
{
if
(sp.ProviderName
==
ProviderName)
{
return
Type.GetType(sp.Type);
}
}
throw
new
ArgumentNullException(
"
找不到类型:
"
+
ProviderName);
return
null
;
}
}
配置读取
[Serializable()]
public
class
ProvidersCollection : CollectionBase
{
public
virtual
void
Add(SingleProvider p)
{
InnerList.Add(p);
}
public
SingleProvider
this
[
int
index]
{
get
{
return
(SingleProvider)InnerList[index]; }
set
{ InnerList[index]
=
value; }
}
}
配置集合
[Serializable()]
public
class
SingleProvider
{
private
string
type;
public
string
Type
{
get
{
return
type; }
set
{ type
=
value; }
}
private
string
providerName;
public
string
ProviderName
{
get
{
return
providerName; }
set
{ providerName
=
value; }
}
}
单条配置
<
configSections
>
<
section
name
="ProvidersConfig"
type
="yansComponents.Handlers.ProvidersHanders, yansComponents"
/>
</
configSections
>
配置文件配置信息
/**/
///
<summary>
///
Providers模式父类
///
</summary>
///
<typeparam name="T"></typeparam>
public
class
BaseAbstract
<
T
>
{
根据类型获取实例
#region
根据类型获取实例
class
Ret
{
static
Ret()
{ }
//
internal static Hashtable instance = new Hashtable();
private
static
T instance;
public
static
T CreateInstance(Type type)
{
//
if (!instance.Contains(type.FullName))
//
{
//
instance.Add(type.FullName, (T)Activator.CreateInstance(type));
//
}
if
(
null
==
instance)
{
instance
=
(T)Activator.CreateInstance(type);
}
//
return (T)instance[type.FullName];
return
instance;
}
}
/**/
///
<summary>
///
根据类型返回一个实例
///
</summary>
///
<param name="type">
类型
</param>
///
<returns></returns>
public
static
T getInstance(Type type)
{
return
CreateInstance(type);
}
/**/
///
<summary>
///
创建实例
///
</summary>
///
<param name="type"></param>
///
<returns></returns>
protected
static
T CreateInstance(Type type)
{
if
(
null
==
type)
{
ThrowExceptionWhileTypeIsNull(type,
"
提交的类型不存在或为空,创建实例失败。
"
);
return
default
(T);
}
return
Ret.CreateInstance(type);
}
/**/
///
<summary>
///
提交的类型不存在或为空
///
</summary>
///
<param name="type"></param>
protected
static
void
ThrowExceptionWhileTypeIsNull(Type type,
string
ErrMessage)
{
throw
new
ArgumentNullException(ErrMessage);
}
#endregion
/**/
///
<summary>
///
获得实例
///
</summary>
public
static
T Instance
{
get
{
return
getInstance(ProvidersConfiguration.GetTypeByProviderName(
typeof
(T).Name));
}
}
}
抽象配置父类...
还有些东西没有摆出来...例如单个的配置啦..还是就是怎么样去使用它呀.....呵呵..有点懒...迟点记得再搞....
查看全文
相关阅读:
replace函数的使用(替换单个和全局)
数据库记录删除方式
UDP接收百万级数据的解决方案
早睡早起+微信朋友圈控制在10人以内…
java Date型时间比较大小
教会你时间管理的奥秘,提升工作效率。
大多数人都输给了这个字——等
二分法查找和普通查找
王者荣耀里拿个王者有啥了不起,有胆就来挑战一下ApsaraCache源码
给予Java初学者的学习路线建议
原文地址:https://www.cnblogs.com/yans/p/1225130.html
最新文章
关于SecureCRT链接服务器出现乱码的问题
关于windows下远程连接Linux服务器的方法(CentOs)
PHP 安装使用 gearman
关于安装Apache之后,解析PHP的配置
限制input只能输入金额(类似:100.00|100.9|100)
安装openssl 扩展的时候出现Cannot find config.m4. Make sure that you run '/usr/local/php/bin/phpize' in the top level source directory of the module的解决方法
ci框架登陆之后每隔几分钟就需要重新登录的问题
Redis安装
2017殚精竭力,2018宏图大业
不要自称为程序员
热门文章
IE、360浏览器select属性onchange遇到的坑
Spring mvc下Ajax获取JSON对象问题 406错误
SpringMVC Maven项目 java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServle
Freemarker-标签使用
怪不得知乎急着招前端开发.
eclipse maven引入第三方jar包后如何下载源代码(sources)
BeanUtils工具类
webservice接口国内手机号码归属地查询
js 循环list
百度的富文本编辑器UEditor批量添加图片自动加上宽度和高度的属性
Copyright © 2011-2022 走看看