给定以下程序:
using System;
using System.Collections.Generic;
namespace ConsoleApplication49
{
using FooSpace;
class Program
{
static void Main(string[] args)
{
IEnumerable<FooBase> foos = FooFactory.CreateFoos();
foreach (var foo in foos)
{
HandleFoo(foo);
}
}
private static void HandleFoo(FooBase foo)
{
dynamic fooObject = foo;
ApplyFooDefaults(fooObject);
}
private static void ApplyFooDefaults(Foo1 foo1)
{
foo1.Name = "Foo 1";
Console.WriteLine(foo1);
}
private static void ApplyFooDefaults(Foo2 foo2)
{
foo2.Name = "Foo 2";
foo2.Description = "SomeDefaultDescription";
Console.WriteLine(foo2);
}
private static void ApplyFooDefaults(Foo3 foo3)
{
foo3.Name = "Foo 3";
foo3.MaxSize = Int32.MaxValue;
Console.WriteLine(foo3);
}
private static void ApplyFooDefaults(Foo4 foo4)
{
foo4.Name = "Foo 4";
foo4.MaxSize = 99999999;
foo4.EnableCache = true;
Console.WriteLine(foo4);
}
private static void ApplyFooDefaults(FooBase unhandledFoo)
{
unhandledFoo.Name = "Unhandled Foo";
Console.WriteLine(unhandledFoo);
}
}
}
/////////////////////////////////////////////////////////
// Assume this namespace comes from a different assembly
namespace FooSpace
{
////////////////////////////////////////////////
// these cannot be changed, assume these are
// from the .Net framework or some 3rd party
// vendor outside of your ability to alter, in
// another assembly with the only way to create
// the objects is via the FooFactory and you
// don't know which foos are going to be created
// due to configuration.
public static class FooFactory
{
public static IEnumerable<FooBase> CreateFoos()
{
List<FooBase> foos = new List<FooBase>();
foos.Add(new Foo1());
foos.Add(new Foo2());
foos.Add(new Foo3());
foos.Add(new Foo4());
foos.Add(new Foo5());
return foos;
}
}
public class FooBase
{
protected FooBase() { }
public string Name { get; set; }
public override string ToString()
{
return String.Format("Type = {0}, Name=\"{1}\"", this.GetType().FullName, this.Name);
}
}
public sealed class Foo1 : FooBase
{
internal Foo1() { }
}
public sealed class Foo2 : FooBase
{
internal Foo2() { }
public string Description { get; set; }
public override string ToString()
{
string baseString = base.ToString();
return String.Format("{0}, Description=\"{1}\"", baseString, this.Description);
}
}
public sealed class Foo3 : FooBase
{
internal Foo3() { }
public int MaxSize { get; set; }
public override string ToString()
{
string baseString = base.ToString();
return String.Format("{0}, MaxSize={1}", baseString, this.MaxSize);
}
}
public sealed class Foo4 : FooBase
{
internal Foo4() { }
public int MaxSize { get; set; }
public bool EnableCache { get; set; }
public override string ToString()
{
string baseString = base.ToString();
return String.Format("{0}, MaxSize={1}, EnableCache={2}", baseString,
this.MaxSize,
this.EnableCache);
}
}
public sealed class Foo5 : FooBase
{
internal Foo5() { }
}
////////////////////////////////////////////////
}
产生以下输出:
Type = ConsoleApplication49.Foo1, Name="Foo 1"
Type = ConsoleApplication49.Foo2, Name="Foo 2", Description="SomeDefaultDescription"
Type = ConsoleApplication49.Foo3, Name="Foo 3", MaxSize=2147483647
Type = ConsoleApplication49.Foo4, Name="Foo 4", MaxSize=99999999, EnableCache=True
Type = ConsoleApplication49.Foo5, Name="Unhandled Foo"
Press any key to continue . . .
我选择在这里使用动态以避免以下情况:
- 使用 switch/if/else 语句,例如
switch(foo.GetType().Name)
- 显式类型检查语句,例如
foo is Foo1
- 显式转换语句,例如
(Foo1)foo
由于dynamic
转换,正确的ApplyFooDefaults
方法会根据传入的对象类型被调用HandleFoo(FooBase foo)
。任何没有适当ApplyFooDefaults
处理程序方法的对象都属于“catch all”方法,ApplyFooDefaults(FooBase unhandledFoo)
.
这里的一个关键部分是 FooBase 和派生类代表了我们无法控制的类型,并且无法派生以添加额外的接口。
这是动态的“好”用途,还是可以在不增加额外复杂性的情况下以 OOP 方式解决这个问题,并且考虑到约束以及这只是在这些对象上设置默认属性值的事实?
*更新*
在 Bob Horn 的回答之后,我意识到我的场景并不完整。附加约束:
- 你不能直接创建Foos,你必须使用FooFactory。
- 您不能假设 Foo 类型,因为 Foo 类型是在配置中指定的并且是反射式创建的。
.