根据某些条件操作订单的最佳方式是什么(除了使用不同的顺序再次编写它们之外)?
假设有一个Person类,Person的每个对象代表一个不同的人。
class Person{
int eatingPriority = 3;
int sleepingPriority = 2;
int recreationPriority = 1;
void eat() {/*eats*/}
void sleep() {/*sleeps*/}
void watchTv() {/*watches tv*/}
void satisfyNeeds() {
//HOW TO DO THIS
}
}
如何让satisfyNeeds()
方法根据优先级调用其他三种方法?
注意:我想说明优先级可以在人与人之间发生变化。
答案 0 :(得分:13)
您可以使用1个类和1个接口执行此操作。
public class Person {
int eatingPriority = 3;
int sleepingPriority = 2;
int recreationPriority = 1;
PriorityQueue<Action> actions;
void eat() { }
void sleep() { }
void watchTv() { }
public Person() {
actions = new PriorityQueue<Action>(new Comparator<Action>() {
@Override
public int compare(Action o1, Action o2) {
return o2.getPriority() - o1.getPriority();
}
});
actions.add(new Action() {
@Override
public int getPriority() {
return eatingPriority;
}
@Override
public void execute() {
eat();
}
});
actions.add(new Action() {
@Override
public int getPriority() {
return sleepingPriority;
}
@Override
public void execute() {
sleep();
}
});
actions.add(new Action() {
@Override
public int getPriority() {
return recreationPriority;
}
@Override
public void execute() {
watchTv();
}
});
}
public void satisfyNeeds() {
for (Action action : actions) {
action.execute();
}
}
interface Action {
public int getPriority();
public void execute();
}
}
答案 1 :(得分:10)
这是另一种可能的实施方式:
abstract class Need {
abstract void satisfy();
}
class Eat extends Need {
@Override
public void satisfy() { /* eat ...*/}
}
class Sleep extends Need {
@Override
public void satisfy() { /* sleep ...*/}
}
class DrinkBeer extends Need {
@Override
public void satisfy() { /* drink beer ...*/}
}
class Person{
// TreeMap will sort the map in the key's natural order (a int here)
private Map<Integer, Need> needs = new TreeMap<>();
Person() {
add(new Eat(), 3);
add(new Sleep(), 2);
add(new DrinkBeer(), 1);
}
void add(Need need, int priority) {
needs.put(Integer.valueOf(priority), need);
}
void satisfyNeeds() {
for(Need need : needs.values())
need.satisfy();
}
}
答案 2 :(得分:9)
此解决方案需要Java 8:
class Person {
void eat() {};
void sleep() {};
void watchTv() {};
// Being in a List you can easily reorder the needs when you want to
List<Runnable> needs = Arrays.asList(this::eat, this::sleep);
// Alternatively, you can use a Map<Runnable, Integer> where the value is your
// priority and sort it (see http://stackoverflow.com/q/109383/1296402)
void satisfyNeeds() {
needs.forEach(r -> r.run());
}
}
答案 3 :(得分:7)
您可以使用此代码
import java.util.Arrays; // must be imported
int[] priorities = {sleepPriority, eatPriority, recreationPriority};
Arrays.sort(priorities);
for (int i=priorities.length-1; 0<=i; i--) {
int priority = priorities[i];
if (priority == sleepingPriority) { sleep(); }
if (priority == eatingPriority) { eat(); }
if (priority == recreationPriority) { watchTv(); }
}
基本上,它将优先级放在一个数组中,对数组进行排序并在其上运行for循环来运行函数。
答案 4 :(得分:5)
找到三个元素的正确顺序可以简单地完成:
void satisfyNeeds() {
boolean eatFirst = eatingPriority>Math.max(sleepingPriority,recreationPriority);
if(eatFirst) eat();
if(sleepingPriority>recreationPriority) {
sleep();
watchTv();
}
else {
watchTv();
sleep();
}
if(!eatFirst) eat();
}
当然,如果你提高行动次数,它将无法扩展。对于更高的数字,您可以查看其他答案之一。
答案 5 :(得分:4)
您应该将地图属性引入Person
类,其中优先考虑方法,例如:
class Person {
...
private Map<Integer, Method> methodsPriority = new HashMap<>();
...
public Person setEatingPriority(int priority) {
methodsPriority.put(priority, /* put 'eat' method reference here*/);
return this;
}
public Person setSleepingPriority(int priority) {
methodsPriority.put(priority, /* put 'sleep' method reference here*/);
return this;
}
public Person setWatchingTVPriority(int priority) {
methodsPriority.put(priority, /* put 'watch TV' method reference here*/);
return this;
}
public void satisfyNeeds() {
Collection<Integer> keys = methodsPriority.keySet();
Collections.sort(keys);
for(Integer key: keys)
methodsPriority.get(key).invoke(this);
}
...
}
它可以用于下一种方式:
Person Anna = new Person()
.setEatingPriority(1)
.setSleepingPriority(2)
.setWatchingTVPriority(3);
Person Bob = new Person()
.setEatingPriority(3)
.setSleepingPriority(2)
.setWatchingTVPriority(1);
Anna.satisfyNeeds();
Bob.satisfyNeeds();