Design Patterns Self-Study: Principles, Singleton & Prototyp

Generated from prompt:

以“设计模式自学:设计原则与单例、原型模式”为主题的学习汇报PPT,内容包括: 1. 封面:主题《设计模式自学:设计原则与单例/原型模式》,汇报人姓名(杨佩瑶)和学号(2023032438)。 2. 学习目标:理解面向对象设计七大原则,掌握单例与原型模式。 3. 七大设计原则: - 单一职责原则(SRP) - 开闭原则(OCP) - 里氏替换原则(LSP) - 接口隔离原则(ISP) - 依赖倒置原则(DIP) - 迪米特法则(LoD) - 合成复用原则(CRP) 每个原则包括定义、反面案例、正面案例与小结。 4. 单例模式介绍: - 定义、特点与应用场景。 - Android InputMethodManager 示例分析。 5. 原型模式介绍: - 定义与用途。 - 示例:对象克隆实现与深浅拷贝比较。 6. 设计原则与模式的联系:原则指导模式设计,模式体现原则。 7. 学习心得与总结:学习设计原则帮助提升架构思维,单例与原型模式是创建型模式的核心。 风格:科技感、蓝色系、简洁结构化,适合课程汇报展示。

This presentation covers self-study on design patterns, focusing on the seven OOP principles (SRP, OCP, LSP, ISP, DIP, LoD, CRP) with definitions, counterexamples, and positives. It introduces Singlet

November 26, 202518 slides
Slide 1 of 18

Slide 1 - 封面

This title slide introduces a self-study presentation on design patterns, focusing on design principles along with the Singleton and Prototype patterns. It credits the presenter as Yang Peiyao with student ID 2023032438.

设计模式自学:设计原则与单例/原型模式

汇报人:杨佩瑶 学号:2023032438

Slide 1 - 封面
Slide 2 of 18

Slide 2 - 学习目标

This slide outlines the learning objectives for a session on object-oriented design, focusing on grasping the core principles of the seven key OOP design principles. It also covers mastering the concepts, applications, and implementations of the singleton and prototype patterns, while applying these principles to practical pattern usage.

学习目标

  • 理解面向对象设计七大原则的核心内涵
  • 掌握单例模式的核心概念与应用场景
  • 掌握原型模式的核心概念与实现方法
  • 运用设计原则指导模式实践应用
Slide 2 - 学习目标
Slide 3 of 18

Slide 3 - 七大设计原则

This slide serves as a section header titled "七大设计原则" (Seven Major Design Principles), marking it as Section 03. It introduces the SOLID principles along with extended rules to improve code design quality.

七大设计原则

03

七大设计原则

介绍SOLID原则及扩展法则,帮助提升代码设计质量

Slide 3 - 七大设计原则
Slide 4 of 18

Slide 4 - 单一职责原则(SRP)

The Single Responsibility Principle (SRP) states that a class should handle only one responsibility. For example, separating user management from data storage into distinct classes like UserManager and Database reduces coupling and improves maintainability, avoiding the complexity of multifunctional classes.

单一职责原则(SRP)

  • 定义:一个类只负责一个职责。
  • 反面案例:一个类同时处理用户管理和数据存储,导致修改复杂。
  • 正面案例:分离UserManager和Database类。
  • 小结:减少耦合,提高可维护性。
Slide 4 - 单一职责原则(SRP)
Slide 5 of 18

Slide 5 - 开闭原则(OCP)

The Open-Closed Principle (OCP) states that software entities should be open for extension but closed for modification. The slide illustrates this with a negative example where adding new shapes requires altering the Draw class, contrasted by a positive approach using an abstract Shape interface and polymorphism, enabling flexible extension through inheritance and interfaces.

开闭原则(OCP)

  • 定义:对扩展开放,对修改关闭。
  • 反面案例:添加新形状需修改Draw类。
  • 正面案例:使用抽象Shape接口和多态。
  • 小结:通过继承和接口实现灵活扩展。
Slide 5 - 开闭原则(OCP)
Slide 6 of 18

Slide 6 - 里氏替换原则(LSP)

The Liskov Substitution Principle (LSP) states that subclasses must be substitutable for their parent classes without affecting program correctness, ensuring inheritance reliability. A counterexample is a Square subclass breaking Rectangle's area calculations, while a positive example involves subclasses strictly adhering to the parent class's contract.

里氏替换原则(LSP)

  • 定义:子类可替换父类而不影响程序正确性。
  • 反例:Square子类破坏Rectangle面积计算。
  • 正例:子类严格遵守父类契约。
  • 小结:确保继承关系的可靠性。
