SOLID 设计原则
SOLID 设计原则鼓励我们创建更易于维护、更易于理解和更灵活的软件。
- 单一职责原则(Single Responsibility Principle)
实体应承担一项责任。
- 开闭原则(Open-Closed Principle)
实体应该对 extension 开放但对修改(modification) 关闭。
- 里氏替换原则(Liskov Substitution Principle)
如果类 A 是类 B 的子类型,那么我们应该能够用 A 替换 B 而不会破坏程序的行为。
- 接口隔离(Interface Segregation)
实体不应使用他们不会完全使用的接口。大接口应该拆分成更小的接口。
- 依赖倒置(Dependency Inversion)
高级模块不应该依赖于低级模块,两者都应该依赖于抽象。
下面我们来看代码示例:
// 一、单一职责原则(Single Responsibility Principle)
class User {
var name: String
var profileName: String
init(name: String, profileName: String) {
self.name = name
self.profileName = profileName
}
func changeName(to name: String) {
self.name = name
}
func changeProfileName(to profileName: String) {
self.profileName = profileName
}
}
struct Account {
var user: User
init(user: User) {
self.user = user
}
func createAccount() {
print("account created")
}
func loginAccount() {
print("logged into account")
}
func logoutAccount() {
print("logged out of account")
}
}
//二、开闭原则
class Guitar {
var brandName: String
var model: String
init(brandName: String, model: String) {
self.brandName = brandName
self.model = model
}
}
class FlameGuitar: Guitar {
var flame: String = "Red"
}
protocol Instrument {
var brandName: String { get set }
var model: String { get set }
}
class BassGuitar: Instrument {
var brandName: String
var model: String
init(brandName: String, model: String) {
self.brandName = brandName
self.model = model
}
}
class WaterGuitar: Instrument {
var brandName: String
var model: String
var waterVolume: Int
init(brandName: String, model: String, waterVolume: Int) {
self.brandName = brandName
self.model = model
self.waterVolume = waterVolume
}
}
// 三、里氏替换原则(Liskov Substitution Principle)
protocol Polygon {
var area: Float { get }
}
class Rectangle: Polygon {
var width: Float = 0
var length: Float = 0
init(length: Float, width: Float) {
self.length = length
self.width = width
}
var area: Float {
return width * length
}
}
class Square: Polygon {
var side: Float = 0
init(side: Float) {
self.side = side
}
var area: Float {
return side * side
}
}
func printArea(ofPolygon polygon: Polygon) {
print(polygon.area)
}
let rectangle = Rectangle(length: 10, width: 2)
printArea(ofPolygon: rectangle)
let square = Square(side: 2)
printArea(ofPolygon: square)
// 四、接口隔离(Interface Segregation)
protocol Vehicle {
func accelerate()
}
protocol Engine {
func turnEngineOn()
}
class MotorCar: Vehicle, Engine {
func turnEngineOn() {
print("engine on!")
}
func accelerate() {
print("going faster")
}
}
class HorseDrawnCarriage: Vehicle {
func accelerate() {
print("going faster")
}
}
// 五、依赖倒置(Dependency Inversion)
protocol CloudService {
func fetchData(url: String) -> [String]
}
struct FirebaseService: CloudService {
func observeSingleEvent(forRef: String) -> [String] {
return ["Post"]
}
func fetchData(url: String) -> [String] {
return observeSingleEvent(forRef: url)
}
}
struct ParseService: CloudService {
func fetchData(url: String) -> [String] {
return ["Post"]
}
}
class UserFeed {
private let data: CloudService
init(data: CloudService) {
self.data = data
}
func getUserFeedData(url: String) {
data.fetchData(url: url)
}
}
let parseService = ParseService()
let userFeedService = UserFeed(data: parseService)
userFeedService.getUserFeedData(url: "url")
网友评论