简体   繁体   English

在Abstract类上实现侦听器是个好主意吗?

[英]Is it a bad or good idea to implement a Listener on an Abstract class?

我正在为当前使用Java(这里是新手程序员)的游戏编写代码, 但是我不确定是否存在危险或好处,或者如果应该的话,不确定是否具有初始化显示模式并进入游戏循环的抽象类 ,该类实现了关键侦听器用于控制

Abstract Class is between Interface and and BaseClass ie it may have some implemented methods and some unimplemented methods. Abstract ClassInterfaceBaseClass之间,即它可以具有一些已实现的方法和一些未实现的方法。

All methods of sub classes which have common implementation can be implemented in Abstract class itself. 具有通用实现的所有子类方法都可以在Abstract类本身中实现。

If you think your Listener implementation is common across most of the implementing implementing classes of the abstract class, then I think it's completely fine to define the listener in the abstract class. 如果您认为您的Listener实现在抽象类的大多数实现类中是通用的,那么我认为在抽象类中定义侦听器是完全可以的。 If implementation is not same but the same method is required in most of the implementing classes, then define the signature as an abstract method in the abstract class. 如果实现不同,但是大多数实现类都需要相同的方法,则将签名定义为抽象类中的抽象方法。 If nothing is common, then I think you had better not have it in the abstract class. 如果没有什么共同之处,那么我认为您最好不要在抽象类中使用它。

There are two reasons (that I can think of) you want to prefer interfaces over abstract classes for your listeners. 对于我的侦听器,有两个原因(我可以想到),您希望使用接口而不是抽象类。

  1. If you have multiple listeners that are all marked abstract, a class can only inherit from one, even though it may make sense for a class to implement multiple listeners. 如果您有多个都标记为抽象的侦听器,则一个类只能从一个继承,即使一个类实现多个侦听器可能有意义。

  2. You may have an existing class that's already inheriting from another class, but you'd also like to be able to mark it as a listener. 您可能有一个已经从另一个类继承的现有类,但是您还希望能够将其标记为侦听器。

If you decide on using an interface but still wish to share some common implementation, a pattern to accomplish this is to create both a listener interface and an abstract implementation, eg 如果您决定使用接口,但仍然希望共享一些常见的实现,则可以通过创建侦听器接口和抽象实现来实现此目的,例如

interface ClickListener {
  void onLeftClickPressed();
  void onLeftClickReleased();
}

// "DefaultClickListener" and "AbstractClickListener" are also common conventions
abstract class DoubleClickListener implements ClickListener {
  int leftClickCount;
  @Override public final void onLeftClickPressed() {
    ++leftClickCount;
    if (leftClickCount == 2) {
      onDoubleClicked();
    }
  }
  @Override public final void onLeftClickReleased() { --leftClickCount; }

  protected abstract void onDoubleClicked();
}

If your listener interface has multiple methods, it can be nice to provide an adapter version that provides stubbed implementation for all of them, eg 如果您的侦听器接口有多种方法,最好提供一个适配器版本,为所有这些方法提供存根实现,例如

interface ComplexListener {
  void onEvent1();
  ...
  void onEvent100();
}

abstract class ComplexListenerAdapter implements ComplexListener {
  @Override public void onEvent1() {}
  ...
  @Override public void onEvent100() {}
}

final class MySimpleListener extends ComplexListenerAdapter {
  // The only event I care about
  @Override public void onEvent42() { ... }
}

I'm not sure it's a standard convention, but I've seen the ListenerAdapter name used for these sorts of listener implementations. 我不确定这是否是标准约定,但是我已经看到用于这些类型的侦听器实现的ListenerAdapter名称。

an abstract class is a class whose object can't be instantiated, for a key listener for any controls you need to include every definition of the game into a class or an abstract class whichever you use and that includes the Listener. 抽象类是无法实例化其对象的类,对于任何控件的关键侦听器,您都需要将游戏的每个定义都包含在一个类或一个抽象类中,无论您使用的是哪个类(包括侦听器)。

for any object which class to model how depends on whether there is any object who plays any role in the game, for eg some classes are meant to be parent classes, the HumanBeing class in any project you won't create an object of human being class, you'll surely extend it to Man, Worker, Woman etc so let HumanBeing be an abstract class 对于任何对象,要模拟哪个类的模型取决于是否有任何对象在游戏中扮演任何角色,例如,某些类是父类,那么在任何将不会创建人类对象的项目中,HumanBeing类类,您一定会将其扩展到男人,工人,女人等,因此让HumanBeing成为抽象类

so that is data modelling part. 这就是数据建模的一部分。 and i am sorry i was wrong when i stated in my previous answer that you can't add a Listener to a abstract class, you surely can but you have to take into consideration which classes to make and what are the roles of the classes only then you can decide if or not to include Listener into Abstract class ( part of Software Engineering) 很抱歉,当我在上一个回答中说您不能向抽象类添加侦听器时,我错了,您当然可以,但是您必须考虑要制作哪些类以及这些类的作用是什么那么您可以决定是否将Listener包含在Abstract类中 (软件工程的一部分)

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

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