下面是简略的策略模式
public abstract class Port {
public abstract void method();
}
public class A extends Port {
public void method() {
System.out.println("方法A");
}
}
public class B extends Port {
public void method() {
System.out.println("方法B");
}
}
public class Test {
public static void main(String[] args) {
Prot p1 = new A();
p1.method();
Prot p2 = new B();
p2.method();
}
}
下面是用枚举实现策略模式
public enum Prot {
A{
public void method(){
System.out.println("方法A");
}
},
B{
public void method(){
System.out.println("方法B");
};
public abstract void method();
}
public class Test {
public static void main(String[] args) {
Prot.A.method();
Prot.B.method();
}
}
这两种方法,哪种开销更大?
{$ item.file.size/1024/1024|number:2 $} MB