哪种设计模式允许基于运行时类型抽象功能? [英] Which design pattern allows abstraction of functionality based on runtime types?

查看:96
本文介绍了哪种设计模式允许基于运行时类型抽象功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个抽象类A和它的几个实现。我希望这可以随着时间的推移,通过添加更多的实现。



我还有一个接口,在上述类层次结构的实例中(例如打印它们)。 / p>

我希望接口的实现为A的一些子类提供一些特殊功能,并为其余的子类提供默认功能。



我希望这个例子澄清一下:

 抽象类A {} 
class B extends A {}
class C扩展A {}

接口处理器{
public void process(A a);
}

class SimpleProcessor实现处理器{

//当我的参数是A或C的实例或任何
//时,我想要调用它将来会添加的新类
public void process(A a){
//行14
System.out.println(默认处理);
}

//当我的参数是B
的实例时,我想要调用此参数public void process(B b){
System.out.println(特殊加工);
}

}

public class Runner {

public static void main(String [] args){
B b = new B();
处理器p = new SimpleProcessor();
p.process(b);
}

}

该示例打印默认处理 。问题是要根据接口方法的编译时类型选择要执行的方法。有没有一种方式(或设计模式)使这个程序打印特殊处理,而不在第14行添加一个列表

 如果(B的一个实例)
进程((B)a);

需要特殊处理的每个课程?



我看了一下访客模式,但似乎并不是一个改进,因为我不想用A的每个子类的方法来污染处理器接口,因为A的更多子类将被添加。



换句话说,我希望接口的实现:




  • 提供A的特定子类的方法的自定义实现

  • 为将要添加的类提供默认实现

  • 避免列出大型if-then-else列表中的所有类



    • 谢谢!!

      解决方案

      如果您创建一个适配器您要处理的对象并返回该对象的处理器?

       如果A - >返回ProcessorA 
      如果B - >返回ProcessorB

      代码示例:

        class Adapter {

      处理器getProcessor(Object o){
      if(o的实例){
      返回新的ProcessorA();
      } else if ...
      }

      }


      I have an abstract class A and several implementations of it. I expect this to evolve over time, by adding more implementations.

      I also have an interface that does something at instances of the above class hierarchy (eg print them).

      I want implementations of the interface to provide some special functionality for some of the subclasses of A and a default functionality for the rest of them.

      I hope this example clarifies things:

      abstract class A { }
      class B extends A { }
      class C extends A { }
      
      interface Processor  {
          public void process(A a);
      }
      
      class SimpleProcessor implements Processor {
      
          //I want this to be called when argument is instance of A or C or any
          //new class that will be added in the future
          public void process(A a) {
              //Line 14
              System.out.println("Default processing");
          }
      
          //I want this to be called when argument is instance of B
          public void process(B b) {
              System.out.println("Special processing");
          }
      
      }
      
      public class Runner {
      
          public static void main(String[] args) {
              B b = new B();
              Processor p = new SimpleProcessor();
              p.process(b);
          }
      
      }
      

      The example prints "Default processing". The problem is that the method to be executed is chosen based at the compile-time type of the interface's method. Is there a way (or design pattern) to make this program print "Special processing" without adding at line 14 a list of

      if (a instance of B)
        process( (B) a );
      

      for every class that needs special processing?

      I had a look at the visitor pattern but it doesn't seem like an improvement because I don't want to "pollute" the Processor interface with methods for every subclass of A because more subclasses of A will be added.

      To put it another way, I want the implementations of the interface to:

      • provide custom implementation of the method for specific subclasses of A
      • provide a default implementation for classes that will be added in the future
      • avoid listing all the classes in a large if-then-else list

      Thanks!!

      解决方案

      How about if you create an Adapter which take the object you want to process and return the processor for that object?

      if A -> return ProcessorA
      if B -> return ProcessorB
      

      code example:

      class Adapter {
      
          Processor getProcessor(Object o) {
              if (o instance of A) {
                  return new ProcessorA();
              } else if ...
          }
      
      }
      

      这篇关于哪种设计模式允许基于运行时类型抽象功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