I have an abstract class called "Operation" and this class has an abstract method called "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!");
}
}
The only issue is that some things that are "Operation" ( some classes that extends Operation ) need arguments to prepare ( some need ints, some need String, some need more complex data types..so it's not always an int )
public class Teleportation extends Operation {
@Override
public void prepare(int capacityRequired ) throws Exception {
// do stuff
}
@Override
public void run() throws Exception {
}
}
What OOP pattern do I use to achieve this and how do I set up this code ?
EDIT :
Ideally, I want to prepare and run operations like this :
for (Operation operation : operations ) {
operation.prepare();
operation.run();
}
Assuming I use this solution :
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 {
}
}
Then - I wonder if it's possible to avoid this :
for (Operation operation : operations ) {
if (operation.getClass().isInstanceOf(Teleporation.class)) {
((Teleporation)operation).setCapacityRequired( 5 );
}
operation.prepare();
operation.run();
}