工厂模式包括:工厂方法(特例:简单工厂),抽象工厂
1.工厂方法模式
抽象工厂类:
package com.factory.abstractfactory;
import com.factor.abstractproduct.Moveable;
public abstract class MoveableFactory {
public abstract Moveable create();
}
具体工厂类:
package com.factory.specificfactory;
import com.factor.abstractproduct.Moveable;
import com.factory.abstractfactory.MoveableFactory;
import com.factory.specificproduct.Broom;
public class BroomFactory extends MoveableFactory {
@Override
public Moveable create() {
return new Broom();
}
}
package com.factory.specificfactory;
import com.factor.abstractproduct.Moveable;
import com.factory.abstractfactory.MoveableFactory;
import com.factory.specificproduct.Car;
public class CarFactory extends MoveableFactory {
@Override
public Moveable create() {
return new Car();
}
}
抽象产品类:
package com.factor.abstractproduct;
public interface Moveable {
void go();
}
具体产品类:
package com.factory.specificproduct;
import com.factor.abstractproduct.Moveable;
public class Broom implements Moveable {
@Override
public void go() {
System.out.println("扫帚去东北broom");
}
}
package com.factory.specificproduct;
import java.util.ArrayList;
import java.util.List;
import com.factor.abstractproduct.Moveable;
public class Car implements Moveable {
// private Car() {}
private static Car car = new Car(); //单例
// private static List<Car> cars = new ArrayList<Car>(); //多例
public static Car getInstance() {
return car;
}
public void go() {
System.out.println("开车去东北car");
}
}
测试类:
package com.factory.main;
import com.factor.abstractproduct.Moveable;
import com.factory.abstractfactory.MoveableFactory;
import com.factory.specificfactory.BroomFactory;
public class Test {
public static void main(String[] args) {
// Car c = Car.getInstance();
// Car c1 = Car.getInstance();
// if(c == c1) System.out.println("相同");
// c.go();
// Moveable m = new Plane();
// Moveable m1 = Car.getInstance();
// m.go();
// m1.go();
MoveableFactory mf = new BroomFactory();
Moveable m = mf.create();
m.go();
}
}
2.抽象工厂:
抽象工厂类:
package com.factory.abstractFactory;
import com.factor.abstractProduct.Food;
import com.factor.abstractProduct.Vehicle;
public abstract class AbstractFactory {
public abstract Vehicle createVehicle();
public abstract Food createFood();
}
具体工厂
package com.factory.instanceFactory;
import com.factor.abstractProduct.Food;
import com.factor.abstractProduct.Vehicle;
import com.factory.abstractFactory.AbstractFactory;
import com.factory.instanceProduct.Apple;
import com.factory.instanceProduct.Car;
public class OneFactory extends AbstractFactory {
@Override
public Vehicle createVehicle() {
return new Car();
}
@Override
public Food createFood() {
return new Apple();
}
}
package com.factory.instanceFactory;
import com.factor.abstractProduct.Food;
import com.factor.abstractProduct.Vehicle;
import com.factory.abstractFactory.AbstractFactory;
import com.factory.instanceProduct.Banana;
import com.factory.instanceProduct.Plain;
public class TwoFactory extends AbstractFactory {
@Override
public Vehicle createVehicle() {
return new Plain();
}
@Override
public Food createFood() {
return new Banana();
}
}
抽象产品类
package com.factor.abstractProduct;
public abstract class Food {
public abstract void eat();
}
package com.factor.abstractProduct;
public abstract class Vehicle {
public abstract void go();
}
具体产品类:
package com.factory.instanceProduct;
import com.factor.abstractProduct.Food;
public class Apple extends Food {
public void eat() {
System.out.println("apple----------");
}
}
package com.factory.instanceProduct;
import com.factor.abstractProduct.Food;
public class Banana extends Food {
@Override
public void eat() {
System.out.println("Banana吃------");
}
}
package com.factory.instanceProduct;
import com.factor.abstractProduct.Vehicle;
public class Car extends Vehicle {
public void go() {
System.out.println("car------开车去东北");
}
}
package com.factory.instanceProduct;
import com.factor.abstractProduct.Vehicle;
public class Plain extends Vehicle {
@Override
public void go() {
System.out.println("plain飞机--------");
}
}
测试类:
package com.factory.main;
import com.factor.abstractProduct.Food;
import com.factor.abstractProduct.Vehicle;
import com.factory.abstractFactory.AbstractFactory;
import com.factory.instanceFactory.OneFactory;
import com.factory.instanceFactory.TwoFactory;
import com.factory.instanceProduct.Apple;
import com.factory.instanceProduct.Car;
public class Test {
public static void main(String[] args) {
AbstractFactory af = new TwoFactory();
Vehicle v = af.createVehicle();
v.go();
Food f = af.createFood();
f.eat();
}
}
总结:
抽象工厂的抽象产品可能一个或多个,在只有一个产品族的情况下,则退化为工厂方法模式。