当前位置: 首页 > news >正文

主要的软件设计模式及其在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中的实现示例

软件设计模式&#xff08;Software Design Patterns&#xff09;是面向对象设计中常用的解决方案&#xff0c;它们为常见的软件设计问题提供了一些被证明有效的解决方案。以下是一些主要的软件设计模式及其在Kotlin中的实现示例。 创建型模式&#xff08;Creational Patterns&…...

FFmpeg音频重采样基本流程

目录 流程概述用到的APItipsdemo样例附录 - SwrContext结构体字段 流程概述 音频重采样的基本流程为&#xff1a; 申请重采样器上下文设置重采样去上下文的参数初始化重采样器申请数据存放的缓冲区空间进行重采样 注意&#xff0c;要先设置参数再对重采样器初始化 用到的API…...

无人机无人车固态锂电池技术详解

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

ElementUI元件库在Axure中使用

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

联想M7615DNA打印机复印证件太黑的解决方法及个人建议

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

【算法题】无重复字符的最长子串(滑动窗口)

目录 一、题目描述 二、解题思路 1、什么是滑动窗口算法&#xff1f; 2、滑动窗口一般解题模板 三、参考答案 一、题目描述 无重复字符的最长子串 给定一个字符串s &#xff0c;请你找出其中不含有重复字符的最长子串的长度。 示例 1: 输入: s "abcabcbb"…...

Hikari连接池 最大连接数与最小空闲连接数配置多少合适?

spring:datasource: # 数据源的相关配置type: com.zaxxer.hikari.HikariDataSource # 数据源类型&#xff1a;HikariCPdriver-class-name: com.mysql.jdbc.Driver # mysql驱动url: jdbc:mysql://localhost:3306/t…...

【2.4 python中的基本输入和输出】

2.4 python中的基本输入和输出 在Python中&#xff0c;基本输入和输出是通过内置的input()函数和print()函数来实现的。这两个函数提供了与用户或其他程序进行交互的基本方式。 1. input() 函数 input() 函数用于从标准输入设备&#xff08;通常是键盘&#xff09;接收一行文…...

netty长连接集群方案

背景 公司某拍卖系统使用的netty服务不支持集群部署,不能进行横向扩展;并且和用户聚合服务耦合在一起,服务多节点部署不能提高拍卖性能,不能支撑更多用户使用拍卖。 目前需要改造并出一个集群的方案。 思路 因为是长连接的服务做集群,需要我们在客户端和服务器建立链接…...

Python面试题:结合Python技术,如何使用Keras进行神经网络建模

使用Keras进行神经网络建模是机器学习和深度学习领域中常用的方法之一。Keras是一个高级神经网络API&#xff0c;能够在TensorFlow、Theano等后端上运行&#xff0c;提供了简单易用的接口。下面是使用Keras进行神经网络建模的基本步骤&#xff1a; 安装Keras Keras是集成在Te…...

dll文件丢失怎么恢复?超简单的5个方法,1分钟搞定dll文件修复!

DLL&#xff0c;或称动态链接库&#xff0c;是一种重要的文件类型&#xff0c;包含了一系列用于运行几乎所有程序的指令&#xff0c;这些程序在win11、win10、win8和win7系统中都广泛使用。如果Windows操作系统中的dll文件丢失&#xff0c;您可能无法正常启动所需的程序或应用。…...

[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++解法),双指针刷穿地心!!!

写在前面&#xff1a;此篇博客是以[双指针总结]博客为基础的针对性训练&#xff0c;题源是codetop标签双指针近一年&#xff0c;频率由高到低 1.无重复字符的最长子串2.三数之和3.环形链表4.合并两个有序数组5.接雨水6.环形链表II7.删除链表的倒数第N个节点8.训练计划II9.最小覆…...

Floyd求最短路

给定一个 nn 个点 mm 条边的有向图&#xff0c;图中可能存在重边和自环&#xff0c;边权可能为负数。 再给定 kk 个询问&#xff0c;每个询问包含两个整数 xx 和 yy&#xff0c;表示查询从点 xx 到点 yy 的最短距离&#xff0c;如果路径不存在&#xff0c;则输出 impossible。…...

python爬虫初识

一、什么互联网 互联网&#xff08;Internet&#xff09;是全球范围内最大的计算机网络&#xff0c;它将数以百万计的私人、公共、学术、商业和政府网络通过一系列标准通信协议&#xff08;如TCP/IP&#xff09;连接起来形成的一个庞大的国际网络。 互联网的起源可以追溯到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的全面解析

