码迷,mamicode.com
首页 > 编程语言 > 详细

swift - Abstract Factory Pattern

时间:2015-08-10 00:19:26      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:swift

It allows a calling component to obtain a family or group of related objects without needing to know which classes were used to create them.

每一层的创建都是工厂,并且客户端和中间层是不知道具体零件的类型的。

技术分享

client:

let car = Car(carType: Cars.SPORTS);

car.printDetails();


design pattern:

enum Cars: String {

    case COMPACT = "VW Golf";

    case SPORTS = "Porsche Boxter";

    case SUV = "Cadillac Escalade";

}


struct Car {

    var carType:Cars;

    var floor:Floorplan;

    var suspension:Suspension;

    var drive:Drivetrain;

    

    init(carType:Cars) {

        let concreteFactory = CarFactory.getFactory(carType);

        self.floor = concreteFactory!.createFloorplan();

        self.suspension = concreteFactory!.createSuspension();

        self.drive = concreteFactory!.createDrivetrain();

        self.carType = carType;

    }

    

    func printDetails() {

        println("Car type: \(carType.rawValue)");

        println("Seats: \(floor.seats)");

        println("Engine: \(floor.enginePosition.rawValue)");

        println("Suspension: \(suspension.suspensionType.rawValue)");

        println("Drive: \(drive.driveType.rawValue)");

    }

}


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

class CarFactory {

    

    required init() {

        // do nothing

    }

    

    func createFloorplan() -> Floorplan {

        fatalError("Not implemented");

    }

    

    func createSuspension() -> Suspension {

        fatalError("Not implemented");

    }

    

    func createDrivetrain() -> Drivetrain {

        fatalError("Not implemented");

    }

    

    final class func getFactory(car:Cars) -> CarFactory? {

        var factoryType:CarFactory.Type;

        switch (car) {

        case .COMPACT:

            factoryType = CompactCarFactory.self;

        case .SPORTS:

            factoryType = SportsCarFactory.self;

        case .SUV:

            factoryType = SUVCarFactory.self;

        }

        var factory = factoryType.sharedInstance;

        if (factory == nil) {

            factory = factoryType();

        }

        return factory;

    }

    

    class var sharedInstance:CarFactory? {

        get {

            return nil;

        }

    }

}



class CompactCarFactory : CarFactory {

    override func createFloorplan() -> Floorplan {

        return StandardFloorplan();

    }

    override func createSuspension() -> Suspension {

        return RoadSuspension.getInstance();

    }

    override func createDrivetrain() -> Drivetrain {

        return FrontWheelDrive();

    }

}



//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


protocol Floorplan {

    var seats:Int { get }

    var enginePosition:EngineOption { get };

}


enum EngineOption : String {

    case FRONT = "Front"; case MID = "Mid";

}


class ShortFloorplan: Floorplan {

    var seats = 2;

    var enginePosition = EngineOption.MID

}




swift - Abstract Factory Pattern

标签:swift

原文地址:http://blog.csdn.net/crazyeveryday/article/details/47381679

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!