沉梦听雨的编程指南 沉梦听雨的编程指南
首页
  • 基础篇
  • 集合篇
  • 并发篇
  • JVM
  • 新特性
  • 计算机网络
  • 操作系统
  • 数据结构与算法
  • 基础篇
  • MySql
  • Redis
  • 达梦数据库
  • Spring
  • SpringBoot
  • Mybatis
  • Shiro
  • 设计须知
  • UML画图
  • 权限校验
  • 设计模式
  • API网关
  • RPC
  • 消息队列
  • SpringCloud
  • 分布式事务
  • 云存储
  • 搜索引擎
  • 多媒体框架
  • 虚拟机
  • 开发工具篇
  • 工具库篇
  • 开发技巧篇
  • 工具类系列
  • 随笔
  • 前端环境搭建
  • HTML与CSS
  • JS学习
  • Vue3入门
  • Vue3进阶
  • 黑马Vue3
  • 脚手架搭建
  • 瑞吉外卖
  • 黑马点评
  • vue-blog
  • 沉梦接口开放平台
  • 用户中心
  • 聚合搜索平台
  • 仿12306项目
  • 壁纸小程序项目
  • RuoYi-Vue
  • 博客搭建
  • 网站收藏箱
  • 断墨寻径摘录
  • 费曼学习法
Github (opens new window)

沉梦听雨

时间是最好的浸渍剂,而沉淀是最好的提纯器🚀
首页
  • 基础篇
  • 集合篇
  • 并发篇
  • JVM
  • 新特性
  • 计算机网络
  • 操作系统
  • 数据结构与算法
  • 基础篇
  • MySql
  • Redis
  • 达梦数据库
  • Spring
  • SpringBoot
  • Mybatis
  • Shiro
  • 设计须知
  • UML画图
  • 权限校验
  • 设计模式
  • API网关
  • RPC
  • 消息队列
  • SpringCloud
  • 分布式事务
  • 云存储
  • 搜索引擎
  • 多媒体框架
  • 虚拟机
  • 开发工具篇
  • 工具库篇
  • 开发技巧篇
  • 工具类系列
  • 随笔
  • 前端环境搭建
  • HTML与CSS
  • JS学习
  • Vue3入门
  • Vue3进阶
  • 黑马Vue3
  • 脚手架搭建
  • 瑞吉外卖
  • 黑马点评
  • vue-blog
  • 沉梦接口开放平台
  • 用户中心
  • 聚合搜索平台
  • 仿12306项目
  • 壁纸小程序项目
  • RuoYi-Vue
  • 博客搭建
  • 网站收藏箱
  • 断墨寻径摘录
  • 费曼学习法
Github (opens new window)
  • 设计须知

  • UML画图

  • 权限校验

  • 设计模式

    • 设计模式基础入门
    • 设计模式七大原则
    • 单例模式
    • 工厂模式
    • 原型模式
    • 建造者模式
    • 适配器模式
    • 桥接模式
    • 装饰者模式
    • 外观模式
    • 模板方法模式
    • 常见设计模式总结
    • 设计模式 13 问
      • 1、谈谈你对设计模式的理解?
      • 2、谈谈你对工厂方法模式的理解?
      • 3、谈谈你对简单工厂模式的理解?
      • 4、谈谈你对单例模式的理解?
      • 5、谈谈你对代理模式的理解?
      • 6、谈谈你对模板模式的理解?
      • 7、谈谈你对观察者模式的理解?
      • 8、谈谈你对装饰器模式的理解?
      • 9、谈谈你对策略模式的理解?
      • 10、谈谈你对适配器模式的理解?
      • 11、谈谈 IO 中用到的设计模式?
      • 12、谈谈 Spring 中用到的设计模式?
      • 13、谈谈 MyBatis 中用到的设计模式?
  • 系统设计
  • 设计模式
沉梦听雨
2023-07-27
目录

设计模式 13 问

# 设计模式 13 问

# 1、谈谈你对设计模式的理解?

  1. 设计模式是一种被广泛应用的解决问题的经验总结,它提供了一套可复用、可扩展、易于维护的解决方案。设计模式可以帮助我们更好地组织代码,提高代码质量和可读性。

    我将他理解成是一种由以前的程序员在实际工作中反复使用,总结得出的经验,最终形成的使用的模式。

  2. 有 7 个常用的软件设计原则

    • 开闭原则 -- 对扩展开放,对修改关闭

    • 单一职责原则 -- ⼀个类只负责⼀个功能领域中的相应职责

    • 里氏替换原则 -- 所有引用基类的地方必须能透明地使用其子类的对象

    • 依赖倒置原则 -- 依赖于抽象,不能依赖于具体实现

    • 接口隔离原则 -- 类之间的依赖关系应该建立在最小的接口上

    • 合成/聚合复用原则 -- 尽量使用合成/聚合,而不是通过继承达到复用的目的

    • 最少知识原则 或者 迪米特法则 -- 一个软件实体应当尽可能少的与其他实体发生相互作用

  3. 主要有三大分类

    • 创建型模式
    • 结构型模式
    • 行为型模式

    image

