简单工厂

master
terry 3 years ago
parent 7167eb5b1e
commit 0dfe1411c2

@ -0,0 +1,8 @@
package factoryMethod;
public class Car implements Vehicle{
@Override
public void go() {
System.out.println("Car starting... go to park...");
}
}

@ -0,0 +1,8 @@
package factoryMethod;
public class CarFactory {
public Vehicle createCar(){
System.out.println("car created...");
return new Car();
}
}

@ -0,0 +1,8 @@
package factoryMethod;
public class Main {
public static void main(String[] args) {
Vehicle vehicle = new Car();
vehicle.go();
}
}

@ -0,0 +1,8 @@
package factoryMethod;
public class Plane implements Vehicle{
@Override
public void go() {
System.out.println("plant moving.. fly fly fly...");
}
}

@ -0,0 +1,16 @@
package factoryMethod;
/*
*
* */
public class SimpleVehicleFactory {
public Car createCar(){
//一些前置操作。。。
return new Car();
}
public Plane createPlane(){
//一些前置操作。。。
return new Plane();
}
}

@ -0,0 +1,5 @@
package factoryMethod;
public interface Vehicle {
void go();
}
Loading…
Cancel
Save