Swift的工厂模式

释放双眼,带上耳机,听听看~!

工厂模式的作用是调用者只需传入要求就可获得想要的对象,无需了解创建对象使用了什么类及选择这个类的理由。
抽象工厂类应只包含选择具体工厂的逻辑,具体工厂类应只包含选择实现类的逻辑

需求描述

买家提供要买的车型,我们提供给他满足车型配置的车,买家无需了解车具体配置的细节

Talk is cheap. Show me the code

车辆减震用的悬架

Suspension.swift

 

protocol Suspension {
    var suspensionType:SuspensionOption { get }
}

// 车辆减震用的悬架
enum SuspensionOption {
    case STANDARD
    case SOFT
    case SPORTS
}

class RoadSuspension : Suspension {
    var suspensionType = SuspensionOption.STANDARD
}

class OffRoadSuspension : Suspension {
    var suspensionType = SuspensionOption.SOFT
}

class RaceSuspension : Suspension {
    var suspensionType = SuspensionOption.SPORTS
}

动力传动系统

Drivetrains.swift

 

protocol Drivetrain {
    var driveType:DriveOption { get }
}

// 动力传动系统
enum DriveOption {
    case FRONT
    case REAR
    case ALL
}

class FrontWheelDrive : Drivetrain {
    var driveType = DriveOption.FRONT
}

class RearWheelDrive : Drivetrain {
    var driveType = DriveOption.REAR
}

class AllWheelDrive : Drivetrain {
    var driveType = DriveOption.ALL
}

抽象工厂类

抽象工厂类应只包含选择具体工厂的逻辑
Abstract.swift

 

class CarFactory {
    
    func createSuspension() -> Suspension {
        fatalError("Not implemented");
    }
    
    func createDrivetrain() -> Drivetrain {
        fatalError("Not implemented");
    }
    
    static func getFactory(car:Cars) -> CarFactory? {
        
        var factory:CarFactory?
        switch (car) {
        case .COMPACT:
            factory = CompactCarFactory()
        case .SPORTS:
            factory = SportsCarFactory()
        case .SUV:
            factory = SUVCarFactory()
        }
        
        return factory
    }
    
}

具体工厂类

具体工厂类应只包含选择实现类的逻辑
Concrete.swift

 

class CompactCarFactory : CarFactory {
    
    override func createSuspension() -> Suspension {
        return RoadSuspension();
    }
    
    override func createDrivetrain() -> Drivetrain {
        return FrontWheelDrive();
    }
    
}

class SportsCarFactory : CarFactory {
    
    override func createSuspension() -> Suspension {
        return RaceSuspension();
    }
    
    override func createDrivetrain() -> Drivetrain {
        return RearWheelDrive();
    }
    
}

class SUVCarFactory : CarFactory {
    
    override func createSuspension() -> Suspension {
        return OffRoadSuspension();
    }
    
    override func createDrivetrain() -> Drivetrain {
        return AllWheelDrive();
    }
    
}

提供车辆

CarParts.swift

 

enum Cars {
    case COMPACT
    case SPORTS
    case SUV
}

struct Car {
    
    var carType:Cars;
    var suspension:Suspension;
    var drive:Drivetrain;
    
    init(carType:Cars) {
        
        let concreteFactory = CarFactory.getFactory(car: carType)
        self.suspension = concreteFactory!.createSuspension();
        self.drive = concreteFactory!.createDrivetrain();
        self.carType = carType;
        
    }
    
    func printDetails() {
        print("根据要求成功创建")
    }
    
}

使用

let car = Car(carType: .SPORTS)
car.printDetails()

 

人已赞赏
iOS文章

iOS 实现Loading效果动画

2019-12-23 20:59:06

iOS文章

iOS KVO监听WebView内容高度变化,同步父控件高度

2019-12-24 2:12:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索