设计模式总结

设计模式总结

1、创建型模式

创建型模式主要就是为了创建对象,可以减少冗余重复的创建对象代码,也可以达到解耦合的效果。

工厂模式

Concept:总体来说,工厂模式就是为了解耦,将对象延迟到子类实例化。除此之外,还可以将对象的创建和使用的过程分开。可批量生产对象,还能有效降低代码重复,在业务逻辑发生变化时降低维护成本。

下面这三种逐渐抽象化。

  • 简单工厂

​ 简单工厂就是将一个类的实例化延迟到子类中。比如有很多的类,我们可以开一个工厂,在工厂类中定义一个静态方法专门来生产这些类对象,这个静态方法就类似于车间,每个车间都有很多条流水线。我们在使用时只需传入对象类型,这又相当于我们选择哪条流水线,这样流水线只负责生成对象。

提供一个统一的工厂类,由用户调用其 static 方法来创建对象。可以根据用户输入参数来返回相应的对象。

public class ShapeFactory {
public static Shape getShape(String shapeType) {
if (shapeType == null) {
return null;
}
if (shapeType.equalsIgnoreCase("CIRCLE")) {
return new Circle();
} else if (shapeType.equalsIgnoreCase("RECTANGLE")) {
return new Rectangle();
} else if (shapeType.equalsIgnoreCase("SQUARE")) {
return new Square();
}
return null;
}
}
Shape circle = ShapeFactory.getShape("CIRCLE");

如果我们新增产品类的话,就需要修改工厂类中的 getShape 方法。这个弊端可以通过反射机制改善。

public class ShapeFactory {
public static Object getShape(Class<? extends Shape> c) {
Object obj = null;
try {
obj = Class.forName(c.getName()).newInstance();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return obj;
}
}
Circle circle = (Circle) ShapeFactory2.getClass(factory_pattern.Circle.class);
  • 工厂方法

​ 工厂方法模式是简单工厂的仅一步深化。在工厂方法模式中,我们不再提供一个统一的工厂类来创建所有的对象,而是针对不同的对象提供不同的工厂。也就是说 每个对象都有一个与之对应的工厂 。

// 工厂接口
public interface Factory {
public Shape getShape();
}
// 专用工厂类,还可以有其他工厂类
public class CircleFactory implements Factory {
@Override
public Shape getShape() {
return new Circle();
}
}
  • 抽象工厂

工厂方法模式中专用工厂类只生产单一产品。如果需要生产相互是有关系或有依赖的整套产品,就使用抽象工厂。

public interface Factory {
public Gun produceGun();
public Bullet produceBullet();
}
public class AKFactory implements Factory{
@Override
public Gun produceGun() {
return new AK();
}
@Override
public Bullet produceBullet() {
return new AK_Bullet();
}
}

简单工厂+ 在项目中的应用:

/**
* 文件上传 Factory 使用了简单工厂
*/
public final class OSSFactory {
private static SysParamsService sysParamsService;
// 使用SpringContext工具类获取bean,因为该工厂类OSSFactory没有注入到IOC容器中去,所以无法在工厂类中无法注入bean
static {
OSSFactory.sysParamsService = SpringContextUtils.getBean(SysParamsService.class);
}
public static AbstractCloudStorageService build(CloudStorageConfig config) {
// 在工厂类中实例化相应的对象,根据配置类获取云存储类型(数据库中的数据),返回对应的云存储实例对象,并传入配置信息
if (config.getType() == Constant.CloudService.QINIU.getValue()) {
return new QiniuCloudStorageService(config);
} else if (config.getType() == Constant.CloudService.ALIYUN.getValue()) {
return new AliyunCloudStorageService(config);
} else if (config.getType() == Constant.CloudService.QCLOUD.getValue()) {
return new QcloudCloudStorageService(config);
}
return null;
}
}
class TestConsumer {
private static SysParamsService sysParamsService;
// 使用 SpringContext 工具类获取 bean,因为该工厂类 TestConsumer 没有注入到IOC容器中去,所以无法在工厂类中无法注入 bean
static {
sysParamsService = SpringContextUtils.getBean(SysParamsService.class);
}
// 从 sys_params 表中,根据 param_code = CLOUD_STORAGE_CONFIG_KEY 获取云存储配置信息 param_value
// 这里传入 CloudStorageConfig 类型,将 param_value 表中存储的 JSON 格式数据重新封装到 CloudStorageConfig 配置类中
CloudStorageConfig config = sysParamsService.getValueObject(Constant.CLOUD_STORAGE_CONFIG_KEY, CloudStorageConfig.class);
// AbstractCloudStorageService 为三个对象类的基类, 如下类图所示
AbstractCloudStorageService build = OSSFactory.build(config);
}

2、结构型模式

3、行为型模式

posted @   lam要努力  阅读(59)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示