1.简单工厂设计模式:用于没有产品族并且产品个数较少 - Spring - BeanFactory
专门定义一个类(第三方),用来创建其他类实例(解耦,将客户端创建对象的操作解耦到外部第三方类),被创建的实例通常都具有共同的父类。
违背了开闭原则,如果要增加新的产品类,就必须修改工厂类。
组成:
a. 一个抽象产品类
b. N个具体产品类
c. 一个工厂类
相当于是有一个生产电脑的工厂类,有两个具体产品类,一个是苹果电脑,一个是联想电脑,客户需要购买什么样的电脑,只要输入类型编号就可以获取该电脑,将类的实例化交给工厂,已于解耦。
import java.util.Scanner;
interface Computer {
void printComputer();
}
//具体产品类
class MacbookProComputer implements Computer {
public void printComputer() {
System.out.println("This is a MacbookPro!");
}
}
//具体产品类
class SurfaceBookComputer implements Computer {
public void printComputer() {
System.out.println("This is a SurfaceBook!");
}
}
//工厂类
class ComputerFactory {
public static Computer getInstance(String type) {
Computer computer = null;
if (type.equals("macbook")) {
computer = new MacbookProComputer();
}else if (type.equals("surface")) {
computer = new SurfaceBookComputer();
}
return computer;
}
}
public class Client {
public void buyComputer(Computer computer) {
computer.printComputer();
}
public static void main(String[] args) {
Client client = new Client();
Scanner scanner = new Scanner(System.in);
System.out.println("请输入您想要的电脑型号...");
String type = scanner.nextLine();
Computer computer = ComputerFactory.getInstance(type);
client.buyComputer(computer);
}
}
2.工厂方法模式 --(简单工厂模式的横向扩展) - 产品族 - Spring - FactoryBean
定义一个用来创建对象的接口,让子类决定实例化哪一个工厂
组成:
a. 一个抽象产品类
b. 多个具体产品类
c. 一个抽象工厂
d.多个具体工厂(每个产品族对应一个具体工厂)
相当于一个每一个具体的产品类都有一个具体的工厂,在客户端中判断使用哪个工厂类去创建对象。
符合开闭原则,但是在客户端就得知道具体的工厂类,将判断逻辑由简单工厂的工厂类挪到了客户端。
interface Computer {
void printComputer();
}
//具体产品类
class MacbookProComputer implements Computer {
public void printComputer() {
System.out.println("This is a MacbookPro");
}
}
//具体产品类
class SurfaceBookComputer implements Computer {
public void printComputer() {
System.out.println("This is a SurfaceBook");
}
}
interface ComputerFactory {
Computer createComputer();
}
//具体产品工厂
class MsFactory implements ComputerFactory {
public Computer createComputer() {
return new SurfaceBookComputer();
}
}
//具体产品工厂
class AppleFactory implements ComputerFactory {
public Computer createComputer() {
return new MacbookProComputer();
}
}
public class Client {
public void buyComputer(Computer computer) {
computer.printComputer();
}
public static void main(String[] args) {
Client client = new Client();
//买哪个电脑就用哪个工厂类创建对象
ComputerFactory factory = new AppleFactory();
client.buyComputer(factory.createComputer());
}
}
3.抽象工厂模式 - 多个产品线混合
提供一个创建一系列相关或相互依赖对象的接口(抽象工厂、多条产品线),而无需指定他们的类。
相当于工厂不止生产电脑,还生产系统,每个工厂有多个商品。
interface Computer {
void printComputer();
}
//生产电脑1
class MacbookProComputer implements Computer {
public void printComputer() {
System.out.println("This is a MacbookPro");
}
}
//生产电脑2
class SurfaceBookComputer implements Computer {
public void printComputer() {
System.out.println("This is a SurfaceBook");
}
}
interface OperatingSystem {
void printSystem();
}
//生产系统1
class MacOsSystem implements OperatingSystem {
public void printSystem() {
System.out.println("This is a mac os");
}
}
//生产系统2
class Windows8System implements OperatingSystem {
public void printSystem() {
System.out.println("This is a window 8");
}
}
interface ProductionFactory {
Computer createComputer();
OperatingSystem createSystem();
}
//具体工厂1
class AppleFactory implements ProductionFactory {
public Computer createComputer() {
return new MacbookProComputer();
}
public OperatingSystem createSystem() {
return new MacOsSystem();
}
}
//具体工厂2
class MsFactory implements ProductionFactory {
public Computer createComputer() {
return new SurfaceBookComputer();
}
public OperatingSystem createSystem() {
return new Windows8System();
}
}
//客户端
public class Client {
public void buyComputer(Computer computer) {
computer.printComputer();
}
public void use(OperatingSystem s) {
s.printSystem();
}
public static void main(String[] args) {
Client client = new Client();
ProductionFactory factory = new AppleFactory();
Computer computer = factory.createComputer();
OperatingSystem system = factory.createSystem();
client.buyComputer(computer);
client.use(system);
}
}