public class Audi implements Car { @Override public void run() { System.out.println("奥迪出发"); } }
//奔驰 package com.match.factory.simplefactory;
public class Benz implements Car {
@Override public void run() { System.out.println("奔驰出发"); }
}
//比亚迪 package com.match.factory.simplefactory;
public class Byd implements Car { @Override public void run() { System.out.println("比亚迪出发"); } }
三种工厂类:
工厂类角色:这是本模式的核心,含有一定的商业逻辑和判断逻辑。
//工厂1 package com.match.factory.simplefactory;
public class CarFactory1 { public static Car createCar(String Type) { if("Audi".equals(Type)) { return new Audi(); } else if("Byd".equals(Type)) { return new Byd(); } else if("Benz".equals(Type)) { return new Benz(); } else { return null; } } }
//工厂2 package com.match.factory.simplefactory;
public class CarFactory2 { public static Car createAudi() { return new Audi(); } public static Car createByd() { return new Byd(); } public static Car createBenz() { return new Benz(); } }
//工厂3 package com.match.factory.simplefactory;
public class CarFactory3 { public static Car createCar(Class<? extends Car> cls) { Car car = null; try { car = (Car) Class.forName(cls.getName()).newInstance(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } return car; } }
测试代码:
package com.match.factory.simplefactory;
public class Client { public static void main(String[] args) { test1(); } /** * 测试CarFactory1 */ public static void test1() { Car c1 = CarFactory1.createCar("Audi"); Car c2 = CarFactory1.createCar("Byd"); Car c3 = CarFactory1.createCar("Benz"); c1.run(); c2.run(); c3.run(); } /** * 测试CarFactory2 */ public static void test2() { Car c1 = CarFactory2.createAudi(); Car c2 = CarFactory2.createByd(); Car c3 = CarFactory2.createBenz(); c1.run(); c2.run(); c3.run(); } /** * 测试CarFactory3 */ public static void test3() { Car c1 = CarFactory3.createCar(Audi.class); Car c2 = CarFactory3.createCar(Byd.class); Car c3 = CarFactory3.createCar(Benz.class); c1.run(); c2.run(); c3.run(); } }
package com.match.factory.factorymethod; /** * 工厂方法接口 * @author Match * */ public interface CarFactory { Car createCar(); }
具体工厂类:
具体工厂角色:它含有和具体业务逻辑有关的代码。由应用程序调用以创建对应的具体产品的对象。
package com.match.factory.factorymethod;
public class AudiFactory implements CarFactory { @Override public Car createCar() { return new Audi(); } }
package com.match.factory.factorymethod;
public class BenzFactory implements CarFactory { @Override public Car createCar() { return new Benz(); } }
package com.match.factory.factorymethod;
public class BydFactory implements CarFactory { @Override public Car createCar() { return new Byd(); } }
测试代码:
package com.match.factory.factorymethod;
public class Client { public static void main(String[] args) { Car c1 = new AudiFactory().createCar(); Car c2 = new BydFactory().createCar(); Car c3 = new BenzFactory().createCar(); c1.run(); c2.run(); c3.run(); } }
抽象工厂模式
类图
代码
产品接口及具体产品类:
抽象产品角色:它是具体产品继承的父类或者是实现的接口。
具体产品角色:具体工厂角色所创建的对象就是此角色的实例。
//引擎 package com.match.factory.abstractfactory;
public interface Engine { void run(); void start(); } class LuxuryEngine implements Engine {
@Override public void run() { System.out.println("转的快!"); }
@Override public void start() { System.out.println("启动快!可以自动启停!"); } } class LowEngine implements Engine {
@Override public void run() { System.out.println("转的慢!"); }
@Override public void start() { System.out.println("启动慢!"); } }
//座椅 package com.match.factory.abstractfactory;
public interface Seat { void message(); } class LuxurySeat implements Seat { @Override public void message() { System.out.println("可以自动按摩!"); } } class LowSeat implements Seat { @Override public void message() { System.out.println("不能按摩!"); } }
//轮胎 package com.match.factory.abstractfactory;
public interface Tyre { void revolve(); } class LuxuryTyre implements Tyre {
@Override public void revolve() { System.out.println("旋转不磨损!"); } } class LowTyre implements Tyre {
@Override public void revolve() { System.out.println("旋转磨损快!"); } }
public interface CarFactory { Engine createEngine(); Seat createSeat(); Tyre createTyre(); }
具体工厂类:
具体工厂角色:它含有和具体业务逻辑有关的代码。由应用程序调用以创建对应的具体产品的对象。
//高级工厂 package com.match.factory.abstractfactory;
public class LuxuryCarFactory implements CarFactory { @Override public Engine createEngine() { return new LuxuryEngine(); } @Override public Seat createSeat() { return new LuxurySeat(); } @Override public Tyre createTyre() { return new LuxuryTyre(); } }
//低级工厂 package com.match.factory.abstractfactory;
public class LowCarFactory implements CarFactory { @Override public Engine createEngine() { return new LowEngine(); } @Override public Seat createSeat() { return new LowSeat(); } @Override public Tyre createTyre() { return new LowTyre(); } }