gpt4 book ai didi

java - Java 中的条件分支

转载 作者:行者123 更新时间:2023-12-01 18:36:09 27 4
gpt4 key购买 nike

我想知道您认为哪一个设计更好。假设我们有

/*
* Created 21 Feb 2014
*/


import java.util.Properties;
import java.util.Random;

/**
*
* @author me
*/

enum EventType{
TRADE, RISK, SHIPPING, MARGIN, STOP_LOSS, TAKE_PROFIT
}

interface Event{
EventType getType();
Properties getProperties();
}


class ShippingEvent implements Event{
public final static String PROPERTY_KEY1 = "property_ke1";
public final static String PROPERTY_KEY2 = "property_ke2";
public final static String PROPERTY_KEY3 = "property_ke3";

@Override
public EventType getType() {
return EventType.SHIPPING;
}

@Override
public Properties getProperties() {
throw new UnsupportedOperationException("Not supported yet.");
}
}


class TradeEvent implements Event{
public final static String PROPERTY_KEY1 = "property_ke1";
public final static String PROPERTY_KEY2 = "property_ke2";
public final static String PROPERTY_KEY3 = "property_ke3";

@Override
public EventType getType() {
return EventType.TRADE;
}

@Override
public Properties getProperties() {
throw new UnsupportedOperationException("Not supported yet.");
}
}

class RiskEvent implements Event{
public final static String PROPERTY_KEY1 = "property_ke1";
public final static String PROPERTY_KEY2 = "property_ke2";
public final static String PROPERTY_KEY3 = "property_ke3";

@Override
public EventType getType() {
return EventType.RISK;
}

@Override
public Properties getProperties() {
throw new UnsupportedOperationException("Not supported yet.");
}
}

class MarginEvent implements Event{
public final static String PROPERTY_KEY1 = "property_ke1";
public final static String PROPERTY_KEY2 = "property_ke2";
public final static String PROPERTY_KEY3 = "property_ke3";

@Override
public EventType getType() {
return EventType.MARGIN;
}

@Override
public Properties getProperties() {
throw new UnsupportedOperationException("Not supported yet.");
}
}

class StopLossEvent implements Event{
public final static String PROPERTY_KEY1 = "property_ke1";
public final static String PROPERTY_KEY2 = "property_ke2";
public final static String PROPERTY_KEY3 = "property_ke3";

@Override
public EventType getType() {
return EventType.STOP_LOSS;
}

@Override
public Properties getProperties() {
throw new UnsupportedOperationException("Not supported yet.");
}
}

class TakeProfiEvent implements Event{
public final static String PROPERTY_KEY1 = "property_ke1";
public final static String PROPERTY_KEY2 = "property_ke2";
public final static String PROPERTY_KEY3 = "property_ke3";

@Override
public EventType getType() {
return EventType.TAKE_PROFIT;
}

@Override
public Properties getProperties() {
throw new UnsupportedOperationException("Not supported yet.");
}
}

public class InstanceOfTest {

private static void testIfBranch(final Event event){
if (event instanceof TakeProfiEvent){
System.out.println("TakeProfiEvent");
}else if (event instanceof StopLossEvent){
System.out.println("StopLossEvent");
}else if (event instanceof MarginEvent){
System.out.println("MarginEvent");
}else if (event instanceof RiskEvent){
System.out.println("RiskEvent");
}else if (event instanceof TradeEvent){
System.out.println("TradeEvent");
}else if (event instanceof ShippingEvent){
System.out.println("ShippingEvent");
}
}




private static void testSwitchBranch(final Event event){

switch(event.getType()){
case TRADE:
System.out.println("TradeEvent");
break;
case RISK:
System.out.println("RiskEvent");
break;
case SHIPPING:
System.out.println("ShippingEvent");
break;
case MARGIN:
System.out.println("MarginEvent");
break;
case STOP_LOSS:
System.out.println("StopLossEvent");
break;
case TAKE_PROFIT:
System.out.println("TakeProfiEvent");
break;
default:
break;
}
}

public static void main(String[] args){
Event[] events = new Event[] {
new TakeProfiEvent(),
new StopLossEvent(),
new MarginEvent(),
new RiskEvent(),
new TradeEvent(),
new ShippingEvent()
};


Random random = new Random(System.currentTimeMillis());
long start = System.currentTimeMillis();
for(int idx=0; idx<10000000; ++idx){
int jdx = random.nextInt(events.length-1);
//testIfBranch(events[jdx]);
testSwitchBranch(events[jdx]);
}
long end = System.currentTimeMillis();

System.out.println("Time taken: " + (end - start));
}

}

我进行的测试表明 switch 语句对于所有测试用例都表现得更好,正如 Compiling switch in the JVM 所预期的那样。 。另外,我倾向于不使用 instanceof 运算符,因为我发现在这种情况下它会使扩展软件变得困难。例如,我使用 getType() 方法,我可以轻松使用 IoC 或 DI。另外,我认为使用 instanceof 运算符会创建对类定义的明确依赖。我想听听您对此的想法,特别是在软件工程方面 - 陷阱和/或最佳实践。

结果示例:

Switch Branch: 304916ms
If Branch: 307924ms

最佳答案

毫无疑问,最好的设计是开关设计。有关最佳实践,请参阅 http://www.javapractices.com/topic/TopicAction.do?Id=31

关于java - Java 中的条件分支,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21936195/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com