简体   繁体   English

使用Java接口验证对象

[英]Verifying Objects using Java interfaces

I'm currently facing a design problem and would appreciate advice on how I could resolve it: 我目前正面临设计问题,并希望了解如何解决它:

The problem 问题

I will use an example to illustrate my problem note this is just an example: 我将用一个例子来说明我的问题,这只是一个例子:

Suppose you have an interface called Pass with methods listed: 假设您有一个名为Pass的接口,其中列出了以下方法:

public interface Pass {
  public boolean hasPassedA();
      public boolean hasPassedB();
      public boolean hasPassedC();
}

Suppose you have a class which implement this interface called Assessor: 假设您有一个实现此接口的类,名为Assessor:

public class Assessor implements Pass{
// how should I implement this class ?? 
}

Finally Student class: 最后学生班:

public class Student {
  // some code that defines student behaviour not important.
}

The question is then how can I make the interaction between the Assessor and the student object a lot more flexible? 那么问题是如何使Assessor和学生对象之间的交互更加灵活?

What I noticed is that an Assessor object should be something that is abstract because in reality there is no such thing as an Assessor, but instead you have different types of assessors such as a Math Assessor or English Assessor etc, which in turn will allow me to create different types of Assessor objects eg 我注意到的是,Assessor对象应该是抽象的东西,因为实际上没有评估者这样的东西,而是你有不同类型的评估者,如数学评估员或英语评估员等,这反过来将允许我创建不同类型的Assessor对象,例如

MathAssessor extends Assessor
EnglishAssessor extends Assessor

The concept is that a Student can pass if all the methods declared in the Pass interface return true and all additional methods in the subjectAssessor classes return true. 如果Pass接口中声明的所有方法都返回true并且subjectAssessor类中的所有其他方法都返回true,则该概念是Student可以传递。

What do I do in the Assessor class? 我在Assessor课上做什么? I have read about adapter design patterns but haven't fully grasped that notion or does it even apply to this situation? 我已经阅读过有关适配器设计模式的内容,但还没有完全理解这个概念,或者它是否适用于这种情况?

To start, the Pass interface you have is not very flexible, which could make for difficulties. 首先,你拥有的Pass接口不是很灵活,这可能会带来困难。 For example, what if one implementation of Pass only needs to have hasPassedA, or you have an implementation which needs hasPassedA, hasPassedB, hasPassedC and hasPassedD. 例如,如果Pass的一个实现只需要hasPassedA,或者你有一个需要hasPassedA,hasPassedB,hasPassedC和hasPassedD的实现,该怎么办? Then the various types of assessors will need to figure out which pass conditions to check. 然后,各种类型的评估员需要确定要检查的通过条件。

A more flexible way to do this might be to do something like this. 更灵活的方法可能是做这样的事情。 Rather than having a Pass interface, maybe something like a Condition interface (the names of the classes/interfaces should be changed to make sense for your domain). 可能类似于Condition接口(类/接口的名称应该更改为对您的域有意义),而不是具有Pass接口。

public interface Condition {

   // true means the condition passed, false means it did not
   boolean evalutate();
}

Now you could have a single Assessor class (I'm not sure if this is exactly how your assessor would work, but it's just a guideline): 现在你可以有一个评估员课程(我不确定这是否正是评估员的工作方式,但这只是一个指导原则):

public class Assessor {

   boolean assess(Collection<Condition> conditions) {
      for (Condition c : conditions) {
        if (!c.evaluate()) {
           return false;
        }
      }
      // all conditions passed
      return true;
   }
}

Hopefully this helps for your problem. 希望这有助于解决您的问题。

First off, to answer your question about the adapter pattern, it doesn't apply here. 首先,要回答有关适配器模式的问题,它不适用于此处。 You use the adapter pattern to add a layer between 2 incompatible systems to allow them to pass data back and forth. 您使用适配器模式在两个不兼容的系统之间添加一个层,以允许它们来回传递数据。

Using your example, I would recommend writing default implementations of the hasPassed_() methods in Assessor, even if the implementation is nothing more than throwing a new UnsupportedOperationException (to answer the question about what if a particular Assessor only needs a subset of hasPassed_() methods you can just overwrite only the ones you need). 使用您的示例,我建议在Assessor中编写hasPassed_()方法的默认实现,即使实现只是抛出一个新的UnsupportedOperationException(回答关于如果特定Assessor只需要hasPassed_()的子集的问题你只能覆盖你需要的方法)。 You can modify the subject assessor's (eg MathAssessor, EnglishAssessor, etc.) Pass methods to be more specific or to provide additional checks before calling super.hasPassed_() (depending on your specific implementation). 您可以修改主题评估者(例如MathAssessor,EnglishAssessor等)。在调用super.hasPassed_()之前,将Pass方法更具体或提供额外的检查(具体取决于您的具体实现)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM