有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

oop Java将常用方法放入超类

我有两个类,它们都以相同类型的对象作为参数,但随后对该对象调用不同的方法以获得另一个对象(两个类中获得的对象的类型也不同),该对象在不同方法的类中广泛使用。现在,这两个类中的一些方法是相同的,所以我认为将它们放在一个子类中是明智的。但是,由于这些方法依赖于通过调用作为构造函数参数的对象上的不同方法而获得的对象,因此我不能将构造函数从子类复制到超类。我不确定超类如何获得所需的对象。似乎我潜在的超类'Server'将依赖于它的子类,这听起来甚至是错误的

下面是问题的说明性代码:

class ServerOne() {

    Connector connector; 

    public ServerOne(Conf conf) {
        Conf.ServerOneConf config = conf.getServerOneConf();
        connector = config.getConnector(); // 
    }

    // a lot of methods that use connector
}

class ServerTwo() {

    Connector connector;

    public ServerTwo(Conf conf) {
        Conf.ServerTwoConf config = conf.getServerTwoConf(); // notice that it uses a different method for obtaining the configuration. Also, the obtained object is of a different type than the configuration object that was obtained in the ServerOne constructor. 

        connector = config.getConnector();
    }

    // a lot of methods that use connector
}

class Server() {
    // would like to implement some common methods that use connector.
    // need to get an instance of the Connector to this class. 
}  

非常感谢您的帮助:)


共 (4) 个答案

  1. # 1 楼答案

    这是扩展超类的完美例子。在两个构造函数中填充的对象类型相同-数据不同。创建ServerOne时,它将以当前的方式填充超类中的对象。然后,超类中的常用方法现在可以对填充的对象进行操作

  2. # 2 楼答案

    让服务器成为一个抽象类并从中扩展ServerOne和ServerTwo怎么样

    像这样:

    public abstract class Server() {
       Connector connector;
       public Server() {
          Configuration config = conf.getServerTwoConf();
          connector = config.getConnector();
       }
    ...
    }  
    
    class ServerOne() extends Server{
    ...
    }
    
    class ServerTwo() extends Server{
    ...
    }
    
  3. # 3 楼答案

    将共享方法放在超级类中

    class Server() {
    
          Connector connector; 
    
          public Server(Conf conf) {
              Configuration config = conf.getServerConf();
              connector = config.getConnector(); // 
          }
    
          // your methods
    }
    

    然后在子类的构造函数中使用super()调用。它们可以轻松地继承超类的方法,而无需再次编写它们

    class ServerOne() extends Server {
    
        public ServerOne(Conf conf) {
            super(conf);
        }
    }
    
  4. # 4 楼答案

    可能有理由对服务器类进行子类化,但如何获取连接器可能不是子类化的理由。制定策略以处理获取连接器的问题:

    interface ConnectorStrategy {
        Connector retrieveConnector(Conf conf);
    }
    

    实现如下

    class ServerOneConnectorStrategy implements ConnectorStrategy {
        public Connector retrieveConnector(Conf conf) {
            return conf.getServerOneConf().getConnector();
        }
    }
    

    并在创建服务器对象时将其传递给服务器对象

    或者,如果需要层次结构,请使用template method pattern

    abstract class Server {
        abstract Connector retrieveConnector(Conf conf);
        void initializeConnector(Conf conf) {
            ...
            connector = retrieveConnector(conf);
        }
        ...
    }
    
    class ServerOne extends Server {
        public Connector retrieveConnector(Conf conf) {
             return conf.getServerOneConf().getConnector();
        }
    }