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

查看:89
本文介绍了重新设计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年4月22日18:25:00498 COM ... [DEBUG]是可监测

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

2013年4月22日18:25:02150 COM ... [DEBUG]是logable

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

2013年4月22日18:25:08322 COM ... [DEBUG]是可监测

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

2013年4月22日18:25:08977 COM ... [DEBUG]是logable

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年4月22日18:25:00498 COM ... [DEBUG]是可监测

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

2013年4月22日18:25:02150 COM ... [DEBUG]是logable

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

我应该如何重新设计这个(在一个干净的方式)的第二个迭代器conditionion将根据引用类型,而不是实例类型。得到的东西是这样的:

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


  • 认为,在未来我可能有额外的冷杉

  • 谢谢,
    射线。

    Thanks, ray.

    推荐答案

    如果您正在测试的对象,看看他们是实例的接口则必须实现该接口。他们可以始终贯彻其它接口了。在您的方案,以达到你需要,你必须使对象只需要实现两个接口之一输出。

    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天全站免登陆