英文:
Is it possible to have a method that can return different class objects in Java depending on the condition?
问题
public class ArriveEvent {
private final List<Server> list;
private final Customer cust;
ArriveEvent(Customer ppl, List<Server> listing){
this.list = listing;
this.cust = ppl;
}
public interface EventOutcome {
String toString();
}
public class ServedBy implements EventOutcome {
private final Server server;
ServedBy(Server server) {
this.server = server;
}
@Override
public String toString() {
return String.format("%.3f %d served by %d", cust.giveArrival(), cust.giveID(), server.getServerID());
}
}
public class Wait implements EventOutcome {
private final Server server;
Wait(Server server) {
this.server = server;
}
@Override
public String toString() {
return String.format("%.3f %d waits to be served by %d", cust.giveArrival(), cust.giveID(), server.getServerID());
}
}
public class DoneServing implements EventOutcome {
private final Server server;
DoneServing(Server server) {
this.server = server;
}
@Override
public String toString() {
return String.format("%.3f %d done serving by %d", cust.giveArrival(), cust.giveID(), server.getServerID());
}
}
public class Leave implements EventOutcome {
Leave() {}
@Override
public String toString() {
return String.format("%.3f %d leaves", cust.giveArrival(), cust.giveID());
}
}
public EventOutcome execute() {
for (Server server : list) {
if (server.isAvailable() && !server.hasWaitingCustomer()) {
server.serveCustomer(cust);
return new ServedBy(server);
} else if (!server.isAvailable() && !server.hasWaitingCustomer()) {
server.addWaitingCustomer(cust);
return new Wait(server);
} else if (!server.isAvailable() && server.hasWaitingCustomer()) {
return new Leave();
}
}
return null;
}
public String toString() {
return String.format("%.3f %d arrives", cust.giveArrival(), cust.giveID());
}
}
Please note that the provided code assumes that you have appropriately defined the methods and attributes in the Server
and Customer
classes. The execute()
method in the ArriveEvent
class iterates through the list of servers and implements the logic to determine the appropriate outcome based on server availability and waiting customers. It returns an instance of the respective inner class based on the outcome.
英文:
How can I make a method to return different immutable class objects depending on some conditions?
If it is not possible, is there an alternative to tackle this question?
public class ArriveEvent {
private final List<Server> list;
private final Customer cust;
ArriveEvent(Customer ppl, List<Server> listing){
this.list = listing;
this.cust = ppl;
}
PutSomethingHere? execute(){
}
public String toString() {
return String.format("%.3f %d arrives", cust.giveArrival(), cust.giveID());
}
}
The main question is for execute()
to return different statements depending on the condition of an immutable class Server
, in the list, which have different attributes, such as availability or if there is another customer in line.
private final boolean avail;
private final boolean anyoneWaitingInLine;
This is the requirement:
jshell> new ArriveEvent(new Customer(1, 0.5), Arrays.asList(new Server(1, false, false, 0), new Server(2, true, false, 0))).execute() // Two servers: (1) busy; (2) free
$.. ==> 0.500 1 served by 2
jshell> new ArriveEvent(new Customer(1, 0.5), Arrays.asList(new Server(1, false, false, 0), new Server(2, true, false, 0))).execute().execute()
$.. ==> 1.500 1 done serving by 2
jshell> new ArriveEvent(new Customer(1, 0.5), Arrays.asList(new Server(1, false, true, 5.0), new Server(2, false, false, 10.0))).execute() // Both servers busy, (1) has waiting customers
$.. ==> 0.500 1 waits to be served by 2
jshell> new ArriveEvent(new Customer(1, 0.5), Arrays.asList(new Server(1, false, true, 5.0), new Server(2, false, false, 10.0))).execute().execute()
$.. ==> 10.000 1 served by 2
jshell> new ArriveEvent(new Customer(1, 0.5), Arrays.asList(new Server(1, false, true, 5.0), new Server(2, false, false, 10.0))).execute().execute().execute()
$.. ==> 11.000 1 done serving by 2
jshell> new ArriveEvent(new Customer(1, 0.5), Arrays.asList(new Server(1, false, true, 5.0), new Server(2, false, true, 10.0))).execute() // Both busy with waiting customer
$.. ==> 0.500 1 leaves
My original idea was to create multiple classes servedby
, wait
, doneserving
, leave
that extends
the ArrivalEvent class
as I would need different toStrings()
depending on the situation of the servers and customers, then when I run an execute()
, it would return different classes that extended ArrivalEvent class depending on the condition of the servers and customers and have another overriding execute()
which returns a class among them.
答案1
得分: 1
你可以在这里使用重构
public <T> T execute() {
Object obj = getObject(ArriveEvent, ArriveEvent.getClass()); // 根据条件传递元素
}
public static <T> T getObject(Object o, Class<T> type) {
if (type.isInstance(o)) {
return type.cast(o);
} else {
return null;
}
}
英文:
You may use refactoring here
public <T> T execute(){
Object obj = getObject(ArriveEvent, ArriveEvent.getClass()) // Pass element as per condition
}
public static <T> T getObject(Object o, Class<T> type){
if(type.isInstance(o)){
return type.cast(o)
}
else{
return null;
}
}
答案2
得分: 1
class ServedBy extends ArrivalEvenet {
//...
}
class Wait extends ArrivalEvenet {
//...
}
class DoneServing extends ArrivalEvenet {
//...
}
class Leave extends ArrivalEvenet {
//...
}
class ArrivalEvenet {
}
public class ArriveEvent {
private final List<Server> list;
private final Customer cust;
ArriveEvent(Customer ppl, List<Server> listing){
this.list = listing;
this.cust = ppl;
}
public String toString() {
return String.format("%.3f %d arrives", cust.giveArrival(), cust.giveID());
}
ArrivalEvenet execute(ArrivalEvenet object) {
if (/*your condition*/) {
return new ServedBy(); //alternatively create your object
} else if (/*your condition*/) {
return new Wait(); //alternatively create your object
} else if (/*your condition*/) {
return new DoneServing(); //alternatively create your object
} else if (/*your condition*/) {
return new Leave(); //alternatively create your object
} else {
throw new IllegalArgumentException("Wrong type has been provided");
}
}
}
/*your condition*/
will look something like list.get(...)==...
英文:
>Yes, my original plan is to create "servedby", "wait", "doneserving", "leave" that extends the ArrivalEvent class and execute would return one of these subclasses depending on the condition of server inside the list, how would I do so?
First of all, few notes about your existing code:
- Always respect Java Naming Conventions, name your classes with PascalCase and methods - with camelCase. Example from your comment would be
doneserving
, which should beDoneServing
; - According to the same convention, try your best to have Noun as your class name;
- your
.toString()
usesgiveArrival()
andgiveID()
, which are not defined in your code, so your snippet would not compile; - You don't provide the class definitions for
Customer
andServer
.
Now, after considering all this, if we assume that you have all the necessary classes in your original code (named accordingly), your working code would look like this:
class ServedBy extends ArrivalEvenet {
//...
}
class Wait extends ArrivalEvenet {
//...
}
class DoneServing extends ArrivalEvenet {
//...
}
class Leave extends ArrivalEvenet {
//...
}
class ArrivalEvenet {
}
public class ArriveEvent {
private final List<Server> list;
private final Customer cust;
ArriveEvent(Customer ppl, List<Server> listing){
this.list = listing;
this.cust = ppl;
}
public String toString() {
return String.format("%.3f %d arrives", cust.giveArrival(), cust.giveID());
}
ArrivalEvenet execute(ArrivalEvenet object) {
if (/*your condition*/) {
return new ServedBy(); //alternatively create your object
} else if (/*your condition*/) {
return new Wait(); //alternatively create your object
} else if (/*your condition*/) {
return new DoneServing(); //alternatively create your object
} else if (/*your condition*/) {
return new Leave(); //alternatively create your object
} else {
throw new IllegalArgumentException("Wrong type has been provided");
}
}
}
/*your condition*/
will look something like list.get(...)==...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论