Speaker Notes
定义:子类对象可替换父类而不影响程序正确性。反面案例:Rectangle子类Square破坏面积计算。正面案例:确保子类严格遵守父类契约。小结:保证继承的可靠性。
Slide 6 - 里氏替换原则(LSP)
Slide 7 of 18

Slide 7 - 接口隔离原则(ISP)

The Interface Segregation Principle (ISP) states that clients should not depend on interfaces they do not use, avoiding the pitfalls of large interfaces that force unrelated classes to implement unnecessary methods. Instead, it advocates breaking down interfaces into smaller, focused ones—like IPrint for printing and IScan for scanning—to enhance system flexibility and maintainability.

接口隔离原则(ISP)

  • 定义:客户端不应依赖不需要的接口。
  • 反面案例:大接口导致无关类实现多余方法。
  • 正面案例:拆分为小接口,如IPrint和IScan。
  • 小结:细粒度接口提升灵活性。
Slide 7 - 接口隔离原则(ISP)
Slide 8 of 18

Slide 8 - 依赖倒置原则(DIP)

The Dependency Inversion Principle (DIP) states that high-level modules should not depend on low-level modules; both should rely on abstractions to promote flexibility. The slide contrasts negative examples, like directly instantiating concrete classes which hinder maintenance, with positive ones using interface-based dependency injection for easy replacement, ultimately reducing coupling and enhancing testability and extensibility.

依赖倒置原则(DIP)

  • - 定义:高层模块不依赖低层模块,双方依赖抽象。
  • - 反面案例:直接new具体类,难以替换和维护。
  • - 正面案例:通过接口注入依赖,实现灵活替换。
  • - 小结:降低系统耦合,便于测试和扩展。

Source: 设计原则

Speaker Notes
定义:高层模块不依赖低层,双方依赖抽象。反面案例:直接new具体类,难以替换。正面案例:通过接口注入依赖。小结:降低耦合,便于测试。
Slide 8 - 依赖倒置原则(DIP)
Slide 9 of 18

Slide 9 - 迪米特法则(LoD)

The Law of Demeter (LoD), also known as the Demeter Principle, advises that classes should only communicate with their direct friends to minimize indirect dependencies. The slide contrasts a negative example of directly calling a private method in another class with a positive approach of interacting via public interfaces, ultimately reducing system coupling.

迪米特法则(LoD)

  • 只与直接朋友通信,减少间接依赖。
  • 反面案例:类A直接调用类B私有方法。
  • 正面案例:通过公共接口进行交互。
  • 小结:有效降低系统耦合度。
Speaker Notes
定义:只与直接朋友通信,减少间接依赖。反面案例:类A调用类B内部私有方法。正面案例:通过公共接口交互。小结:降低系统耦合度。
Slide 9 - 迪米特法则(LoD)
Slide 10 of 18

Slide 10 - 合成复用原则(CRP)

The Composite Reuse Principle (CRP) defines the preference for using composition over inheritance to reuse functionality in software design. It highlights negative examples like excessive inheritance causing fragile base class issues, contrasted with positive cases such as a Car class incorporating an Engine component, ultimately concluding that composition offers greater flexibility and avoids inheritance misuse.

合成复用原则(CRP)

  • 定义:优先使用组合而非继承复用功能。
  • 反面案例:过度继承导致脆弱基类问题。
  • 正面案例:Car包含Engine组件实现复用。
  • 小结:组合更灵活,避免继承滥用。
Slide 10 - 合成复用原则(CRP)
Slide 11 of 18

Slide 11 - 单例模式介绍

This slide introduces the Singleton design pattern as section 04. It explains that the pattern ensures a class has only one instance while providing a global access point to it.

单例模式介绍

04

单例模式介绍

确保类只有一个实例,并提供全局访问点。

Slide 11 - 单例模式介绍
Slide 12 of 18

Slide 12 - 单例模式定义、特点与应用场景

The Singleton design pattern ensures a class has only one instance and provides a global access point to it. It features support for global access, lazy loading, and thread safety, with common applications in configuration managers for unified parameter settings and loggers for a single output channel.

单例模式定义、特点与应用场景

  • 定义:确保类只有一个实例,并提供全局访问点。
  • 特点:支持全局访问、懒加载实现及线程安全机制。
  • 应用场景:配置管理器用于统一参数设置。
  • 应用场景:日志记录器实现单一日志输出通道。
Slide 12 - 单例模式定义、特点与应用场景
Slide 13 of 18

Slide 13 - Android InputMethodManager 示例分析

The slide analyzes Android's InputMethodManager, highlighting its singleton implementation via the getInstance() method to ensure a single system-wide instance, avoiding conflicts from multiple managers and providing unified input control. It outlines advantages like preventing resource competition and maintaining global consistency, alongside drawbacks such as global state issues that complicate testing and risk state pollution.

