[设计模式]工厂模式
简单工厂
// 计算类的基类
@Setter
@Getter
public abstract class Operation {
private double value1 = 0;
private double value2 = 0;
protected abstract double getResule();
}
//加法
public class OperationAdd extends Operation {
@Override
protected double getResule() {
return getValue1() + getValue2();
}
}
//减法
public class OperationSub extends Operation {
@Override
protected double getResule() {
return getValue1() - getValue2();
}
}
//乘法
public class OperationMul extends Operation {
@Override
protected double getResule() {
return getValue1() * getValue2();
}
}
//除法
public class OperationDiv extends Operation {
@Override
protected double getResule() {
if (getValue2() != 0) {
return getValue1() / getValue2();
}
throw new IllegalArgumentException("除数不能为零");
}
}
//工厂类
public class OperationFactory {
public static Operation createOperation(String operation) {
Operation oper = null;
switch (operation) {
case "add":
oper = new OperationAdd();
break;
case "sub":
oper = new OperationSub();
break;
case "mul":
oper = new OperationMul();
break;
case "div":
oper = new OperationDiv();
break;
default:
throw new UnsupportedOperationException("不支持该操作");
}
return oper;
}
}
public static void main(String[] args) {
Operation operationAdd = OperationFactory.createOperation("add");
operationAdd.setValue1(1);
operationAdd.setValue2(2)
System.out.println(operationAdd.getResule());
}
工厂方法
//工厂接口
public interface IFactory {
Operation CreateOption();
}
//加法类工厂
public class AddFactory implements IFactory {
public Operation CreateOption() {
return new OperationAdd();
}
}
//减法类工厂
public class SubFactory implements IFactory {
public Operation CreateOption() {
return new OperationSub();
}
}
//乘法类工厂
public class MulFactory implements IFactory {
public Operation CreateOption() {
return new OperationMul();
}
}
//除法类工厂
public class DivFactory implements IFactory {
public Operation CreateOption() {
return new OperationDiv();
}
}
public class Client {
public static void main(String[] args) {
//减法
IFactory subFactory = new SubFactory();
Operation operationSub = subFactory.CreateOption();
operationSub.setValue1(22);
operationSub.setValue2(20);
System.out.println("sub:"+operationSub.getResult());
//除法
IFactory Divfactory = new DivFactory();
Operation operationDiv = Divfactory.CreateOption();
operationDiv.setValue1(99);
operationDiv.setValue2(33);
System.out.println("div:"+operationSub.getResult());
}
}
抽象工厂
public class Engine {
public void getStyle(){
System.out.println("这是汽车的发动机");
}
}
public class UnderPan {
public void getStyle(){
System.out.println("这是汽车的底盘");
}
}
public class Wheel {
public void getStyle(){
System.out.println("这是汽车的轮胎");
}
}
public interface ICar {
void show();
}
@Getter
@Setter
@AllArgsConstructor
public class AudiCar implements ICar {
private Engine engine;
private Underpan underpan;
private Wheel wheel;
public void show() {
engine.getStyle();
underpan.getStyle();
wheel.getStyle();
System.out.println("造了一台奥迪汽车");
}
}
@Getter
@Setter
@AllArgsConstructor
public class BenzCar implements ICar {
private Engine engine;
private Underpan underpan;
private Wheel wheel;
public void show() {
engine.getStyle();
underpan.getStyle();
wheel.getStyle();
System.out.println("造了一台奔驰汽车");
}
}
public interface IFactory {
ICar createBenzCar();
ICar createAudiCar();
}
public class Factory implements IFactory {
public ICar createBenzCar() {
Engine engine = new Engine();
Underpan underpan = new Underpan();
Wheel wheel = new Wheel();
ICar car = new BenzCar(engine, underpan, wheel);
return car;
}
public ICar createAudiCar() {
Engine engine = new Engine();
Underpan underpan = new Underpan();
Wheel wheel = new Wheel();
ICar car = new AudiCar(engine, underpan, wheel);
return car;
}
}
public class AbstractFactoryMain {
public static void main(String[] args) {
IFactory factory = new Factory();
ICar benzCar = factory.createBenzCar();
benzCar.show();
ICar audi = factory.createAudiCar();
audi.show();
}
}
工厂方法模式与抽象工厂模式的区别在于:
- 工厂方法只有一个抽象产品类和一个抽象工厂类,但可以派生出多个具体产品类和具体工厂类,每个具体工厂类只能创建一个具体产品类的实例。
- 抽象工厂模式拥有多个抽象产品类(产品族)和一个抽象工厂类,每个抽象产品类可以派生出多个具体产品类;抽象工厂类也可以派生出多个具体工厂类,同时每个具体工厂类可以创建多个具体产品类的实例
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了