我有一个名为“Operation”的抽象类 这个类有一个名为“Prepare”的抽象方法。
public abstract class Operation {
public abstract void prepare() throws Exception;
public abstract void run() throws Exception;
// other stuff here that's not abstract
public void printHelloWorld() {
System.out.println("Hello World!");
}
}
唯一的问题是某些是“操作”的东西( 一些扩展Operation的类需要参数准备(一些 需要整数,有些需要String,有些需要更复杂的数据类型......所以它并不总是一个int)
public class Teleportation extends Operation {
@Override
public void prepare(int capacityRequired ) throws Exception {
// do stuff
}
@Override
public void run() throws Exception {
}
}
我用什么OOP模式来实现这一目标 以及如何设置此代码?
编辑:
理想情况下,我想准备并运行这样的操作:
for (Operation operation : operations ) {
operation.prepare();
operation.run();
}
假设我使用此解决方案:
public class Teleportation extends Operation {
private int cReq;
public void setCapacityRequired(int cReq) {
this.cReq = cReq;
}
@Override
public void prepare() throws Exception {
// I can do the preparation stuff
// since I have access to cReq here
}
@Override
public void run() throws Exception {
}
}
然后 - 我想知道是否可以避免这种情况:
for (Operation operation : operations ) {
if (operation.getClass().isInstanceOf(Teleporation.class)) {
((Teleporation)operation).setCapacityRequired( 5 );
}
operation.prepare();
operation.run();
}
答案 0 :(得分:2)
这里要做的第一件事就是覆盖抽象类操作并使用容量重载。
public class Teleportation extends Operation {
public void prepare() throws Exception {
prepare(0);
}
public void prepare(int capacityRequired) throws Exception {
//do stuff
}
}
答案 1 :(得分:2)
您需要扩展抽象类以包含两个方法签名或更改签名以采用varargs int参数:
public abstract class Operation {
public abstract void prepare(int... args) throws Exception;
}
答案 2 :(得分:2)
我建议有一个额外的构造函数,您可以在其中添加实现所需的必要数据,并将其存储在类实现的字段中。
对于你的例子:
public class Teleportation extends Operation {
private final int capacityRequired;
public Teleportation(int capacityRequired) {
this.capacityRequired = capacityRequired;
}
public void prepare() throws Exception {
// do stuff using the capacityRequired field...
}
}
这种方法也适用于更复杂的参数。
答案 3 :(得分:1)
您可以为您的操作类使用泛型类:
public abstract class Operation<T>
{
private T operationModel;
public Operation(T operationModel)
{
super();
this.operationModel = operationModel;
}
public abstract void prepare() throws Exception;
public abstract void run() throws Exception;
public T getOperationModel()
{
return operationModel;
}
}
然后对于具体类,使用适当的参数类型扩展它(每个操作可以有一个特定的类):
public class TeleOperation extends Operation<TeleOperationModel>
{
public TeleOperation(TeleOperationModel operationModel)
{
super(operationModel);
}
@Override
public void prepare() throws Exception
{
TeleOperationModel teleOperationModel = getOperationModel();
//...
}
@Override
public void run() throws Exception
{
}
}
public class TeleOperationModel
{
private int capacity;
....
}
和
public class MicroOperation extends Operation<MicroOperationModel>
{
public MicroOperation(MicroOperationModel operationModel)
{
super(operationModel);
}
@Override
public void prepare() throws Exception
{
MicroOperationModel microOperationModel = getOperationModel();
//...
}
@Override
public void run() throws Exception
{
}
}
public class MicroOperationModel
{
private int x;
private int y;
private int z;
....
}