返回稍后要创建的类

时间:2016-02-01 01:14:35

标签: java

目前,我正在将游戏从Python迁移到Java。在其中,我创建了一个Generator,它返回一个“类”,它将使用调用者可能具有的参数创建,如下所示:

package me.mathmaniac.smallworlds.world.generation;



import me.mathmaniac.smallworlds.block.Block;
import me.mathmaniac.smallworlds.block.NullBlock;
import me.mathmaniac.smallworlds.world.LayerType;

public class FlatGenerator implements Generator {

  @Override
  public Block generateBlock(LayerType ltype, int x, int y) {
    switch (ltype) {
      case Liquid:
        return HoleBlock;
      case Solid:
        return GrassBlock;
      case Air:
        return AirBlock;
      default:
        throw new RuntimeException();
    }
  }

}

从这里打来:

package me.mathmaniac.smallworlds.world;

import me.mathmaniac.smallworlds.block.Block;
import me.mathmaniac.smallworlds.world.generation.Generator;

public class World {

  Generator generator = ...;

  // ...

  private void generateBlocks(int x, int y) {
    for (LayerType ltype : LayerType.values())

      setblock(generator.generateBlock(ltype, x, y).new(x, y, ltype), //.new() is as an example
         x, y, ltype);
  }

}

我如何用Java实现这一目标?

3 个答案:

答案 0 :(得分:1)

您想使用Factory Pattern

您将拥有一些类:抽象类Block(或接口)和工厂类BlockFactory(或者您所称的FlatGenerator)。

如果有许多函数可以在所有Block类型中具有相同的实现,那么使基类Block成为抽象类并将这些方法放在那里。否则,您可以使用界面。

答案 1 :(得分:0)

一种方法可能是为所有图层类提供相同的接口和无参数构造函数,并从返回要在以后实例化的类的方法返回private void CalculateFutureValue(decimal futureValue, decimal monthlyInvestment, decimal monthlyInterestRate, int months) { for (int i = 0; i < months; i++) { futureValue = (futureValue + monthlyInvestment) * (1 + monthlyInterestRate); } } 对象:

Class<T>

使用此设置,您可以执行此操作:

interface Layer {
    void init(LayerType ltype, int x, int y);
}
class Liquid implements Layer {
    public Liquid() {
        ...
    }
    public void init(LayerType ltype, int x, int y) {
        ...
    }
}
class Solid implements Layer {
    public Solid() {
        ...
    }
    public void init(LayerType ltype, int x, int y) {
        ...
    }
}

调用者将按如下方式初始化图层:

public Class generateBlock(LayerType ltype, int x, int y) {
    if (...) return Liquid.class;
    if (...) return Solid.class;
    ...
}

答案 2 :(得分:0)

我会用传统的OOP风格来做。

首先为工厂类定义接口

public interface BlockFactory {
    Block createInstance(int x, int y, LayerType ltype);
}

然后定义相应的工厂

public class HoleBlockFactory implements BlockFactory {

    public Block createInstance(int x, int y, LayerType ltype) {
        return new HoleBlock(x, y);
    }
}


public class GrassBlockFactory implements BlockFactory {
    public Block createInstance(int x, int y, LayerType ltype) {
        return new GrassBlock(x, y);
    }
}

最后在您的生成器中,首先在switch语句中获得一个工厂,并在需要时使用工厂创建对象:

public class FlatGenerator implements Generator {

      @Override
      public BlockFactory generateBlock(LayerType ltype, int x, int y) {
        switch (ltype) {
          case Liquid:
            return new HoleBlockFactory();
          case Solid:
            return new GrassBlockFactory();

          default:
            throw new RuntimeException();
        }
      }

    }
}

public class World {

      Generator generator = ...;

      // ...

      private void generateBlocks(int x, int y) {
        for (LayerType ltype : LayerType.values())

          setblock(generator.generateBlock(ltype, x, y).createInstance(x, y, ltype), //.new() is as an example
             x, y, ltype);
      }

}