【问题标题】:Problem about double dispatch with fixed class关于固定类的双重调度问题
【发布时间】:2011-09-26 14:44:58
【问题描述】:

我有 3 个班级:**Parent**、**Child1** 和 **Child2**。 Child1 和 Child 2 都扩展了 Parent 并且它们**不能**被修改。

有一个类Action定义如下:

public class Action {
    public static void perform(Parent p) {
        if (p instanceof Child1) {
            action((Child1)p);
        }
        else if (p instanceof Child2) {
            action((Child2)p);
        }
        else {
            action(p);
        }
    }

    private static void action(Parent p) {
        ...
    }
    private static void action(Child1 c1) {
        ...
    }
    private static void action(Child2 c2) {
        ...
    }
}


Parent p = new Child1();
Action.perform(p);

如果没有 instanceof 运算符,有没有办法获得与上述相同的行为?

======
(修改)

PS:Action.perform的给定参数类型是Parent,但它的值是Child1,所以我认为方法重载不起作用....

【问题讨论】:

标签: java oop design-patterns


【解决方案1】:

这里有两种 C# 解决方案,一种基本相同,但更简洁,适用于子类数量较少的情况。

public class Parent
{
}

public class Child1 : Parent
{
}

public class Child2 : Parent
{
}

public class Action
{
    private static Dictionary<Type, Action<Parent>> _d = new Dictionary<Type, Action<Parent>>()
                                                    {
                                                        {typeof(Child1), p=>action((Child1)p)},
                                                        {typeof(Child2), p=>action((Child2)p)},
                                                        {typeof(Parent), p=>action(p)}
                                                    };
    public static void perform(Parent p)
    {
        _d[p.GetType()](p);
    }

    private static void action(Parent p)
    {

    }

    private static void action(Child1 c1)
    {

    }
    private static void action(Child2 c2)
    {

    }
}



class Program
{
    static void Main(string[] args)
    {
        Parent p = new Child1();
        Action.perform(p);
    }
}`enter code here`

无论如何它会破坏 OCP,但比使用反射更快。这个不会破坏 OCP 而是使用反射来填充字典:

public class Action
        {
            private static Dictionary<Type, Action<Parent>> _d;

            static Action()
            {
                Initialize();
            }

            private static void Initialize()
            {
                var methods = typeof(Action)
                    .GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)
                    .Where(IsActionMethod)
                    .ToList();

                _d = methods.ToDictionary(m => m.GetParameters().First().ParameterType,
                                          m => (Action<Parent>) (x => m.Invoke(null, new[] {x})));
            }

            private static bool IsActionMethod(MethodInfo methodInfo)
            {
                var parameters = methodInfo.GetParameters();

                return parameters.Length == 1
                       && typeof(Parent).IsAssignableFrom(parameters.First().ParameterType)
                       && methodInfo.ReturnType == typeof(void);
            }

            public static void perform(Parent p)
            {
                _d[p.GetType()](p);
            }

            private static void action(Parent p)
            {

            }

            private static void action(Child1 c1)
            {

            }

            private static void action(Child2 c2)
            {

            }
}

【讨论】:

    【解决方案2】:

    不幸的是,我认为您必须借助反思来解决此问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-12
      • 2017-01-23
      相关资源
      最近更新 更多