工厂方法
工厂方法代码示例
public class FactoryMethod {
public static void main(String[] args) {
TransportFactory factory = new TransportFactory();
Movable movable = factory.createTransport();
movable.move();
}
}
class TransportFactory {
Movable createTransport() {
// do something...
System.out.println("TransportFactory.createTransport");
return new Car();
}
}
interface Movable {
void move();
}
class Car implements Movable {
@Override
public void move() {
System.out.println("Car.move");
}
}
class Train implements Movable {
@Override
public void move() {
System.out.println("Train.move");
}
}
保持微笑,时刻冷静,相信自己也相信队友,坚持信念