主要的软件设计模式及其在Kotlin中的实现示例
软件设计模式(Software Design Patterns)是面向对象设计中常用的解决方案,它们为常见的软件设计问题提供了一些被证明有效的解决方案。以下是一些主要的软件设计模式及其在Kotlin中的实现示例。
创建型模式(Creational Patterns)
单例模式(Singleton Pattern)
确保一个类只有一个实例,并提供全局访问点。
object Singleton {init {println("Singleton instance created")}fun doSomething() {println("Doing something")}
}// 使用
fun main() {Singleton.doSomething()
}
工厂模式(Factory Pattern)
定义一个创建对象的接口,但由子类决定要实例化的类。
interface Product {fun use()
}class ConcreteProductA : Product {override fun use() {println("Using Product A")}
}class ConcreteProductB : Product {override fun use() {println("Using Product B")}
}class ProductFactory {fun createProduct(type: String): Product {return when (type) {"A" -> ConcreteProductA()"B" -> ConcreteProductB()else -> throw IllegalArgumentException("Unknown product type")}}
}// 使用
fun main() {val factory = ProductFactory()val productA = factory.createProduct("A")productA.use()val productB = factory.createProduct("B")productB.use()
}
抽象工厂模式(Abstract Factory Pattern)
提供一个接口,用于创建相关或依赖对象的家族,而不需要指定具体类。
interface Button {fun click()
}class WindowsButton : Button {override fun click() {println("Windows Button clicked")}
}class MacButton : Button {override fun click() {println("Mac Button clicked")}
}interface GUIFactory {fun createButton(): Button
}class WindowsFactory : GUIFactory {override fun createButton(): Button {return WindowsButton()}
}class MacFactory : GUIFactory {override fun createButton(): Button {return MacButton()}
}// 使用
fun main() {val factory: GUIFactory = WindowsFactory()val button = factory.createButton()button.click()
}
建造者模式(Builder Pattern)
将一个复杂对象的构建与其表示分离,使得同样的构建过程可以创建不同的表示。
class Product private constructor(builder: Builder) {val partA: String?val partB: String?val partC: String?init {partA = builder.partApartB = builder.partBpartC = builder.partC}class Builder {var partA: String? = nullprivate setvar partB: String? = nullprivate setvar partC: String? = nullprivate setfun setPartA(partA: String) = apply { this.partA = partA }fun setPartB(partB: String) = apply { this.partB = partB }fun setPartC(partC: String) = apply { this.partC = partC }fun build() = Product(this)}
}// 使用
fun main() {val product = Product.Builder().setPartA("A").setPartB("B").setPartC("C").build()println("Product parts: ${product.partA}, ${product.partB}, ${product.partC}")
}
结构型模式(Structural Patterns)
适配器模式(Adapter Pattern)
将一个类的接口转换成客户希望的另一个接口,使得原本由于接口不兼容而不能一起工作的类可以协同工作。
interface Target {fun request()
}class Adaptee {fun specificRequest() {println("Specific request")}
}class Adapter(private val adaptee: Adaptee) : Target {override fun request() {adaptee.specificRequest()}
}// 使用
fun main() {val adaptee = Adaptee()val adapter = Adapter(adaptee)adapter.request()
}
装饰器模式(Decorator Pattern)
动态地将责任附加到对象上,提供了一种灵活替代继承的方法来扩展功能。
interface Component {fun operation()
}class ConcreteComponent : Component {override fun operation() {println("Concrete Component operation")}
}open class Decorator(private val component: Component) : Component {override fun operation() {component.operation()}
}class ConcreteDecorator(component: Component) : Decorator(component) {override fun operation() {super.operation()addedBehavior()}private fun addedBehavior() {println("Added behavior")}
}// 使用
fun main() {val component: Component = ConcreteDecorator(ConcreteComponent())component.operation()
}
代理模式(Proxy Pattern)
为另一个对象提供一个代理以控制对这个对象的访问。
interface Subject {fun request()
}class RealSubject : Subject {override fun request() {println("RealSubject request")}
}class Proxy(private val realSubject: RealSubject) : Subject {override fun request() {println("Proxy request")realSubject.request()}
}// 使用
fun main() {val realSubject = RealSubject()val proxy = Proxy(realSubject)proxy.request()
}
外观模式(Facade Pattern)
为子系统中的一组接口提供一个一致的界面,使得子系统更容易使用。
class SubsystemA {fun operationA() {println("Subsystem A operation")}
}class SubsystemB {fun operationB() {println("Subsystem B operation")}
}class Facade {private val subsystemA = SubsystemA()private val subsystemB = SubsystemB()fun operation() {subsystemA.operationA()subsystemB.operationB()}
}// 使用
fun main() {val facade = Facade()facade.operation()
}
行为型模式(Behavioral Patterns)
策略模式(Strategy Pattern)
定义一系列算法,把它们一个个封装起来,并且使它们可以相互替换。
interface Strategy {fun execute()
}class ConcreteStrategyA : Strategy {override fun execute() {println("Executing Strategy A")}
}class ConcreteStrategyB : Strategy {override fun execute() {println("Executing Strategy B")}
}class Context(private var strategy: Strategy) {fun setStrategy(strategy: Strategy) {this.strategy = strategy}fun executeStrategy() {strategy.execute()}
}// 使用
fun main() {val context = Context(ConcreteStrategyA())context.executeStrategy()context.setStrategy(ConcreteStrategyB())context.executeStrategy()
}
观察者模式(Observer Pattern)
定义对象间的一种一对多的依赖关系,以便当一个对象的状态发生改变时,所有依赖于它的对象都会得到通知并自动更新。
interface Observer {fun update()
}class ConcreteObserver : Observer {override fun update() {println("Observer updated")}
}class Subject {private val observers = mutableListOf<Observer>()fun addObserver(observer: Observer) {observers.add(observer)}fun removeObserver(observer: Observer) {observers.remove(observer)}fun notifyObservers() {for (observer in observers) {observer.update()}}
}// 使用
fun main() {val subject = Subject()val observer = ConcreteObserver()subject.addObserver(observer)subject.notifyObservers()subject.removeObserver(observer)subject.notifyObservers()
}
命令模式(Command Pattern)
将请求封装成对象,从而使得您可以用不同的请求对客户进行参数化。
interface Command {fun execute()
}class ConcreteCommand(private val receiver: Receiver) : Command {override fun execute() {receiver.action()}
}class Receiver {fun action() {println("Receiver action")}
}class Invoker {private lateinit var command: Commandfun setCommand(command: Command) {this.command = command}fun executeCommand() {command.execute()}
}// 使用
fun main() {val receiver = Receiver()val command = ConcreteCommand(receiver)val invoker = Invoker()invoker.setCommand(command)invoker.executeCommand()
}
责任链模式(Chain of Responsibility Pattern)
使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合。
abstract class Handler {var nextHandler: Handler? = nullfun handleRequest(request: String) {if (canHandle(request)) {process(request)} else {nextHandler?.handleRequest(request)}}protected abstract fun canHandle(request: String): Booleanprotected abstract fun process(request: String)
}class ConcreteHandlerA : Handler() {override fun canHandle(request: String) = request == "A"override fun process(request: String) {println("Handler A processed $request")}
}class ConcreteHandlerB : Handler(){override fun canHandle(request: String) = request == "B"override fun process(request: String) {println("Handler B processed $request")}
}// 使用
fun main() {val handlerA = ConcreteHandlerA()val handlerB = ConcreteHandlerB()handlerA.nextHandler = handlerBhandlerA.handleRequest("A")handlerA.handleRequest("B")
}
这些示例展示了Kotlin中如何实现一些常见的设计模式。每个模式都有其特定的用途和场景,选择适合的模式可以大大提升代码的可维护性和扩展性。
我有多年软件开发经验,精通嵌入式STM32,RTOS,Linux,Ubuntu, Android AOSP, Android APP, Java , Kotlin , C, C++, Python , QT。 如果您有软件开发定制需求,请联系我,电子邮件: mysolution@qq.com
相关文章:
主要的软件设计模式及其在Kotlin中的实现示例
软件设计模式(Software Design Patterns)是面向对象设计中常用的解决方案,它们为常见的软件设计问题提供了一些被证明有效的解决方案。以下是一些主要的软件设计模式及其在Kotlin中的实现示例。 创建型模式(Creational Patterns&…...
FFmpeg音频重采样基本流程
目录 流程概述用到的APItipsdemo样例附录 - SwrContext结构体字段 流程概述 音频重采样的基本流程为: 申请重采样器上下文设置重采样去上下文的参数初始化重采样器申请数据存放的缓冲区空间进行重采样 注意,要先设置参数再对重采样器初始化 用到的API…...

无人机无人车固态锂电池技术详解
随着无人机和无人车技术的飞速发展,对高性能、高安全性电池的需求日益迫切。固态锂电池作为下一代电池技术的代表,正逐步从实验室走向市场,为无人机和无人车等应用领域带来革命性的变化。相比传统液态锂电池,固态锂电池在能量密度…...

ElementUI元件库在Axure中使用
一、ElementUI元件库介绍 ElementUI 是一套为开发者、UI/UX设计师和产品经理准备的基于Vue 2.0的桌面端组件库。它以其优雅的设计和丰富的组件,极大地提升了Web应用的开发效率与用户体验。ElementUI的组件设计精致且符合现代UI规范,包括按钮、表单、弹窗…...

联想M7615DNA打印机复印证件太黑的解决方法及个人建议
打印机在使用过程中,可能会出现复印的文字或图片太黑的问题,这会影响到打印或复印的效果。下面我们来了解一下这种情况的原因和解决方法;以下所述操作仅供大家参考,如有不足请大家提出宝贵意见; 证件包括:…...

【算法题】无重复字符的最长子串(滑动窗口)
目录 一、题目描述 二、解题思路 1、什么是滑动窗口算法? 2、滑动窗口一般解题模板 三、参考答案 一、题目描述 无重复字符的最长子串 给定一个字符串s ,请你找出其中不含有重复字符的最长子串的长度。 示例 1: 输入: s "abcabcbb"…...
Hikari连接池 最大连接数与最小空闲连接数配置多少合适?
spring:datasource: # 数据源的相关配置type: com.zaxxer.hikari.HikariDataSource # 数据源类型:HikariCPdriver-class-name: com.mysql.jdbc.Driver # mysql驱动url: jdbc:mysql://localhost:3306/t…...
【2.4 python中的基本输入和输出】
2.4 python中的基本输入和输出 在Python中,基本输入和输出是通过内置的input()函数和print()函数来实现的。这两个函数提供了与用户或其他程序进行交互的基本方式。 1. input() 函数 input() 函数用于从标准输入设备(通常是键盘)接收一行文…...
netty长连接集群方案
背景 公司某拍卖系统使用的netty服务不支持集群部署,不能进行横向扩展;并且和用户聚合服务耦合在一起,服务多节点部署不能提高拍卖性能,不能支撑更多用户使用拍卖。 目前需要改造并出一个集群的方案。 思路 因为是长连接的服务做集群,需要我们在客户端和服务器建立链接…...
Python面试题:结合Python技术,如何使用Keras进行神经网络建模
使用Keras进行神经网络建模是机器学习和深度学习领域中常用的方法之一。Keras是一个高级神经网络API,能够在TensorFlow、Theano等后端上运行,提供了简单易用的接口。下面是使用Keras进行神经网络建模的基本步骤: 安装Keras Keras是集成在Te…...

dll文件丢失怎么恢复?超简单的5个方法,1分钟搞定dll文件修复!
DLL,或称动态链接库,是一种重要的文件类型,包含了一系列用于运行几乎所有程序的指令,这些程序在win11、win10、win8和win7系统中都广泛使用。如果Windows操作系统中的dll文件丢失,您可能无法正常启动所需的程序或应用。…...

[Meachines] [Easy] Sense PFSense防火墙RCE
信息收集 IP AddressOpening Ports10.10.10.60TCP:80,443 $ nmap -p- 10.10.10.60 --min-rate 1000 -sC -sV PORT STATE SERVICE VERSION 80/tcp open http lighttpd 1.4.35 |_http-title: Did not follow redirect to https://10.10.10.60/ |_http-server-header…...

codetop标签双指针题目大全解析(C++解法),双指针刷穿地心!!!
写在前面:此篇博客是以[双指针总结]博客为基础的针对性训练,题源是codetop标签双指针近一年,频率由高到低 1.无重复字符的最长子串2.三数之和3.环形链表4.合并两个有序数组5.接雨水6.环形链表II7.删除链表的倒数第N个节点8.训练计划II9.最小覆…...
Floyd求最短路
给定一个 nn 个点 mm 条边的有向图,图中可能存在重边和自环,边权可能为负数。 再给定 kk 个询问,每个询问包含两个整数 xx 和 yy,表示查询从点 xx 到点 yy 的最短距离,如果路径不存在,则输出 impossible。…...

python爬虫初识
一、什么互联网 互联网(Internet)是全球范围内最大的计算机网络,它将数以百万计的私人、公共、学术、商业和政府网络通过一系列标准通信协议(如TCP/IP)连接起来形成的一个庞大的国际网络。 互联网的起源可以追溯到196…...

Java中类的构造
1.私有化成员变量。 2.空参构造方法。 3.带全部参数的构造方法。 4.get / set方法。 package demo;public class student{//1.私有化成员变量。//2.空参构造方法。//3.带全部参数的构造方法。//4.get / set方法。private String name;private int age;public student() {}pu…...

【C++高阶】深入理解C++异常处理机制:从try到catch的全面解析
📝个人主页🌹:Eternity._ ⏩收录专栏⏪:C “ 登神长阶 ” 🤡往期回顾🤡:Lambda表达式 🌹🌹期待您的关注 🌹🌹 ❀C异常 📒1. C异常概念…...

【RHEL7】无人值守安装系统
目录 一、kickstart服务 1.下载kickstart 2.启动图形制作工具 3.选择设置 4.查看生成的文件 5.修改ks.cfg文件 二、HTTP服务 1.下载HTTP服务 2.启动HTTP服务 3.将挂载文件和ks.cfg放在HTTP默认目录下 4.测试HTTP服务 三、PXE 1.查看pxe需要安装什么 2.安装 四、…...

[RTOS 学习记录] 预备知识:C语言结构体
这篇文章是我阅读《嵌入式实时操作系统μCOS-II原理及应用》后的读书笔记,记录目的是为了个人后续回顾复习使用。 文章目录 结构体结构体基础声明和定义结构体类型声明和定义结构体变量初始化结构体变量初始化各个成员使用列表符号初始化 使用结构体变量综上 结构体…...

sqli-labs注入漏洞解析--less-9/10
第九关: 这一关相比第八关,第八关他正确显示you are in,错误不显示you are in,但是第九关你不管是输入正确或者错误都显示 you are in ,这个时候布尔盲注就不适合我们用,所以我们的换一下思路,布尔盲注适合页面对于错误和正确结果…...
在HarmonyOS ArkTS ArkUI-X 5.0及以上版本中,手势开发全攻略:
在 HarmonyOS 应用开发中,手势交互是连接用户与设备的核心纽带。ArkTS 框架提供了丰富的手势处理能力,既支持点击、长按、拖拽等基础单一手势的精细控制,也能通过多种绑定策略解决父子组件的手势竞争问题。本文将结合官方开发文档,…...
FastAPI 教程:从入门到实践
FastAPI 是一个现代、快速(高性能)的 Web 框架,用于构建 API,支持 Python 3.6。它基于标准 Python 类型提示,易于学习且功能强大。以下是一个完整的 FastAPI 入门教程,涵盖从环境搭建到创建并运行一个简单的…...

【CSS position 属性】static、relative、fixed、absolute 、sticky详细介绍,多层嵌套定位示例
文章目录 ★ position 的五种类型及基本用法 ★ 一、position 属性概述 二、position 的五种类型详解(初学者版) 1. static(默认值) 2. relative(相对定位) 3. absolute(绝对定位) 4. fixed(固定定位) 5. sticky(粘性定位) 三、定位元素的层级关系(z-i…...
C++.OpenGL (10/64)基础光照(Basic Lighting)
基础光照(Basic Lighting) 冯氏光照模型(Phong Lighting Model) #mermaid-svg-GLdskXwWINxNGHso {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-GLdskXwWINxNGHso .error-icon{fill:#552222;}#mermaid-svg-GLd…...
LLM基础1_语言模型如何处理文本
基于GitHub项目:https://github.com/datawhalechina/llms-from-scratch-cn 工具介绍 tiktoken:OpenAI开发的专业"分词器" torch:Facebook开发的强力计算引擎,相当于超级计算器 理解词嵌入:给词语画"…...

让AI看见世界:MCP协议与服务器的工作原理
让AI看见世界:MCP协议与服务器的工作原理 MCP(Model Context Protocol)是一种创新的通信协议,旨在让大型语言模型能够安全、高效地与外部资源进行交互。在AI技术快速发展的今天,MCP正成为连接AI与现实世界的重要桥梁。…...

图表类系列各种样式PPT模版分享
图标图表系列PPT模版,柱状图PPT模版,线状图PPT模版,折线图PPT模版,饼状图PPT模版,雷达图PPT模版,树状图PPT模版 图表类系列各种样式PPT模版分享:图表系列PPT模板https://pan.quark.cn/s/20d40aa…...
现有的 Redis 分布式锁库(如 Redisson)提供了哪些便利?
现有的 Redis 分布式锁库(如 Redisson)相比于开发者自己基于 Redis 命令(如 SETNX, EXPIRE, DEL)手动实现分布式锁,提供了巨大的便利性和健壮性。主要体现在以下几个方面: 原子性保证 (Atomicity)ÿ…...
人工智能--安全大模型训练计划:基于Fine-tuning + LLM Agent
安全大模型训练计划:基于Fine-tuning LLM Agent 1. 构建高质量安全数据集 目标:为安全大模型创建高质量、去偏、符合伦理的训练数据集,涵盖安全相关任务(如有害内容检测、隐私保护、道德推理等)。 1.1 数据收集 描…...

Ubuntu系统多网卡多相机IP设置方法
目录 1、硬件情况 2、如何设置网卡和相机IP 2.1 万兆网卡连接交换机,交换机再连相机 2.1.1 网卡设置 2.1.2 相机设置 2.3 万兆网卡直连相机 1、硬件情况 2个网卡n个相机 电脑系统信息,系统版本:Ubuntu22.04.5 LTS;内核版本…...