抽象工厂(Abstract Factory)属于创建型模式中的一种,提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。
1. 一个系统要独立于它的产品的创建、组合和表示时。
2. 一个系统要由多个产品系列中的一个来配置时。
3. 当你要强调一系列相关的产品对象的设计以便进行联合使用时。
4. 当你提供一个产品类库,而只想显示它们的接口而不是实现时。
1. AbstractFactory:声明一个创建抽象产品对象的操作接口。
2. ConcreteFactory:实现创建具体产品对象的操作。
3. AbstractProduct:为一类产品对象声明一个接口。
4. ConcreteProduct:定义一个将被相应的具体工厂创建的产品对象,实现AbstractProduct接口。
5. Client:仅使用由AbstractFactory和AbstractProduct类声明的接口
a) IAnimalFactory.swift
// // IAnimalFactory.swift // 23设计模式 // // Created by 阳君 on 14/11/27. // Copyright (c) 2014年 六月. All rights reserved. // import Foundation /// IAnimalFactory声明一个创建抽象产品对象的操作接口。 protocol IAnimalFactory { /// 创建ICat func createCat() ->ICat /// 创建IDog func createDog() ->IDog }
a) BlackAnimalFactory.swift
// // BlackAnimalFactory.swift // 23设计模式 // // Created by 阳君 on 14/11/27. // Copyright (c) 2014年 六月. All rights reserved. // import Foundation /// BlackAnimalFactory 实现创建具体产品对象的操作。 class BlackAnimalFactory: IAnimalFactory { func createCat() -> ICat { return BlackCat() } func createDog() ->IDog { return BlackDog() } }
b) WhiteAnimalFactory.swift
// // WhiteAnimalFactory.swift // 23设计模式 // // Created by 阳君 on 14/11/27. // Copyright (c) 2014年 六月. All rights reserved. // import Foundation /// WhiteAnimalFactory 实现创建具体产品对象的操作。 class WhiteAnimalFactory: IAnimalFactory { func createCat() -> ICat { return WhiteCat() } func createDog() ->IDog { return WhiteDog() } }
// // ICat.swift // 23设计模式 // // Created by 阳君 on 14/11/27. // Copyright (c) 2014年 六月. All rights reserved. // import Foundation /// ICat为一类产品对象声明一个接口。 protocol ICat { func eat() }
b) IDog.swift
// // IDog.swift // 23设计模式 // // Created by 阳君 on 14/11/27. // Copyright (c) 2014年 六月. All rights reserved. // import Foundation /// IDog为一类产品对象声明一个接口。 protocol IDog { func eat() }
// // BlackCat.swift // 23设计模式 // // Created by 阳君 on 14/11/27. // Copyright (c) 2014年 六月. All rights reserved. // import Foundation /// BlackCat定义一个将被相应的具体工厂创建的产品对象。实现ICat接口 class BlackCat : ICat{ func eat() { print("The black cat is eating!") } }
b) WhiteCat.swift
// // WhiteCat.swift // 23设计模式 // // Created by 阳君 on 14/11/27. // Copyright (c) 2014年 六月. All rights reserved. // import Foundation /// WhiteCat定义一个将被相应的具体工厂创建的产品对象。实现ICat接口 class WhiteCat : ICat{ func eat() { print("The white cat is eating!") } }
c) BlackDog.swift
// // BlackDog.swift // 23设计模式 // // Created by 阳君 on 14/11/27. // Copyright (c) 2014年 六月. All rights reserved. // import Foundation /// BlackDog定义一个将被相应的具体工厂创建的产品对象。实现IDog接口 class BlackDog : IDog{ func eat() { print("The black dog is eating!") } }
d) WhiteDog.swift
// // WhiteDog.swift // 23设计模式 // // Created by 阳君 on 14/11/27. // Copyright (c) 2014年 六月. All rights reserved. // import Foundation /// WhiteDog定义一个将被相应的具体工厂创建的产品对象。实现IDog接口 class WhiteDog : IDog{ func eat() { print("The white dog is eating!") } }
// 抽象工厂Client:仅使用由AbstractFactory和AbstractProduct类声明的接口 let blackAnimalFactory:IAnimalFactory = BlackAnimalFactory() let whiteAnimalFactory:IAnimalFactory = WhiteAnimalFactory() var cat:ICat = blackAnimalFactory.createCat() cat.eat() cat = whiteAnimalFactory.createCat() cat.eat() var dog:IDog = blackAnimalFactory.createDog() dog.eat() dog = whiteAnimalFactory.createDog() dog.eat()
The black cat is eating!
The white cat is eating!
The black dog is eating!
The white dog is eating!