示例名称:简单计算器
示例说明:定义一个抽象类(Calculate)和其中的抽象方法(Result),定义两个子类(Add、Reduce)继承自该抽象类,并实现该类的抽象方法(Result)。抽象类(Calculate)中定义一个方法(Creat)根据变化的条件,负责创建不确定的类的实例。
示例类图:
关键部分说明
web.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="Calculate">
<section name="CalculateGroup"
type="System.Configuration.NameValueSectionHandler, System,Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
</sectionGroup>
</configSections>
<Calculate>
<CalculateGroup>
<add key="add" value=" David.Calculator.Add"/>
<add key="reduce" value=" David.Calculator.Reduce"/>
</CalculateGroup>
</Calculate>
</configuration>
其中的<configuration>
<configSections>
<sectionGroup name="Calculate">
<section name="CalculateGroup"
type="System.Configuration.NameValueSectionHandler, System,Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
</sectionGroup>
</configSections>
<Calculate>
<CalculateGroup>
<add key="add" value=" David.Calculator.Add"/>
<add key="reduce" value=" David.Calculator.Reduce"/>
</CalculateGroup>
</Calculate>
</configuration>
<add key="add" value=" David.Calculator.Add"/>
<add key="reduce" value=" David.Calculator.Reduce"/>
分别是继承抽象类(Calculate)的两个子类的标识-key,类的完全限定名-value,key将来做为方法(Creat)的参数传递.<add key="reduce" value=" David.Calculator.Reduce"/>
Calculate抽象类
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Configuration;
using System.Collections;
using System.Collections.Specialized;
namespace David.Calculator
{
public abstract class Calculate
{
private static Dictionary<string, string> types = new Dictionary<string, string>();
public static Dictionary<string, string> Types
{
get
{
if (types == null || types.Count == 0)
{
NameValueCollection config = new NameValueCollection();
config = (NameValueCollection)ConfigurationManager.GetSection("Calculate/CalculateGroup");
if (config != null)
{
foreach (string name in config)
{
types.Add(name, config[name]);
}
}
}
return types;
}
set
{
types = value;
}
}
public static Calculate Creat(string method)
{
Dictionary<string, string>.Enumerator myenu = Types.GetEnumerator();
while (myenu.MoveNext())
{
if (string.Compare(myenu.Current.Key, method, false) == 0)
{
return (Calculate)Activator.CreateInstance(Locate(myenu.Current.Value));
}
}
return null;
}
private static Type Locate(string typeName)
{
if (typeName == null || typeName == string.Empty)
return null;
Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
for (int i = 0; i < assemblies.Length; i++)
{
Assembly assembly = assemblies[i];
Type type = assembly.GetType(typeName, false);
if (type != null)
return type;
}
return null;
}
public abstract double Result(double a, double b);
}
}
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Configuration;
using System.Collections;
using System.Collections.Specialized;
namespace David.Calculator
{
public abstract class Calculate
{
private static Dictionary<string, string> types = new Dictionary<string, string>();
public static Dictionary<string, string> Types
{
get
{
if (types == null || types.Count == 0)
{
NameValueCollection config = new NameValueCollection();
config = (NameValueCollection)ConfigurationManager.GetSection("Calculate/CalculateGroup");
if (config != null)
{
foreach (string name in config)
{
types.Add(name, config[name]);
}
}
}
return types;
}
set
{
types = value;
}
}
public static Calculate Creat(string method)
{
Dictionary<string, string>.Enumerator myenu = Types.GetEnumerator();
while (myenu.MoveNext())
{
if (string.Compare(myenu.Current.Key, method, false) == 0)
{
return (Calculate)Activator.CreateInstance(Locate(myenu.Current.Value));
}
}
return null;
}
private static Type Locate(string typeName)
{
if (typeName == null || typeName == string.Empty)
return null;
Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
for (int i = 0; i < assemblies.Length; i++)
{
Assembly assembly = assemblies[i];
Type type = assembly.GetType(typeName, false);
if (type != null)
return type;
}
return null;
}
public abstract double Result(double a, double b);
}
}
本类共有五个成员:
1) private static Dictionary<string, string> types
2) public static Dictionary<string, string> Types:读取Web.config配置节Calculate/CalculateGroup的配置,将类key和value做为Dictionary<string, string>放入types私有变量中。
3) public static Calculate Creat(string method):通过判断传递的method字符串是否等于Web.config配置节Calculate/CalculateGroup的key,创建完全限定名等于对应的value值的类型的实例。
4) private static Type Locate(string typeName):根据类型的完全限定名在当前程序域得到类型的实例
5) public abstract double Result(double a, double b) :抽象方法需子类实现。
Add和Reduce类
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Xml;
namespace David.Calculator
{
public class Add : Calculate
{
public override double Result(double a, double b)
{
return a + b;
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace David.Calculator
{
public class Reduce : Calculate
{
public override double Result(double a, double b)
{
return a - b;
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Xml;
namespace David.Calculator
{
public class Add : Calculate
{
public override double Result(double a, double b)
{
return a + b;
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace David.Calculator
{
public class Reduce : Calculate
{
public override double Result(double a, double b)
{
return a - b;
}
}
}
继承Calculate类型,实现它的Result抽象方法。
调用
using System;
using System.Collections.Generic;
using System.Text;
namespace David.Calculator
{
class Program
{
static void Main(string[] args)
{
Calculate calculate = Calculate.Creat("add");
//Calculate calculate = Calculate.Creat("reduce");
Console.WriteLine(calculate.Result(1, 1));
Console.Read();
}
}
}
using System.Collections.Generic;
using System.Text;
namespace David.Calculator
{
class Program
{
static void Main(string[] args)
{
Calculate calculate = Calculate.Creat("add");
//Calculate calculate = Calculate.Creat("reduce");
Console.WriteLine(calculate.Result(1, 1));
Console.Read();
}
}
}