惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

罗磊的独立博客
I
InfoQ
雷峰网
雷峰网
Hugging Face - Blog
Hugging Face - Blog
IT之家
IT之家
云风的 BLOG
云风的 BLOG
有赞技术团队
有赞技术团队
Martin Fowler
Martin Fowler
MyScale Blog
MyScale Blog
The GitHub Blog
The GitHub Blog
博客园_首页
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
G
Google Developers Blog
WordPress大学
WordPress大学
B
Blog
人人都是产品经理
人人都是产品经理
小众软件
小众软件
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
量子位
Apple Machine Learning Research
Apple Machine Learning Research
N
Netflix TechBlog - Medium
Last Week in AI
Last Week in AI
博客园 - 聂微东
Jina AI
Jina AI

博客园 - 裸奔的小鸟

Screen Pen FAQ StoreAssets FAQ Heart Beat FAQ Learn Chinese FAQ 天天记单词 FAQ Swift 面向协议编程的一点见解 Swift 项目中添加main方法 Swift 模块化框架的实现以及模块之间的解藕 ScriptToApp FAQ Eye Protection FAQ Live Score FAQ Quick Search FAQ iPhone Screen FAQ Quick Launcher FAQ Zoom Me FAQ Show Desktop Pro FAQ The Startup Manager FAQ Finder Quick Menu FAQ The App Locker FAQ 【转载】在block中使用weakSelf/strongSelf
Swift 简单工厂模式、工厂方法模式、抽象工厂模式解析
裸奔的小鸟 · 2020-03-31 · via 博客园 - 裸奔的小鸟

1. 简单工厂模式

  • 一个工厂类
  • 内部用 switch case 创建不同对象
 1 import UIKit
 2 
 3 protocol Service {
 4     var url: URL { get }
 5 }
 6 
 7 // dev阶段
 8 class StagingService: Service {
 9     var url: URL { return URL(string: "https://dev.localhost/")! }
10 }
11 
12 // product阶段
13 class ProductionService: Service {
14     var url: URL { return URL(string: "https://live.localhost/")! }
15 }
16 
17 class EnvironmentFactory {
18     enum Environment {
19         case staging
20         case production
21     }
22     
23     func create(_ environment:Environment) -> Service{
24         switch environment {
25         case .staging:
26             return StagingService()
27         case .production:
28             return ProductionService()
29         }
30     }
31 }
32 
33 let env = EnvironmentFactory()
34 let serv = env.create(.production)
35 print(serv.url) //https://live.localhost/

2. 工厂方法模式

  • 多个 (解耦的) 工厂类
  • 每个工厂方法创建一个实例
 1 import UIKit
 2 
 3 protocol Service {
 4     var url: URL { get }
 5 }
 6 
 7 protocol ServiceFactory{
 8     func create() -> Service
 9 }
10 
11 // dev阶段
12 class StagingService: Service {
13     var url: URL { return URL(string: "https://dev.localhost/")! }
14 }
15 
16 // product阶段
17 class ProductionService: Service {
18     var url: URL { return URL(string: "https://live.localhost/")! }
19 }
20 
21 // dev 工厂类
22 class StagingServiceFactory: ServiceFactory{
23     func create() -> Service {
24         return StagingService()
25     }
26 }
27 
28 // production 工厂类
29 class ProductionServiceFactory: ServiceFactory{
30     func create() -> Service {
31         return ProductionService()
32     }
33 }
34 
35 let serv = ProductionServiceFactory().create()
36 print(serv.url)

3. 抽象工厂模式

  • 通过工厂方法组合简单工厂
 1 import UIKit
 2 
 3 // 服务协议
 4 protocol ServiceFactory {
 5     // 创建一个服务
 6     func create() -> Service
 7 }
 8 
 9 protocol Service {
10     var url: URL { get }
11 }
12 
13 // dev阶段
14 class StagingService: Service {
15     var url: URL { return URL(string: "https://dev.localhost/")! }
16 }
17 
18 class StagingServiceFactory: ServiceFactory {
19     // dev工厂就是创建一个dev的服务
20     func create() -> Service {
21         return StagingService()
22     }
23 }
24 
25 // product阶段
26 class ProductionService: Service {
27     var url: URL { return URL(string: "https://live.localhost/")! }
28 }
29 
30 class ProductionServiceFactory: ServiceFactory {
31     // product工厂就是创建一个product的服务
32     func create() -> Service {
33         return ProductionService()
34     }
35 }
36 
37 // 抽象工厂
38 class AppServiceFactory: ServiceFactory {
39 
40     enum Environment {
41         case production
42         case staging
43     }
44     
45     var env: Environment
46 
47     init(env: Environment) {
48         self.env = env
49     }
50 
51     func create() -> Service {
52         switch self.env {
53         case .production:
54             return ProductionServiceFactory().create()
55         case .staging:
56             return StagingServiceFactory().create()
57         }
58     }
59 }
60 
61 let factory = AppServiceFactory(env: .production)
62 let service = factory.create()
63 print(service.url)