当前位置:网站首页>抽象工厂模式(Swift 实现)
抽象工厂模式(Swift 实现)
2022-07-30 05:43:00 【大头鑫】
Abstract Factory 抽象工厂
Base on the factory method, add an abstract factory. We can call the same abstract factory method to create different mode’s product. If we want to create another mode’s product, we need to change the concrete factory.
基于工厂方法模式,我们添加了一个抽象工厂协议,其它具体的工厂来实现这个工厂协议。从而我们可以调用同一个抽象工厂方法来创建不同模式的相似产品(比如 WinButton 和 MacButton)。如果想要切换模式,我们需要切换到另外一种具体的工厂实现。

// Abstract factory
protocol GUIFactory {
func createButton() -> Button
func createCheckBox() -> CheckBox
}
// Concrete factory
class WinGUIFactory: GUIFactory {
func createButton() -> Button {
var btn = WinButton()
return btn
}
func createCheckBox() -> CheckBox {
var box = WinCheckBox()
return box
}
}
// Concrete factory
class MacGUIFactory: GUIFactory {
func createButton() -> Button {
var btn = MacButton()
return btn
}
func createCheckBox() -> CheckBox {
var box = MacCheckBox()
return box
}
}
// Abstract product
protocol Button {
func click()
}
// Abstract product
protocol CheckBox {
func render()
}
// Concrete product
class WinButton: Button {
func click() {
print("Win click button")
}
}
// Concrete product
class WinCheckBox: CheckBox {
func render() {
print("Here the win render checkBox")
}
}
// Concrete product
class MacButton: Button {
func click() {
print("Mac click button")
}
}
// Concrete product
class MacCheckBox: CheckBox {
func render() {
print("Here the mac render checkBox")
}
}
class App {
var factory: GUIFactory
init(factory: GUIFactory) {
self.factory = factory
}
func someOperation() {
var checkBox = factory.createCheckBox()
checkBox.render()
}
}
let factory = WinGUIFactory()
let app = App(factory: factory) // The mode is choosen now, and the behavior in the app is specific.
let button = app.factory.createButton();
button.click()
app.someOperation()
边栏推荐
- Jdbc & Mysql timeout analysis
- Arthas 命令解析(jvm/thread/stack/heapdump)
- "MySQL Advanced Chapter" four, the storage structure of the index
- Understand JDBC in one article
- Thread state of five
- TDengine cluster construction
- Flink CDC implements Postgres to MySQL streaming processing transmission case
- 线程的5种状态
- 互联网商城盲盒app为何如此火爆
- Extraction of BaseDAO
猜你喜欢

Understand JDBC in one article

十三、Kotlin进阶学习:内联函数let、also、with、run、apply的用法。

MySQL - 多表查询与案例详解

【OS】操作系统高频面试题英文版(1)

TDengine集群搭建

Nodejs PM2 monitoring and alarm email (2)

MySQL - Multi-table query and case detailed explanation

Mycat2.0 build tutorial
Bypassing the file upload vulnerability

SQL Server database generation and execution of SQL scripts
随机推荐
php vulnerability full solution
Thread state of five
Detailed explanation of regular expression syntax and practical examples
Flink PostgreSQL CDC configuration and FAQ
GraphQL (1) Basic introduction and application examples
MySQL - Function and Constraint Commands
Invalid bound statement (not found)出现的原因和解决方法
SQL Server安装教程
标准输入输出流(System.in,System.out)
【OS】操作系统高频面试题英文版(1)
uni-app: about custom components, easycom specs, uni_modules, etc.
第一个WebAssembly程序
《MySQL高级篇》四、索引的存储结构
The Request request body is repackaged to solve the problem that the request body can only be obtained once
Using PyQt5 to add an interface to YoloV5 (1)
Function functional interface and application
Detailed introduction to the usage of Nacos configuration center
sqli-labs less3/4 Targeting Notes
冒泡排序、选择排序、插入排序、快速排序
sql concat()函数