设计模式iOS原理专题

设计模式之抽象工厂模式(2)

2019-05-20  本文已影响0人  _兜兜转转_

抽象工厂模式

抽象工厂模式(Abstract Factory Pattern)是围绕一个超级工厂创建其他工厂。该超级工厂又称为其他工厂的工厂。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。

在抽象工厂模式中,接口是负责创建一个相关对象的工厂,不需要显式指定它们的类。每个生成的工厂都能按照工厂模式提供对象。

介绍

意图:提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。

主要解决:主要解决接口选择的问题。

何时使用:系统的产品有多于一个的产品族,而系统只消费其中某一族的产品。

如何解决:在一个产品族里面,定义多个产品。

关键代码:在一个工厂里聚合多个同类产品。

应用实例:工作了,为了参加一些聚会,肯定有两套或多套衣服吧,比如说有商务装(成套,一系列具体产品)、时尚装(成套,一系列具体产品),甚至对于一个家庭来说,可能有商务女装、商务男装、时尚女装、时尚男装,这些也都是成套的,即一系列具体产品。假设一种情况(现实中是不存在的,要不然,没法进入共产主义了,但有利于说明抽象工厂模式),在您的家中,某一个衣柜(具体工厂)只能存放某一种这样的衣服(成套,一系列具体产品),每次拿这种成套的衣服时也自然要从这个衣柜中取出了。用 OO 的思想去理解,所有的衣柜(具体工厂)都是衣柜类的(抽象工厂)某一个,而每一件成套的衣服又包括具体的上衣(某一具体产品),裤子(某一具体产品),这些具体的上衣其实也都是上衣(抽象产品),具体的裤子也都是裤子(另一个抽象产品)。

优点:当一个产品族中的多个对象被设计成一起工作时,它能保证客户端始终只使用同一个产品族中的对象。

缺点:产品族扩展非常困难,要增加一个系列的某一产品,既要在抽象的 Creator 里加代码,又要在具体的里面加代码。

使用场景: 1、QQ 换皮肤,一整套一起换。 2、生成不同操作系统的程序。

注意事项:产品族难扩展,产品等级易扩展。

实现

我们将创建 ShapeColor 接口和实现这些接口的实体类。下一步是创建抽象工厂类 AbstractFactory。接着定义工厂类 ShapeFactoryColorFactory,这两个工厂类都是扩展了 AbstractFactory。然后创建一个工厂创造器/生成器类 FactoryProducer

AbstractFactoryPatternDemo,我们的演示类使用 FactoryProducer 来获取 AbstractFactory 对象。它将向 AbstractFactory 传递形状信息 ShapeCIRCLE / RECTANGLE / SQUARE),以便获取它所需对象的类型。同时它还向 AbstractFactory 传递颜色信息 ColorRED / GREEN / BLUE),以便获取它所需对象的类型。

抽象工厂模式的 UML 图
步骤一

创建一个接口

@protocol Shape <NSObject>

- (void)draw;

@end
步骤 2

创建实现接口的实体类。

@interface Square : NSObject <Shape>
- (void)draw;
@end

@interface Circle : NSObject <Shape>
- (void)draw;
@end


@implementation Square

- (void)draw{
    NSLog(@"inside Square draw method ");
}

@end
@implementation Circle
- (void)draw {
    NSLog(@"inside Circle draw method ");
}
@end

步骤 3
为颜色创建一个接口。

@protocol Color <NSObject>

- (void)fill;

@end

步骤4
创建实现接口的实体类。

@protocol Color <NSObject>

- (void)fill;

@end

@interface Red : NSObject <Color>
- (void)fill;
@end

@interface Green : NSObject <Color>
- (void)fill;
@end

@interface Blue : NSObject <Color>
- (void)fill;
@end


@implementation Red
- (void)fill{
    NSLog(@"inside Red fill method ");
}
@end

@implementation Green
- (void)fill{
    NSLog(@"inside Green fill method ");
}
@end


