Last active
October 1, 2017 14:26
-
-
Save alobanov/08ba60188dc77171c4484caf9f963656 to your computer and use it in GitHub Desktop.
π Π€Π°Π±ΡΠΈΡΠ½ΡΠΉ ΠΌΠ΅ΡΠΎΠ΄ β ΡΡΠΎ ΠΏΠΎΡΠΎΠΆΠ΄Π°ΡΡΠΈΠΉ ΠΏΠ°ΡΡΠ΅ΡΠ½ ΠΏΡΠΎΠ΅ΠΊΡΠΈΡΠΎΠ²Π°Π½ΠΈΡ, ΠΊΠΎΡΠΎΡΡΠΉ ΠΎΠΏΡΠ΅Π΄Π΅Π»ΡΠ΅Ρ ΠΎΠ±ΡΠΈΠΉ ΠΈΠ½ΡΠ΅ΡΡΠ΅ΠΉΡ Π΄Π»Ρ ΡΠΎΠ·Π΄Π°Π½ΠΈΡ ΠΎΠ±ΡΠ΅ΠΊΡΠΎΠ² Π² ΡΡΠΏΠ΅ΡΠΊΠ»Π°ΡΡΠ΅, ΠΏΠΎΠ·Π²ΠΎΠ»ΡΡ ΠΏΠΎΠ΄ΠΊΠ»Π°ΡΡΠ°ΠΌ ΠΈΠ·ΠΌΠ΅Π½ΡΡΡ ΡΠΈΠΏ ΡΠΎΠ·Π΄Π°Π²Π°Π΅ΠΌΡΡ
ΠΎΠ±ΡΠ΅ΠΊΡΠΎΠ².
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: Playground - noun: a place where people can play | |
import UIKit | |
// Models | |
struct UniverseType { | |
static let marvel = MarvelHero.self | |
static let dc = DCHero.self | |
} | |
protocol Hero { | |
var name: String {get} | |
init(rawHeroId: Int) | |
} | |
class UnknownHero: Hero { | |
var name: String | |
required init(rawHeroId: Int) { | |
self.name = "Kick Ass" | |
} | |
} | |
class DCHero: Hero { | |
enum DCHeroType: Int { | |
case batman = 0, superman, flash | |
var name: String { | |
switch self { | |
case .batman: return "batman" | |
case .superman: return "superman" | |
case .flash: return "flash" | |
} | |
} | |
static let count = 2 | |
} | |
var name: String | |
required init(rawHeroId: Int) { | |
if let hero = DCHeroType(rawValue: rawHeroId) { | |
self.name = hero.name | |
} else { | |
self.name = "ΠΠ΅Π»Π΅Π½ΡΠΉ ΡΠ°Π½Π°ΡΡ" | |
} | |
} | |
} | |
class MarvelHero: Hero { | |
enum MarvelHeroType: Int { | |
case ironman = 0, capamerica, spiderman | |
var name: String { | |
switch self { | |
case .ironman: return "ironman" | |
case .capamerica: return "capamerica" | |
case .spiderman: return "spiderman" | |
} | |
} | |
static let count = 2 | |
} | |
var name: String | |
required init(rawHeroId: Int) { | |
if let hero = MarvelHeroType(rawValue: rawHeroId) { | |
self.name = hero.name | |
} else { | |
self.name = "Π‘ΠΎΠΊΠΎΠ»ΠΈΠ½ΡΠΉ Π³Π»Π°Π·" | |
} | |
} | |
} | |
// | |
// Π€Π°Π±ΡΠΈΠΊΠ° | |
// | |
protocol HeroFactoryProtocol { | |
func createHero() -> Hero | |
} | |
extension HeroFactoryProtocol { | |
func createHero() -> Hero { | |
return UnknownHero(rawHeroId: 0) | |
} | |
func random(max: Int) -> Int { | |
return Int(arc4random_uniform(UInt32(1 + max - 0))) + 0 | |
} | |
} | |
class HeroFactory<H: Hero>: HeroFactoryProtocol { | |
func createHero() -> Hero { | |
return H(rawHeroId: random(max: 2)) | |
} | |
} | |
class FactoryDemo { | |
private(set) var heroFactory: HeroFactoryProtocol! | |
func configure<T: Hero>(type: T.Type) { | |
heroFactory = HeroFactory<T>() | |
} | |
} | |
let demo = FactoryDemo() | |
demo.configure(type: UniverseType.marvel) | |
print(demo.heroFactory.createHero().name) | |
print(demo.heroFactory.createHero().name) | |
print(demo.heroFactory.createHero().name) | |
// ΠΠ±ΡΡΡΠ°ΠΊΡΠ½Π°Ρ ΡΠ°Π±ΡΠΈΠΊΠ° | |
protocol AbstractHeroFactoryProtocol { | |
func hero() -> Hero | |
} | |
class AbstractHeroFactory: AbstractHeroFactoryProtocol { | |
private var factory: HeroFactoryProtocol | |
init(factory: HeroFactoryProtocol) { | |
self.factory = factory | |
} | |
func hero() -> Hero { | |
return factory.createHero() | |
} | |
} | |
// Demo | |
let dcFactory: HeroFactoryProtocol = HeroFactory<DCHero>() | |
let afDemo: AbstractHeroFactoryProtocol = AbstractHeroFactory(factory: dcFactory) | |
print(afDemo.hero().name) | |
print(afDemo.hero().name) | |
print(afDemo.hero().name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment