枚举实现工厂模式
package com. lee.first1200 ;
public interface Car {
public void sayName() ;
}
package com. lee.first1200 ;
public class AudiCar implements Car {
@Override
public void sayName() {
System.out .println( "Audi");
}
}
package com. lee.first1200 ;
public class BenzCar implements Car {
@Override
public void sayName() {
System.out .println( "Benz");
}
}
package com. lee.first1200 ;
public enum CarFactory {
BENZ {
@Override
public Car create() {
// TODO Auto-generated method stub
return new BenzCar() ;
}
},
AUDI {
@Override
public Car create() {
// TODO Auto-generated method stub
return new AudiCar() ;
}
},
BMW {
@Override
public Car create() {
// TODO Auto-generated method stub
return new BmwCar() ;
}
};
public abstract Car create();
}
package com. lee.first1200 ;
public class CarMain {
public static void main(String [] args ) {
CarFactory .BMW. create(). sayName();
CarFactory .AUDI. create(). sayName();
CarFactory .BMW. create(). sayName();
}
}