@implementation Blue
- (void)fill{
    NSLog(@"inside Blue fill method ");
}
@end

步骤 5
为 Color 和 Shape 对象创建抽象类来获取工厂。

@interface AbstractFactory :NSObject
- (id)getColor:(NSString *)colorName;
- (id)getShape:(NSString *)shapeName;
@end

步骤 6
创建扩展了 AbstractFactory 的工厂类,基于给定的信息生成实体类的对象。

@interface ColorFactory : AbstractFactory
- (id)getColor:(NSString *)colorName;
- (id)getShape:(NSString *)shapeName;

@end

@interface ShapeFactory : AbstractFactory
- (id)getColor:(NSString *)colorName;
- (id)getShape:(NSString *)shapeName;
@end


@implementation ColorFactory

- (id)getColor:(NSString *)colorName{
    if ([colorName isEqualToString:NSStringFromClass(Red.class)]) {
        return Red.new;
    }else if ([colorName isEqualToString:NSStringFromClass(Green.class)]) {
        return Green.new;
    }else if ([colorName isEqualToString:NSStringFromClass(Blue.class)]) {
        return Blue.new;
    }
    return nil;
}
- (id)getShape:(NSString *)shapeName{
    return nil;
}
@end

@implementation ShapeFactory

- (id)getColor:(NSString *)colorName{
    return nil;
}
- (id)getShape:(NSString *)shapeName{
    if ([shapeName isEqualToString:NSStringFromClass(Square.class)]) {
        return Square.new;
    }else if ([shapeName isEqualToString:NSStringFromClass(Circle.class)]){
        return Circle.new;
    }
    return nil;
}
@end

步骤 7
创建一个工厂创造器/生成器类,通过传递形状或颜色信息来获取工厂。

@interface  FactoryProducer : NSObject
- (id)getFactory:(NSString *)factoryName;

@end



@implementation FactoryProducer

-(id)getFactory:(NSString *)factoryName{
    if ([factoryName isEqualToString:NSStringFromClass(ColorFactory.class)]) {
        return ColorFactory.new;
    }else if ([factoryName isEqualToString:NSStringFromClass(ShapeFactory.class)]){
        return ShapeFactory.new;
    }
    return nil;
}
@end

步骤 8
使用 FactoryProducer 来获取 AbstractFactory,通过传递类型信息来获取实体类的对象。

 //shape获取工厂
    AbstractFactory * shape = [[FactoryProducer new] getFactory:NSStringFromClass(ShapeFactory.class)];
    Square *sq =[shape getShape:NSStringFromClass(Square.class)];
    [sq draw];
    Circle *ci = [shape getShape:NSStringFromClass(Circle.class)];
    //执行draw方法
    [ci draw];
    //获取color工厂
    AbstractFactory * colorF = [[FactoryProducer new] getFactory:NSStringFromClass(ColorFactory.class)];
    Red *red = [colorF getColor:NSStringFromClass(Red.class)];
    [red fill];
    Blue *blue = [colorF getColor:NSStringFromClass(Blue.class)];
    [blue fill];
    Green *green = [colorF getColor:NSStringFromClass(Green.class)];
    [green fill];
    

步骤 9
执行程序,输出结果:

2019-05-20 14:23:00.704964+0800 test[2067:17476100] inside Square draw method
2019-05-20 14:23:00.705115+0800 test[2067:17476100] inside Circle draw method
2019-05-20 14:23:00.705237+0800 test[2067:17476100] inside Red fill method
2019-05-20 14:23:00.705351+0800 test[2067:17476100] inside Blue fill method
2019-05-20 14:23:00.705477+0800 test[2067:17476100] inside Green fill method
其实简单来讲就是抽象工厂在工厂生产产品的功能上又进行了一层封装,把流水线封装成了一层,抽象工厂可以提供流水线,流水线提供产品。
上一篇下一篇

猜你喜欢

热点阅读