【代码保留】成对值类(PairCollection和Pair)
第一次泛型编程(以前从来也只有用过List<T>之类的)
不知道会有什么问题呢?继承IList的部分没有注释了,MSDN可以查的。欢迎提意见噢~
你可能会看到下面的类用多种方式实现了同样的效果,有多态,有模版,还有包装wrapper(不知道是不是恰当)……还用了泛型……
实际项目中,如果有可取之处,请只取其中一种就可以了……(计划中……)
补充一张VS自动生成的类图
不知道会有什么问题呢?继承IList的部分没有注释了,MSDN可以查的。欢迎提意见噢~
你可能会看到下面的类用多种方式实现了同样的效果,有多态,有模版,还有包装wrapper(不知道是不是恰当)……还用了泛型……
实际项目中,如果有可取之处,请只取其中一种就可以了……(计划中……)
补充一张VS自动生成的类图
using System;
using System.Collections.Generic;
using System.Collections;
namespace CA_PairCollection
{
/// <summary>
/// 基础格式化机制
/// </summary>
/// <typeparam name="T"></typeparam>
interface IFormat<T>
{
T GetFormat();
}
/// <summary>
/// 格式化模板接口
/// </summary>
interface IFormatTemplate
{
/// <summary>
/// Text模版,形如:"Text = "
/// </summary>
string TextTemplate { get;set;}
/// <summary>
/// Value模版,形如:"Value = "
/// </summary>
string ValueTemplate { get;set;}
/// <summary>
/// 分隔符模版,形如:","
/// </summary>
string Separator { get;set;}
}
/// <summary>
/// 提供用于格式化对象的模板的格式化机制
/// </summary>
interface IFormatProvider
{
string GetFormat(IFormatTemplate formatTemplate);
}
/// <summary>
/// 格式化模板类
/// </summary>
class FormatTemplate : IFormatTemplate
{
public FormatTemplate(string textTemplate,string separator,string valueTemplate)
{
this.textTemplate = textTemplate;
this.separator = separator;
this.valueTemplate = valueTemplate;
}
private string textTemplate;
private string separator;
private string valueTemplate;
IFormatTemplate 成员
}
/// <summary>
/// 成对参数类
/// </summary>
/// <typeparam name="U"></typeparam>
/// <typeparam name="V"></typeparam>
class Pair<U, V> : IFormat<string>, IFormatProvider
{
public Pair() { }
public Pair(U text, V value)
{
this.text = text;
this.value = value;
}
private U text;
public U Text
{
get
{
return this.text;
}
set
{
this.text = value;
}
}
private V value;
public V Value
{
get
{
return this.value;
}
set
{
this.value = value;
}
}
IFormat 成员
IFormatProvider 成员
}
/// <summary>
/// 成对参数类集合
/// </summary>
/// <typeparam name="U"></typeparam>
/// <typeparam name="V"></typeparam>
class PairCollection<U, V> : IList<Pair<U, V>>, IFormat<string>, IFormatProvider
{
private IList<Pair<U, V>> pairs;
public PairCollection()
{
pairs = new List<Pair<U, V>>();
}
IListPairU,V 成员
ICollectionPairU,V 成员
IEnumerablePairU,V 成员
IEnumerable 成员
IFormat 成员
IFormatProvider 成员
}
/// <summary>
/// 实现了中文式成对参数的类
/// </summary>
/// <typeparam name="U"></typeparam>
/// <typeparam name="V"></typeparam>
sealed class ChinesePair<U, V> : Pair<U, V>
{
public ChinesePair()
: base()
{ }
public ChinesePair(U text,V value)
: base(text, value)
{ }
public override string GetFormat()
{
return "文本 = " + this.Text.ToString() + "; 值 = " + this.Value.ToString();
}
}
/// <summary>
/// 实现了英文式成对参数的类
/// </summary>
/// <typeparam name="U"></typeparam>
/// <typeparam name="V"></typeparam>
sealed class EnglishPair<U, V> : Pair<U, V>
{
public EnglishPair()
: base()
{ }
public EnglishPair(U text, V value)
: base(text, value)
{ }
public override string GetFormat()
{
return "Text = " + this.Text.ToString() + "; Value = " + this.Value.ToString();
}
}
/// <summary>
/// 键值均为string类型的一个类型包装
/// </summary>
class KVPairCollection : PairCollection<string, string>,IFormat<string>
{
private PairCollection<string, string> p;
public KVPairCollection()
{
p = new PairCollection<string, string>();
}
IFormat 成员
}
class Program
{
static void Main(string[] args)
{
//
//以下示例用多种方式实现了成对类的格式化输出
//
PairCollection<string, string> psBases = new PairCollection<string, string>();
psBases.Add(new Pair<string, string>("baseText1", "baseValue1"));
psBases.Add(new Pair<string, string>("baseText2", "baseValue2"));
foreach (Pair<string, string> p in psBases)
{
Console.WriteLine(p.GetFormat());
}
Console.WriteLine("----------------------");
PairCollection<string, string> psPloyStrings = new PairCollection<string, string>();
psPloyStrings.Add(new ChinesePair<string, string>("textChinese1", "valueChinese1"));
psPloyStrings.Add(new EnglishPair<string, string>("textEnglish1", "valueEnglish1"));
foreach (Pair<string, string> p in psPloyStrings)
{
Console.WriteLine(p.GetFormat());
}
Console.WriteLine("----------------------");
PairCollection<string, int> psPloyInts = new PairCollection<string, int>();
psPloyInts.Add(new ChinesePair<string, int>("textChinese1", 1));
psPloyInts.Add(new EnglishPair<string, int>("textEnglish1", 2));
foreach (Pair<string, int> p in psPloyInts)
{
Console.WriteLine(p.GetFormat());
}
Console.WriteLine("----------------------");
Console.WriteLine(psPloyStrings.GetFormat());
Console.WriteLine(psPloyInts.GetFormat());
Console.WriteLine("----------------------");
KVPairCollection kvs = new KVPairCollection();
kvs.Add(new ChinesePair<string, string>("textChinese1", "valueChinese1"));
kvs.Add(new EnglishPair<string, string>("textEnglish1", "valueEnglish1"));
kvs.Add(new Pair<string, string>("textBase1", "valueBase"));
Console.WriteLine(kvs.GetFormat());
Console.WriteLine("----------------------");
PairCollection<string, string> pFormatProvider = new PairCollection<string, string>();
pFormatProvider.Add(new ChinesePair<string, string>("textChinese1", "valueChinese1"));
pFormatProvider.Add(new EnglishPair<string, string>("textEnglish1", "valueEnglish1"));
Console.WriteLine(pFormatProvider.GetFormat((IFormatTemplate)(new FormatTemplate("模版Text = "," ; ", "模版Value = "))));
}
}
}
using System.Collections.Generic;
using System.Collections;
namespace CA_PairCollection
{
/// <summary>
/// 基础格式化机制
/// </summary>
/// <typeparam name="T"></typeparam>
interface IFormat<T>
{
T GetFormat();
}
/// <summary>
/// 格式化模板接口
/// </summary>
interface IFormatTemplate
{
/// <summary>
/// Text模版,形如:"Text = "
/// </summary>
string TextTemplate { get;set;}
/// <summary>
/// Value模版,形如:"Value = "
/// </summary>
string ValueTemplate { get;set;}
/// <summary>
/// 分隔符模版,形如:","
/// </summary>
string Separator { get;set;}
}
/// <summary>
/// 提供用于格式化对象的模板的格式化机制
/// </summary>
interface IFormatProvider
{
string GetFormat(IFormatTemplate formatTemplate);
}
/// <summary>
/// 格式化模板类
/// </summary>
class FormatTemplate : IFormatTemplate
{
public FormatTemplate(string textTemplate,string separator,string valueTemplate)
{
this.textTemplate = textTemplate;
this.separator = separator;
this.valueTemplate = valueTemplate;
}
private string textTemplate;
private string separator;
private string valueTemplate;
IFormatTemplate 成员
}
/// <summary>
/// 成对参数类
/// </summary>
/// <typeparam name="U"></typeparam>
/// <typeparam name="V"></typeparam>
class Pair<U, V> : IFormat<string>, IFormatProvider
{
public Pair() { }
public Pair(U text, V value)
{
this.text = text;
this.value = value;
}
private U text;
public U Text
{
get
{
return this.text;
}
set
{
this.text = value;
}
}
private V value;
public V Value
{
get
{
return this.value;
}
set
{
this.value = value;
}
}
IFormat
IFormatProvider 成员
}
/// <summary>
/// 成对参数类集合
/// </summary>
/// <typeparam name="U"></typeparam>
/// <typeparam name="V"></typeparam>
class PairCollection<U, V> : IList<Pair<U, V>>, IFormat<string>, IFormatProvider
{
private IList<Pair<U, V>> pairs;
public PairCollection()
{
pairs = new List<Pair<U, V>>();
}
IListPairU,V 成员
ICollectionPairU,V 成员
IEnumerablePairU,V 成员
IEnumerable 成员
IFormat 成员
IFormatProvider 成员
}
/// <summary>
/// 实现了中文式成对参数的类
/// </summary>
/// <typeparam name="U"></typeparam>
/// <typeparam name="V"></typeparam>
sealed class ChinesePair<U, V> : Pair<U, V>
{
public ChinesePair()
: base()
{ }
public ChinesePair(U text,V value)
: base(text, value)
{ }
public override string GetFormat()
{
return "文本 = " + this.Text.ToString() + "; 值 = " + this.Value.ToString();
}
}
/// <summary>
/// 实现了英文式成对参数的类
/// </summary>
/// <typeparam name="U"></typeparam>
/// <typeparam name="V"></typeparam>
sealed class EnglishPair<U, V> : Pair<U, V>
{
public EnglishPair()
: base()
{ }
public EnglishPair(U text, V value)
: base(text, value)
{ }
public override string GetFormat()
{
return "Text = " + this.Text.ToString() + "; Value = " + this.Value.ToString();
}
}
/// <summary>
/// 键值均为string类型的一个类型包装
/// </summary>
class KVPairCollection : PairCollection<string, string>,IFormat<string>
{
private PairCollection<string, string> p;
public KVPairCollection()
{
p = new PairCollection<string, string>();
}
IFormat
}
class Program
{
static void Main(string[] args)
{
//
//以下示例用多种方式实现了成对类的格式化输出
//
PairCollection<string, string> psBases = new PairCollection<string, string>();
psBases.Add(new Pair<string, string>("baseText1", "baseValue1"));
psBases.Add(new Pair<string, string>("baseText2", "baseValue2"));
foreach (Pair<string, string> p in psBases)
{
Console.WriteLine(p.GetFormat());
}
Console.WriteLine("----------------------");
PairCollection<string, string> psPloyStrings = new PairCollection<string, string>();
psPloyStrings.Add(new ChinesePair<string, string>("textChinese1", "valueChinese1"));
psPloyStrings.Add(new EnglishPair<string, string>("textEnglish1", "valueEnglish1"));
foreach (Pair<string, string> p in psPloyStrings)
{
Console.WriteLine(p.GetFormat());
}
Console.WriteLine("----------------------");
PairCollection<string, int> psPloyInts = new PairCollection<string, int>();
psPloyInts.Add(new ChinesePair<string, int>("textChinese1", 1));
psPloyInts.Add(new EnglishPair<string, int>("textEnglish1", 2));
foreach (Pair<string, int> p in psPloyInts)
{
Console.WriteLine(p.GetFormat());
}
Console.WriteLine("----------------------");
Console.WriteLine(psPloyStrings.GetFormat());
Console.WriteLine(psPloyInts.GetFormat());
Console.WriteLine("----------------------");
KVPairCollection kvs = new KVPairCollection();
kvs.Add(new ChinesePair<string, string>("textChinese1", "valueChinese1"));
kvs.Add(new EnglishPair<string, string>("textEnglish1", "valueEnglish1"));
kvs.Add(new Pair<string, string>("textBase1", "valueBase"));
Console.WriteLine(kvs.GetFormat());
Console.WriteLine("----------------------");
PairCollection<string, string> pFormatProvider = new PairCollection<string, string>();
pFormatProvider.Add(new ChinesePair<string, string>("textChinese1", "valueChinese1"));
pFormatProvider.Add(new EnglishPair<string, string>("textEnglish1", "valueEnglish1"));
Console.WriteLine(pFormatProvider.GetFormat((IFormatTemplate)(new FormatTemplate("模版Text = "," ; ", "模版Value = "))));
}
}
}
posted on 2007-08-21 03:27 volnet(可以叫我大V) 阅读(468) 评论(0) 编辑 收藏 举报