美文网首页
Swift的工厂模式

Swift的工厂模式

作者: 蜜蜂6520 | 来源:发表于2017-05-05 13:20 被阅读291次

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

    需求描述

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

    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()

    如果觉得我的文章对您有用,请点击喜欢。您的支持将鼓励我继续创作!

    大家有什么不懂或我哪里写错了都可以评论留言,我一定会回复的~

    相关文章

      网友评论

          本文标题:Swift的工厂模式

          本文链接:https://www.haomeiwen.com/subject/qgnjtxtx.html