简单FactoryPattern

package com.potevio.zjx.study;

/**
 * 单例模式
 * @author Xian-JZ
 *
 */
public class Car implements Moveable{

	public static Car car = new Car();
	
	public Car(){}
	
	public static Car getIntance(){
		return car;
	}
	
	public void run(){
		System.out.println("This car is runing...");
	}
}
 
package com.potevio.zjx.study;

public class CarFactory extends VehicleFactory {

	@Override
	Moveable create() {
		return Car.getIntance();
	}

}
 
package com.potevio.zjx.study;

/**
 * 移动接口
 * @author Xian-JZ
 *
 */
public interface Moveable {

	void run();
}
 
package com.potevio.zjx.study;

public class Plane implements Moveable {
	
	public static Plane plane = new Plane();
	
	public Plane(){}
	
	public static Plane getInstance(){
		return plane;
	}

	public void run() {
		System.out.println("Fly in the blue sky。。。");
	}

}
 
package com.potevio.zjx.study;

public class PlaneFactory extends VehicleFactory {

	@Override
	Moveable create() {
		return Plane.getInstance();
	}

}
 
package com.potevio.zjx.study;

public abstract class VehicleFactory {

	abstract Moveable create();
}
 
package com.potevio.zjx.study;

public class Test {

	public static void main(String[] args) {
//		VehicleFactory factory = new PlaneFactory();
		VehicleFactory factory = new CarFactory();
		Moveable m = factory.create();
		m.run();
	}

}
 

你可能感兴趣的:(demo)