重新设计面向个人的 OOP 拆分组合接口 [英] Redesign the OOP Splitting combined interfaces to individuals

查看:18
本文介绍了重新设计面向个人的 OOP 拆分组合接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解如何以干净且可设计"的方式避免重复输入以下场景:

I was trying to understand how to avoid double entry in a clean and "designable" way to the following scenario:

public interface ICommandAble
{ }

public interface ILogAble extends ICommandAble
{ }

public interface IMonitorAble extends ICommandAble
{ }

public abstract class ClassAbs
{ }

public class A extends ClassAbs implements IMonitorAble, ILogAble
{ }

测试方法:

public void test()
{
   A a=new A();
   List<ICommandAble>commandList=new ArrayList<ICommandAble>()
   if (a instanceof ILogAble)
   {
       ILogAble logAbleItem=(ILogAble)a;
       commandList.add(logAbleItem);
   }
   if (a instanceof IMonitorAble) {
       IMonitorAble monitorAbleItem=(IMonitorAble)a;
       commandList.add(monitorAbleItem);
   }
   for(ICommandAble item: commandList)
   {
        if(item instanceof IMonitorAble)
        {
          log.debug("is monitorable");
        }
        if(item instanceof ILogAble)
        {
          log.debug("is logable");
        }
}

输出为:

2013-04-22 18:25:00,498 com... [DEBUG] 可监控

2013-04-22 18:25:00,498 com... [DEBUG] is monitorable

2013-04-22 18:25:02,150 com.... [DEBUG] 是可记录的

2013-04-22 18:25:02,150 com.... [DEBUG] is logable

2013-04-22 18:25:08,322 com.... [DEBUG] 是可监控的

2013-04-22 18:25:08,322 com.... [DEBUG] is monitorable

2013-04-22 18:25:08,977 com.... [DEBUG] 是可记录的

2013-04-22 18:25:08,977 com.... [DEBUG] is logable

这将导致我的程序对每个xAble"进行两次执行

That will cause my program to do double executions for each "xAble"

我希望看到:

2013-04-22 18:25:00,498 com... [DEBUG] 可监控

2013-04-22 18:25:00,498 com... [DEBUG] is monitorable

2013-04-22 18:25:02,150 com.... [DEBUG] 是可记录的

2013-04-22 18:25:02,150 com.... [DEBUG] is logable

我应该如何(以干净的方式)重新设计第二个迭代器条件将基于引用类型而不是实例类型.得到这样的东西:

How should I redesign this(in a clean way) that the second iterator conditionion will be based on the reference type and not on the instance type. getting something like this:

2013-04-22 18:25:00,498 com... [DEBUG] is monitorable

2013-04-22 18:25:02,150 com.... [DEBUG] is logable

  • 想想将来我可能会有更多的能力"
  • 谢谢,射线.

    推荐答案

    如果您正在测试对象以查看它们是否是接口的实例,那么它们必须实现该接口.他们也总是可以实现其他接口.在您的场景中,要实现您需要的输出,您必须使对象仅实现两个接口之一.

    If you are testing objects to see if they are instances of interfaces then they must implement that interface. They can always implement other interfaces too. In your scenario, to achieve the output you need you must make the objects only implement one of the two interfaces.

    public class Test {
      public interface ICommandAble {
      }
    
      public interface ILogAble extends ICommandAble {
      }
    
      public interface IMonitorAble extends ICommandAble {
      }
    
      public abstract class ClassAbs {
      }
    
      public class A extends ClassAbs implements IMonitorAble, ILogAble {
      }
    
      public class B extends ClassAbs implements IMonitorAble {
      }
    
      public class C extends ClassAbs implements ILogAble {
      }
    
      public void test() {
        A a = new A();
        B b = new B();
        C c = new C();
        List<ICommandAble> commandList = new ArrayList<ICommandAble>();
        commandList.add(a); // Remove this line to just get the two printouts.
        commandList.add(b);
        commandList.add(c);
        for (ICommandAble item : commandList) {
          if (item instanceof IMonitorAble) {
            System.out.println(item.getClass().getSimpleName() + " is monitorable");
          }
          if (item instanceof ILogAble) {
            System.out.println(item.getClass().getSimpleName() + " is logable");
          }
        }
      }
    
      public static void main(String args[]) {
        new Test().test();
      }
    }
    

    印刷品

    A is monitorable
    A is logable
    B is monitorable
    C is logable
    

    我也在列表中添加了一个 A 来演示区别.

    I have added an A to the list too to demonstrate the difference.

    这篇关于重新设计面向个人的 OOP 拆分组合接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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