Android InputMethodManager 示例分析

单例实现机制优势与缺点
InputMethodManager 通过 getInstance() 方法实现单例模式,确保系统中只有一个输入法管理器实例。这避免了多个实例同时管理输入法导致的冲突,提供统一的输入控制接口。(28 字)优势:防止多实例竞争资源,确保输入法管理的全局一致性。缺点:引入全局状态,可能导致测试困难和状态污染问题,需要谨慎使用。(32 字)
Slide 13 - Android InputMethodManager 示例分析
Slide 14 of 18

Slide 14 - 原型模式介绍

This slide introduces the Prototype design pattern as section 05. It explains that the pattern enables creating new objects by cloning existing ones, thereby avoiding repetitive initialization processes.

原型模式介绍

05

原型模式介绍

通过克隆现有对象创建新对象,避免重复初始化。

Slide 14 - 原型模式介绍
Slide 15 of 18

Slide 15 - 原型模式定义与用途

The Prototype pattern is defined as using a prototype instance to specify object types and creating new objects through cloning, typically by implementing the Cloneable interface or a custom clone method. It is useful for efficiently copying complex objects to avoid repeated initialization, such as in game development for duplicating character attributes and states, ultimately reducing creation costs and enhancing system performance and flexibility.

原型模式定义与用途

  • 定义:通过原型实例指定对象种类,并通过克隆创建新对象。
  • 核心:实现Cloneable接口或自定义clone方法进行拷贝。
  • 用途:高效复制复杂对象,避免重复初始化过程。
  • 场景:游戏开发中快速复制角色属性和状态。
  • 优势:降低创建成本,提高系统性能和灵活性。
Slide 15 - 原型模式定义与用途
Slide 16 of 18

Slide 16 - 对象克隆实现与深浅拷贝比较

Shallow copy replicates only the object's reference without duplicating its sub-objects, causing shared modifications between the original and copy, which suits simple structures to minimize overhead. Deep copy recursively clones the object and all nested sub-objects for full independence, as exemplified by Java's Cloneable interface and clone() method, ensuring changes to one do not affect the other.

对象克隆实现与深浅拷贝比较

浅拷贝(Shallow Copy)深拷贝(Deep Copy)
浅拷贝仅复制对象的引用,而不复制子对象本身。原对象与拷贝对象共享子对象引用,导致修改一方会影响另一方。适用于简单数据结构,避免不必要的深层复制开销。(约25字)深拷贝递归复制对象及其所有嵌套子对象,确保拷贝完全独立。修改拷贝不会影响原对象。示例:在Java中使用Cloneable接口实现clone()方法,进行完整对象克隆。(约28字)
Speaker Notes
解释浅拷贝与深拷贝的区别,并提及Java Cloneable接口在原型模式中的应用。
Slide 16 - 对象克隆实现与深浅拷贝比较
Slide 17 of 18

Slide 17 - 设计原则与模式的联系

Design principles guide the creation of design patterns, as seen in how the Open-Closed Principle (OCP) is embodied in singleton pattern extensions. Design patterns, in turn, embody these principles—such as the Singleton following the Single Responsibility Principle (SRP) and the Prototype pattern supporting Dependency Inversion (DIP)—with principles serving as the foundation and patterns as practical tools.

设计原则与模式的联系

  • 原则指导模式设计,如OCP在单例扩展中体现
  • 模式体现设计原则,单例遵循SRP
  • 原型模式支持DIP,实现依赖倒置
  • 整体:原则为基础,模式为实践工具
Slide 17 - 设计原则与模式的联系
Slide 18 of 18

Slide 18 - 学习心得与总结

Studying design principles enhances architectural thinking and enables the creation of more robust code. Singleton and prototype patterns, as core creational modes, are highly applicable in practical development like Android, with plans to explore other patterns in the future.

学习心得与总结

- 学习设计原则提升架构思维,帮助编写更robust代码。

  • 单例与原型作为创建型模式核心,适用于实际开发如Android。
  • 未来继续探索其他模式!

感谢聆听,学习不止!

Source: 设计模式自学汇报

Speaker Notes
Call-to-action: 欢迎提问与讨论设计模式的应用!
Slide 18 - 学习心得与总结

Discover More Presentations

Explore thousands of AI-generated presentations for inspiration

Browse Presentations
Powered by AI

Create Your Own Presentation

Generate professional presentations in seconds with Karaf's AI. Customize this presentation or start from scratch.

Create New Presentation

Powered by Karaf.ai — AI-Powered Presentation Generator