Это старая версия документа!
Порождающие паттерны - это паттерны, которые упрощают и абстрагируют создание информационных объектов. На которых базируется все ООП.
import Foundation Factoty enum ClothesType { case head case shoes } protocol Clothes { var title: String { get } var type: ClothesType { get } var color: String { get } func putOn() } class Hat: Clothes { var title: String = «Шляпа» var type: ClothesType = .head var color: String = «черная» func putOn() { print(«Одета \(color) \(title)») } } class Shoes:Clothes { var title: String = «Ботинки» var type: ClothesType = .shoes var color: String = «белые» func putOn() { print(«Одеты \(color) \(title)») } } class ClothesFactory { static let shared = ClothesFactory() private init() { } заприватим инициаизатор. чтобы не давало создавать объекты этого класса
func createClothes(type:ClothesType ) → Clothes {
switch type {
case .head: return Hat()
case .shoes:return Shoes()
default:
fatalError("Unsupported")
}
} }
Тестирование паттерна let hat = ClothesFactory.shared.createClothes(type: .head) let shoes = ClothesFactory.shared.createClothes(type: .shoes) let hat2 = ClothesFactory.shared.createClothes(type: .head) var clothes: [Clothes] = [hat,shoes,hat2] for clothes1 in clothes { clothes1.putOn() }