package cn.vehicle
public interface Moveable
{
public void run();
}
public class Car implements Moveable
{
public void run()
{
System.out.println("开车喽");
}
}
public class Plane implements Moveable
{
public void run()
{
System.out.println("开飞机喽");
}
}
package cn.factory
import cn.vehicle.Moveable;
public abstract class VehicleFactory
{
public abstract Moveable createInstance();
}
import cn.vehicle.Car;
public class CarFactory extends VehicleFactory
{
public Car createInstance()
{
return new Car();
}
}
import cn.vehicle.Plane;
public class PlaneFactory extends VehicleFactory
{
public Plane createInstance()
{
return new Plane();
}
}
package cn.test
import cn.factory.VehicleFactory;
import cn.factory.PlaneFactory;
import cn.vehicle.Moveable;
public class Test
{
public static void main(String[] args)
{
VehicleFactory factory = new PlaneFactory();
Moveable m = factory.createInstance();
m.run();
}
}