实现了创建者和调用者的分离。
package com.qianyu.gof.factory.simpleFactory;
/**
* @author lijing
* @date 2019-04-08-19:22
* @discription 车接口
*/
public interface Car {
public void run();
}
package com.qianyu.gof.factory.simpleFactory;
/**
* @author lijing
* @date 2019-04-08-19:42
* @discription 比亚迪车实现了车接口
*/
public class BydCar implements Car {
@Override
public void run() {
System.out.println("比亚迪在跑");
}
}
package com.qianyu.gof.factory.simpleFactory;
/**
* @author lijing
* @date 2019-04-08-19:43
* @discription 奥迪车实现了车接口
*/
public class AudiCar implements Car {
@Override
public void run() {
System.out.println("奥迪在跑");
}
}
package com.qianyu.gof.factory.simpleFactory;
/**
* @author lijing
* @date 2019-04-08-19:44
* @discription 没有工厂模式的测试类
*/
public class NoFactoryTest {
public static void main(String[] args) {
BydCar bydCar = new BydCar();
AudiCar audiCar = new AudiCar();
bydCar.run();
audiCar.run();
}
}
package com.qianyu.gof.factory.simpleFactory;
/**
* @author lijing
* @date 2019-04-08-19:48
* @discription 简单工厂模式
*/
public class SimpleCarFactory {
public static Car createCar(String name) {
if ("奥迪".equals(name)) {
return new AudiCar();
} else if ("比亚迪".equals(name)) {
return new BydCar();
}
return null;
}
}
此时的测试类如下:
package com.qianyu.gof.factory.simpleFactory;
/**
* @author lijing
* @date 2019-04-08-19:51
* @discription 简单工厂模式测试类
*/
public class SimpleCarFactoryTest {
public static void main(String[] args) {
Car audi = SimpleCarFactory.createCar("奥迪");
Car byd = SimpleCarFactory.createCar("比亚迪");
audi.run();
byd.run();
}
}
UML图如下,测试类只用和SimpleCarFactory类和Car接口打交道即可:
此外,简单工厂还有另一种实现:
package com.qianyu.gof.factory.simpleFactory;
/**
* @author lijing
* @date 2019-04-08-19:48
* @discription 简单工厂模式的另一种实现
*/
public class SimpleCarFactory2 {
public static Car createAudi(){
return new AudiCar();
}
public static Car createByd(){
return new BydCar();
}
}
package com.qianyu.gof.factory.factoryMethod;
import com.qianyu.gof.factory.simpleFactory.Car;
/**
* @author lijing
* @date 2019-04-08-20:49
* @discription 工厂接口
*/
public interface CarFactory {
public Car create();
}
工厂类的实现类:
package com.qianyu.gof.factory.factoryMethod;
import com.qianyu.gof.factory.simpleFactory.AudiCar;
import com.qianyu.gof.factory.simpleFactory.Car;
/**
* @author lijing
* @date 2019-04-08-20:50
* @discription 奥迪工厂类
*/
public class AudiFactory implements CarFactory {
@Override
public Car create() {
return new AudiCar();
}
}
package com.qianyu.gof.factory.factoryMethod;
import com.qianyu.gof.factory.simpleFactory.BydCar;
import com.qianyu.gof.factory.simpleFactory.Car;
/**
* @author lijing
* @date 2019-04-08-20:51
* @discription 比亚迪工厂类
*/
public class BydFactory implements CarFactory {
@Override
public Car create() {
return new BydCar();
}
}
测试类:
package com.qianyu.gof.factory.factoryMethod;
import com.qianyu.gof.factory.simpleFactory.Car;
/**
* @author lijing
* @date 2019-04-08-20:39
* @discription 工厂方法设计模式测试类
*/
public class FactoryMethodTest {
public static void main(String[] args) {
Car audi = new AudiFactory().create();
Car byd = new BydFactory().create();
audi.run();
byd.run();
}
}
package com.qianyu.gof.factory.abstractFactory;
/**
* @author lijing
* @date 2019-04-08-21:56
* @discription 轮胎接口以及实现类
*/
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("质量差的轮胎,不耐用");
}
}
package com.qianyu.gof.factory.abstractFactory;
/**
* @author lijing
* @date 2019-04-08-22:01
* @discription 发动机接口和其实现类
*/
public interface Engine {
void run();
}
/**
* 高端发动机类
*/
class LuxuryEngine implements Engine {
@Override
public void run() {
System.out.println("高端发动机,转的快");
}
}
/**
* 低端发动机类
*/
class LowEngine implements Engine {
@Override
public void run() {
System.out.println("低端发动机,转的慢");
}
}
package com.qianyu.gof.factory.abstractFactory;
/**
* @author lijing
* @date 2019-04-08-22:08
* @discription 座椅接口及其实现类
*/
public interface Seat {
void massage();
}
/**
* 高端座椅类
*/
class LuxurySeat implements Seat {
@Override
public void massage() {
System.out.println("高端座椅,可以按摩");
}
}
/**
* 地端座椅类
*/
class LowSeat implements Seat {
@Override
public void massage() {
System.out.println("低端座椅,不可以按摩");
}
}
package com.qianyu.gof.factory.abstractFactory;
/**
* @author lijing
* @date 2019-04-08-22:12
* @discription 汽车工厂类接口以及其实现类
*/
public interface CarFactory {
Tyre createTyre();
Engine createEngine();
Seat createSeat();
}
/**
* 高端车工厂类
*/
class LuxuryCarFactory implements CarFactory {
@Override
public Tyre createTyre() {
return new LuxuryTyre();
}
@Override
public Engine createEngine() {
return new LuxuryEngine();
}
@Override
public Seat createSeat() {
return new LuxurySeat();
}
}
/**
* 低端车工厂类
*/
class LowCarFactory implements CarFactory {
@Override
public Tyre createTyre() {
return new LowTyre();
}
@Override
public Engine createEngine() {
return new LowEngine();
}
@Override
public Seat createSeat() {
return new LowSeat();
}
}
测试类:
package com.qianyu.gof.factory.abstractFactory;
/**
* @author lijing
* @date 2019-04-08-21:51
* @discription 抽象工厂设计模式测试类
*/
public class AbstractFactoryTest {
public static void main(String[] args) {
LuxuryCarFactory luxuryCarFactory = new LuxuryCarFactory();
Tyre luxuryTyre = luxuryCarFactory.createTyre();
Engine luxuryEngine = luxuryCarFactory.createEngine();
LowCarFactory lowCarFactory = new LowCarFactory();
Engine lowEngine = lowCarFactory.createEngine();
Tyre lowTyre = lowCarFactory.createTyre();
luxuryTyre.revolve();
luxuryEngine.run();
lowEngine.run();
lowTyre.revolve();
}
}
——基于高淇老师的《java300集》