2、如果程序中有多个else,如下:
if(con1){
dothing1();
}else if(con2){
dothing2();
}else if(con3){
dothing3();
}......
可是使用策略模式或多态机制来拒绝else(还有很多其他方式),下面先介绍“策略模式”的方式:
首先讲一个使用if...else...的例子:
package ifelse.use;
public class UseIfElse {
public static void main(String args[]){
MyPaper myPaper = new MyPaper(PaperColor.RED);
if(myPaper.getMyPaperColor() == PaperColor.BLACK){
System.out.println("You need a black pen!");
}else if(myPaper.getMyPaperColor() == PaperColor.BLUE){
System.out.println("You need a blue pen!");
}else if(myPaper.getMyPaperColor() == PaperColor.RED){
System.out.println("You need a red pen!");
}else if(myPaper.getMyPaperColor() == PaperColor.WHITE){
System.out.println("You need a white pen!");
}
}
}
class MyPaper{
private PaperColor paperColor;
public MyPaper(PaperColor paperColor){
this.paperColor = paperColor;
}
public PaperColor getMyPaperColor(){
return this.paperColor;
}
}
enum PaperColor{
WHITE, BLACK, BLUE, RED
}
使用if...else...的弊端在于:不利于对程序的扩展,如果新添加了一个颜色类型,那么就得去修改程序再添加一个if...else...分支,根据“开-闭原则”的宗旨:对扩展开,对修改闭。显然是用if...else...已经go out了。
下面讲一个使用“策略模式”解决上述问题的办法:
package ifelse.no;
public class NoIfElse {
public static void main(String args[]){
MyPaper myPaper = new MyPaper(new White());
myPaper.choicePen();
}
}
interface PaperColor{
public void getPenColor();
}
class White implements PaperColor{
public void getPenColor(){
System.out.println("You need a white pen!");
}
}
class Red implements PaperColor{
public void getPenColor(){
System.out.println("You need a red pen!");
}
}
class Blue implements PaperColor{
public void getPenColor(){
System.out.println("You need a blue pen!");
}
}
class Black implements PaperColor{
public void getPenColor(){
System.out.println("You need a black pen!");
}
}
class MyPaper{
private PaperColor paperColor;
public MyPaper(PaperColor paperColor){
this.paperColor = paperColor;
}
public PaperColor getPaperColor(){
return this.paperColor;
}
public void choicePen(){
this.paperColor.getPenColor();
}
}
接下来介绍使用“多态”机制来实现拒绝else:
package ifelse.no.Polymorphism;
public class NoIfElse_Polymorphism {
public static void main(String args[]){
MyPaper myPaper = new MyPaper();
myPaper.choice(new Black());
}
}
class White {
public void getPenColor(){
System.out.println("You need a white pen!");
}
}
class Red {
public void getPenColor(){
System.out.println("You need a red pen!");
}
}
class Blue {
public void getPenColor(){
System.out.println("You need a blue pen!");
}
}
class Black {
public void getPenColor(){
System.out.println("You need a black pen!");
}
}
class MyPaper{
public void choice(White white){
white.getPenColor();
}
public void choice(Red red){
red.getPenColor();
}
public void choice(Blue blue){
blue.getPenColor();
}
public void choice(Black black){
black.getPenColor();
}
}
总结:
此处使用的多态机制指:方法的重构,根据方法名相同儿参数不同的机制,来实现拒绝关键字。