# 2、谈谈你对工厂方法模式的理解?

工厂方法模式是一种创建型设计模式,它将对象的创建委托给子类,由子类决定实例化哪个类。这样可以将对象的创建与使用分离,提高了代码的可扩展性和灵活性。

interface Product {
    void show();
}

class ConcreteProduct implements Product {
    @Override
    public void show() {
        System.out.println("具体产品");
    }
}

abstract class Factory {
    public abstract Product createProduct();
}

class ConcreteFactory extends Factory {
    @Override
    public Product createProduct() {
        return new ConcreteProduct();
    }
}

// 在客户端代码中使用
public static void main(String[] args) {
    Factory factory = new ConcreteFactory();
    Product product = factory.createProduct();
    product.show(); // 输出:具体产品
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

# 3、谈谈你对简单工厂模式的理解?

简单工厂模式是一种创建型设计模式,它通过一个工厂类来创建对象,而不是直接在客户端代码中实例化对象。简单工厂模式简化了对象的创建过程,对客户端隐藏了对象的创建细节。

interface Product {
    void show();
}

class ConcreteProductA implements Product {
    @Override
    public void show() {
        System.out.println("产品A");
    }
}

class ConcreteProductB implements Product {
    @Override
    public void show() {
        System.out.println("产品B");
    }
}

class SimpleFactory {
    public static Product createProduct(String type) {
        if ("A".equals(type)) {
            return new ConcreteProductA();
        } else if ("B".equals(type)) {
            return new ConcreteProductB();
        } else {
            throw new IllegalArgumentException("无效的产品类型");
        }
    }
}

// 在客户端代码中使用
public static void main(String[] args) {
    Product productA = SimpleFactory.createProduct("A");
    Product productB = SimpleFactory.createProduct("B");
    productA.show(); // 输出:产品A
    productB.show(); // 输出:产品B
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

# 4、谈谈你对单例模式的理解?

单例模式是一种创建型设计模式,它确保一个类只有一个实例,并提供一个全局访问点。单例模式通常用于管理全局资源或控制某些共享资源的访问。

class Singleton {
    private static Singleton instance;

    private Singleton() {
        // 私有构造方法
    }

    public static Singleton getInstance() {
        if (instance == null) {
            instance = new Singleton();
        }
        return instance;
    }
}

// 在客户端代码中使用
public static void main(String[] args) {
    Singleton singleton = Singleton.getInstance();
    // 使用singleton对象进行操作
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

# 5、谈谈你对代理模式的理解?

代理模式是一种结构型设计模式,它通过一个代理类来控制对另一个对象的访问。代理模式可以用于实现延迟加载、权限控制、远程访问等功能。

interface Subject {
    void request();
}

class RealSubject implements Subject {
    @Override
    public void request() {
        System.out.println("真实主题");
    }
}

class Proxy implements Subject {
    private RealSubject realSubject;

    @Override
    public void request() {
        if (realSubject == null) {
            realSubject = new RealSubject();
        }
        System.out.println("代理前处理");
        realSubject.request();
        System.out.println("代理后处理");
    }
}

// 在客户端代码中使用
public static void main(String[] args) {
    Subject proxy = new Proxy();
    proxy.request();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

# 6、谈谈你对模板模式的理解?

模板模式是一种行为型设计模式,它定义了一个算法的骨架,将一些步骤的具体实现延迟到子类中。模板模式可以提高代码的复用性和可扩展性。

abstract class AbstractClass {
    public void templateMethod() {
        // 一些公共的处理逻辑
        specificMethod();
        // 一些其他的公共处理逻辑
    }

    protected abstract void specificMethod();
}

class ConcreteClass extends AbstractClass {
    @Override
    protected void specificMethod() {
        System.out.println("具体子类实现");
    }
}

// 在客户端代码中使用
public static void main(String[] args) {
    AbstractClass abstractClass = new ConcreteClass();
    abstractClass.templateMethod();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

# 7、谈谈你对观察者模式的理解?

观察者模式是一种行为型设计模式,它定义了一种一对多的依赖关系,当一个对象状态发生改变时,它的所有依赖者都会收到通知。观察者模式可以用于实现事件监听、消息订阅等功能。

interface Observer {
    void update(String message);
}

class ConcreteObserver implements Observer {
    @Override
    public void update(String message) {
        System.out.println("收到消息:" + message);
    }
}

class Subject {
    private List<Observer> observers = new ArrayList<>();

    public void addObserver(Observer observer) {
        observers.add(observer);
    }

    public void removeObserver(Observer observer) {
        observers.remove(observer);
    }

    public void notifyObservers(String message) {
        for (Observer observer : observers) {
            observer.update(message);
        }
    }
}

// 在客户端代码中使用
public static void main(String[] args) {
    Subject subject = new Subject();
    Observer observer1 = new ConcreteObserver();
    Observer observer2 = new ConcreteObserver();

    subject.addObserver(observer1);
    subject.addObserver(observer2);

    subject.notifyObservers("Hello, observers!");
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

# 8、谈谈你对装饰器模式的理解?

装饰器模式是一种结构型设计模式,它动态地给一个对象添加一些额外的功能,而不影响其接口。装饰器模式可以用于扩展一个类的功能,而不需要修改其原始代码。

interface Component {
    void operation();
}

class ConcreteComponent implements Component {
    @Override
    public void operation() {
        System.out.println("具体组件");
    }
}

abstract class Decorator implements Component {
    protected Component component;

    public Decorator(Component component) {
        this.component = component;
    }

    @Override
    public void operation() {
        component.operation();
    }
}

class ConcreteDecorator extends Decorator {
    public ConcreteDecorator(Component component) {
        super(component);
    }

    @Override
    public void operation() {
        super.operation();
        System.out.println("装饰器扩展功能");
    }
}

// 在客户端代码中使用
public static void main(String[] args) {
    Component component = new ConcreteComponent();
    Component decorator = new ConcreteDecorator(component);
    decorator.operation();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

# 9、谈谈你对策略模式的理解?

策略模式是一种行为型设计模式,它定义了一系列算法,并使它们可以相互替换,让算法的变化独立于使用算法的客户端。策略模式可以提高代码的灵活性和可维护性。

interface Strategy {
    void algorithm();
}

class ConcreteStrategyA implements Strategy {
    @Override
    public void algorithm() {
        System.out.println("具体策略A");
    }
}

class ConcreteStrategyB implements Strategy {
    @Override
    public void algorithm() {
        System.out.println("具体策略B");
    }
}

class Context {
    private Strategy strategy;

    public Context(Strategy strategy) {
        this.strategy = strategy;
    }

    public void execute() {
        strategy.algorithm();
    }
}

// 在客户端代码中使用
public static void main(String[] args) {
    Strategy strategyA = new ConcreteStrategyA();
    Strategy strategyB = new ConcreteStrategyB();

    Context contextA = new Context(strategyA);
    Context contextB = new Context(strategyB);

    contextA.execute(); // 输出:具体策略A
    contextB.execute(); // 输出:具体策略B
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

# 10、谈谈你对适配器模式的理解?

适配器模式是一种结构型设计模式,它将一个类的接口转换成客户端所期待的接口,使得原本不兼容的类可以协同工作。适配器模式可以用于解决不同接口之间的兼容性问题。

interface Target {
    void request();
}

class Adaptee {
    public void specificRequest() {
        System.out.println("适配者的方法");
    }
}

class Adapter implements Target {
    private Adaptee adaptee;

    public Adapter(Adaptee adaptee) {
        this.adaptee = adaptee;
    }

    @Override
    public void request() {
        adaptee.specificRequest();
    }
}

// 在客户端代码中使用
public static void main(String[] args) {
    Adaptee adaptee = new Adaptee();
    Target target = new Adapter(adaptee);
    target.request();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

# 11、谈谈 IO 中用到的设计模式?

在IO中,常用的设计模式包括装饰器模式(例如BufferedInputStream)、观察者模式(例如Java IO的事件监听器)、适配器模式(例如InputStreamReader)等。

InputStream inputStream = new FileInputStream("file.txt");
inputStream = new BufferedInputStream(inputStream);
inputStream = new DataInputStream(inputStream);
// 在这里,BufferedInputStream 和 DataInputStream 就是装饰器,通过组合方式给 FileInputStream 添加了缓冲和处理基本数据类型的功能。
1
2
3
4

# 12、谈谈 Spring 中用到的设计模式?

在Spring中,常用的设计模式包括单例模式(例如Spring容器管理Bean的单例实例)、模板模式(例如JdbcTemplate)、代理模式(例如AOP实现)等。

// IoC示例
@Service
public class MyService {
    private MyRepository repository;

    @Autowired
    public MyService(MyRepository repository) {
        this.repository = repository;
    }

    // ...
}

// AOP示例
@Aspect
@Component
public class MyAspect {
    @Before("execution(* com.example.MyService.*(..))")
    public void beforeAdvice() {
        System.out.println("执行前置通知");
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

# 13、谈谈 MyBatis 中用到的设计模式?

在MyBatis中,常用的设计模式包括工厂模式(例如SqlSessionFactoryBuilder用于创建SqlSessionFactory)、模板模式(例如BaseExecutor中定义SqlSession的模板方法)等。

SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session = factory.openSession();

// Mapper接口代理示例
MapperInterface mapper = session.getMapper(MapperInterface.class);
1
2
3
4
5
上次更新: 2024/11/22 17:53:04
常见设计模式总结

← 常见设计模式总结

Theme by Vdoing | Copyright © 2023-2025 沉梦听雨 | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式