设计模式之 Factory Mode 工厂模式:Swift 实现

news/2024/10/18 9:21:54/

Factory Mode 工厂模式

Provide the method for creating an instance in the superclass, and allow the subclass to choose the type of the instance.

在父类中提供创建对象的方法,允许子类决定实例化对象的类型。

具备的部分:生产者协议、产品协议,往后就可以根据需要来扩展每一种产品。

请添加图片描述

具体的生产者比如 MongoCakeCreator 的存在是为了实现与产品相关的核心业务逻辑,而不仅仅是创建 MongoCake 实例。工厂方法将核心业务逻辑从具体产品类中分离出来。

// Creator
protocol CakeCreator {func createCake() -> Cakefunc doSomethingForCake(cake: Cake) -> Cake
}// Product
protocol Cake {func doWork()
}// ConcreteCreator
class MongoCakeCreator: CakeCreator {var cake: MongoCake?func createCake() -> Cake {var cake = MongoCake()doSomethingForCake(cake: cake)return cake}func doSomethingForCake(cake: Cake) -> Cake{cake.doWork()cake.doWork()return cake}}// ConcreteCreator
class ChocolateCakeCreator: CakeCreator {func createCake() -> Cake {var cake = ChocolateCake()doSomethingForCake(cake: cake)return cake}func doSomethingForCake(cake: Cake) -> Cake{cake.doWork()return cake}
}class MongoCake: Cake {func doWork() {print("Add some mongo")}
}class ChocolateCake: Cake {func doWork() {print("Add some chocolate")}
}// If we want to add a type of cake call "PinapleCake", just need to 
// make it conform to Cake and add a creator that conform to the CakeCreator for the "PinapleCake"let cakeOne = MongoCakeCreator().createCake()

http://www.ppmy.cn/news/394807.html

相关文章

Setup Factory 点击uninstall.exe Invalid start mode : archive filename

搜了很多资料,琢磨半天,找到一个有效方法,记录下 点击uninstall.exe不生效,主要是因为没带参数 方法:生成一个快捷方式,添加命令 Shell.CreateShortcut("%AppFolder%", "卸载 NetManage.e…

Review and start again——First Period JAVA web Program hotelmanagement:6.some about JAVA factory mode

Today I’d like to discuss something about the JAVA factory mode. JAVA factory mode is the mode which can take the place of the new(**) to instantiate an object. The brilliant interpretation of it I’v ever seen is: https://blog.csdn.net/RivenDong/articl…

Setup Factory 提示 Invalid start mode : archive filename 的解决办法

第一步: 打开在安装之后 创建一个卸载的快捷方式 代码如下: Shell.CreateShortcut("%AppFolder%", "卸载 NetManage.exe", "%AppFolder%\\uninstall.exe", "/U:.\\Uninstall\\uninstall.xml", "%AppFol…

[Camera Drv]Factory mode下camera图像rotate了180度 - MTK物联网在线解答 - 技术论坛

[Camera Drv]Factory mode下 camera图像rotate了180度 在projectconfig.mk中设置了MTK_LCM_PHYSICAL_ROTATION180,则Factorymode下camera旋转180度 最佳答案 FDD2打patch,patchid:ALPS253222, ALPS00259131 GB2打patch,patchid:AL…

telnet连接失败的常见错误

telnet提示“在端口23:连接失败”,说明没有开启telnet服务。 解决方法如下 1、在运用telnet命令时经常显示不是内部或外部命令,或无法打开到主机的连接,在端口23:连接失败,如下图。 2、点击左下角控制面…

Setup Factory 卸载时出现Invalid Start mode :archive filename 无法卸载问题

最近要打包程序在网上找啦几个打包工具最后感觉SetupFactory用着舒服,然后愉快的下载使用,前面都很满意。但是最后安装之后卸载时出现了问题。 执行安装目标下的uninstall.exe后会弹出如下错误 在网上找啦一圈没有发现解决方案。 最后发现生成在开始菜单中卸载快捷方式可以正常…

Arduino ESP8266固件bin升级四种方法

升级过程 1、通过arduino一键生成bin文件 关于生成bin文件配置参数参考 如果没有错误,就会生成一个文件 *.bin见下图(可以重命名您要的文件名称,一般改为注册包同名的文件名方便记忆)或带日期的注册包文件名。 如“20210408-注册…

Android P 连接tbox 后热点无法打开的问题

连接tbox 后,设置中开启热点会报如下错误,导致热点无法开启 解决办法: add CONFIG_NETFILTER_XT_TARGET_CT This config is required for wifi hotspot to start properly This config is used for crosshatch by having it selected as a…