Jim*_*Jim 5 .net c# oop dynamic c#-4.0
鉴于以下计划:
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 . . .
我选择在这里使用dynamic来避免以下情况:
switch(foo.GetType().Name)    foo is Foo1(Foo1)foo由于dynamic转换,将ApplyFooDefaults根据传入的对象类型调用正确的方法HandleFoo(FooBase foo).任何没有适当ApplyFooDefaults处理方法的对象都属于"全部捕获"方法,ApplyFooDefaults(FooBase unhandledFoo).
这里的一个关键部分是FooBase和派生类表示我们无法控制的类型,无法从中添加其他接口.
这是动态的"好"用法还是可以在OOP方式下解决这个问题而不会增加额外的复杂性给定约束以及这只是为了在这些对象上设置默认属性值这一事实?
*更新*
在Bob Horn的回答之后,我意识到我的场景并不完整.附加限制:
.
那么,单个对象的初始化应该发生在类型的构造函数中。如果工厂未能做到这一点并仅输出具有基本类型的对象,那么显然基于类型初始化对象超出了 OOP 模式。
唉,运行时类型检测是可行的方法,而动态检测就是这样做的,所以是的,您的解决方案非常漂亮。(但第三方库不是,因为它强制您使用动态类型)
| 归档时间: | 
 | 
| 查看次数: | 192 次 | 
| 最近记录: |