&#x1f4dd;个人主页&#x1f339;&#xff1a;Eternity._ ⏩收录专栏⏪&#xff1a;C “ 登神长阶 ” &#x1f921;往期回顾&#x1f921;&#xff1a;Lambda表达式 &#x1f339;&#x1f339;期待您的关注 &#x1f339;&#x1f339; ❀C异常 &#x1f4d2;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原理及应用》后的读书笔记&#xff0c;记录目的是为了个人后续回顾复习使用。 文章目录 结构体结构体基础声明和定义结构体类型声明和定义结构体变量初始化结构体变量初始化各个成员使用列表符号初始化 使用结构体变量综上 结构体…...

sqli-labs注入漏洞解析--less-9/10

第九关&#xff1a; 这一关相比第八关&#xff0c;第八关他正确显示you are in,错误不显示you are in,但是第九关你不管是输入正确或者错误都显示 you are in &#xff0c;这个时候布尔盲注就不适合我们用&#xff0c;所以我们的换一下思路,布尔盲注适合页面对于错误和正确结果…...

AI-调查研究-01-正念冥想有用吗?对健康的影响及科学指南

点一下关注吧&#xff01;&#xff01;&#xff01;非常感谢&#xff01;&#xff01;持续更新&#xff01;&#xff01;&#xff01; &#x1f680; AI篇持续更新中&#xff01;&#xff08;长期更新&#xff09; 目前2025年06月05日更新到&#xff1a; AI炼丹日志-28 - Aud…...

谷歌浏览器插件

项目中有时候会用到插件 sync-cookie-extension1.0.0&#xff1a;开发环境同步测试 cookie 至 localhost&#xff0c;便于本地请求服务携带 cookie 参考地址&#xff1a;https://juejin.cn/post/7139354571712757767 里面有源码下载下来&#xff0c;加在到扩展即可使用FeHelp…...

Python:操作 Excel 折叠

💖亲爱的技术爱好者们,热烈欢迎来到 Kant2048 的博客!我是 Thomas Kant,很开心能在CSDN上与你们相遇~💖 本博客的精华专栏: 【自动化测试】 【测试经验】 【人工智能】 【Python】 Python 操作 Excel 系列 读取单元格数据按行写入设置行高和列宽自动调整行高和列宽水平…...

如何在看板中体现优先级变化

在看板中有效体现优先级变化的关键措施包括&#xff1a;采用颜色或标签标识优先级、设置任务排序规则、使用独立的优先级列或泳道、结合自动化规则同步优先级变化、建立定期的优先级审查流程。其中&#xff0c;设置任务排序规则尤其重要&#xff0c;因为它让看板视觉上直观地体…...

Qt Widget类解析与代码注释

#include "widget.h" #include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget) {ui->setupUi(this); }Widget::~Widget() {delete ui; }//解释这串代码&#xff0c;写上注释 当然可以&#xff01;这段代码是 Qt …...

Spring Boot面试题精选汇总

&#x1f91f;致敬读者 &#x1f7e9;感谢阅读&#x1f7e6;笑口常开&#x1f7ea;生日快乐⬛早点睡觉 &#x1f4d8;博主相关 &#x1f7e7;博主信息&#x1f7e8;博客首页&#x1f7eb;专栏推荐&#x1f7e5;活动信息 文章目录 Spring Boot面试题精选汇总⚙️ **一、核心概…...

10-Oracle 23 ai Vector Search 概述和参数

一、Oracle AI Vector Search 概述 企业和个人都在尝试各种AI&#xff0c;使用客户端或是内部自己搭建集成大模型的终端&#xff0c;加速与大型语言模型&#xff08;LLM&#xff09;的结合&#xff0c;同时使用检索增强生成&#xff08;Retrieval Augmented Generation &#…...

算法:模拟

1.替换所有的问号 1576. 替换所有的问号 - 力扣&#xff08;LeetCode&#xff09; ​遍历字符串​&#xff1a;通过外层循环逐一检查每个字符。​遇到 ? 时处理​&#xff1a; 内层循环遍历小写字母&#xff08;a 到 z&#xff09;。对每个字母检查是否满足&#xff1a; ​与…...

基于SpringBoot在线拍卖系统的设计和实现

摘 要 随着社会的发展&#xff0c;社会的各行各业都在利用信息化时代的优势。计算机的优势和普及使得各种信息系统的开发成为必需。 在线拍卖系统&#xff0c;主要的模块包括管理员&#xff1b;首页、个人中心、用户管理、商品类型管理、拍卖商品管理、历史竞拍管理、竞拍订单…...

tomcat指定使用的jdk版本

说明 有时候需要对tomcat配置指定的jdk版本号&#xff0c;此时&#xff0c;我们可以通过以下方式进行配置 设置方式 找到tomcat的bin目录中的setclasspath.bat。如果是linux系统则是setclasspath.sh set JAVA_HOMEC:\Program Files\Java\jdk8 set JRE_HOMEC:\Program Files…...