美文网首页
设计模式

设计模式

作者: 夜雨聲煩_ | 来源:发表于2023-07-13 15:52 被阅读0次

工厂模式

链接:https://www.jianshu.com/p/9cbb18774194

简单工厂模式

简单工厂模式并不是常用的设计模式之一,它只算是工厂模式的特殊实现。可以根据不同的参数来实例化不同的对象,这些对象通常有共同的父类或者接口。

适用场景:
需要创建的对象较少。
客户端不关心对象创建的过程。

  1. 创建图像父类Shape
@interface Shape : NSObject

- (void)draw;

@end

2.创建继承此父类的具体图形类,分别重写父类中的draw方法,这里分别创建了圆形、长方形、正方形

#import "Circle.h"
// 圆形类
@implementation Circle

- (void)draw {
    NSLog(@"圆形");
}

@end

#import "Rectangle.h"
// 长方形类
@implementation Rectangle

- (void)draw {
    NSLog(@"长方形");
}

@end

#import "Square.h"
// 正方形类
@implementation Square

- (void)draw {
    NSLog(@"正方形");
}

@end
  1. 创建工厂类,用来获取实例
@implementation ShapeFactory

+ (Shape *)getShape:(ShapeFactoryType)type {
    if (type == ShapeFactoryTypeCircle) {
        return [[Circle alloc] init];
    } else if (type == ShapeFactoryTypeRectangle) {
        return [[Rectangle alloc] init];
    } else if (type == ShapeFactoryTypeSquare) {
        return [[Square alloc] init];
    }
    
    return [[Shape alloc] init];
}

@end
  1. 测试方法
// 简单工厂模式
Shape *shape = [ShapeFactory getShape:ShapeFactoryTypeCircle];
//    Shape *shape = [ShapeFactory getShape:ShapeFactoryTypeRectangle];
//    Shape *shape = [ShapeFactory getShape:ShapeFactoryTypeSquare];
[shape draw];
工厂模式

工厂模式是简单工厂模式的进一步深化,在工厂模式中我们将不再提供一个统一的工厂类去创建对象,而是针对不同的对象提供不同的工厂。将创建具体产品类的工作延迟到子类去完成

适用场景:
对象创建过程复杂或对象创建要满足某些条件,使用者不容易掌握,并且也不需要知道具体细节。
对象的创建和使用要进行解耦。

示例:
现在需要设计一个这样的图片加载类,它具有多个图片加载器,用来加载jpg,png,gif格式的图片,每个加载器都有一个load()方法,用于读取图片。

  1. 创建抽象产品了类,ImageLoader
@interface ImageLoader : NSObject
 
- (void)loadImage;
 
@end
  1. 创建具体的产品类,PNG加载器、GIF加载器、JPG加载器,并复写父类的loadImage方法。
#import "PNGImageLoader.h"

@implementation PNGImageLoader

- (void)loadImage {
    NSLog(@"加载PNG格式图片");
}

@end

#import "GIFImageLoader.h"

@implementation GIFImageLoader

- (void)loadImage {
    NSLog(@"加载GIF格式图片");
}

@end

#import "JPGImageLoader.h"

@implementation JPGImageLoader

- (void)loadImage {
    NSLog(@"加载JPG格式图片");
}

@end
  1. 创建工厂抽象类ImageFactory
@interface ImageFactory : NSObject

- (ImageLoader *)getImageLoader;

@end
  1. 创建工厂具体类
#import "PNGImageFactory.h"
#import "PNGImageLoader.h"

@implementation PNGImageFactory

- (ImageLoader *)getImageLoader {
    return [PNGImageLoader new];
}

@end

#import "GIFImageFactory.h"
#import "GIFImageLoader.h"

@implementation GIFImageFactory

- (ImageLoader *)getImageLoader {
    return [GIFImageLoader new];
}

@end

#import "JPGImageFactory.h"
#import "JPGImageLoader.h"

@implementation JPGImageFactory

- (ImageLoader *)getImageLoader {
    return [JPGImageLoader new];
}

@end
  1. 测试方法,只需要知道具体的工厂类即可使用
// 工厂模式
//    ImageFactory *fac = [[PNGImageFactory alloc] init];
//    ImageFactory *fac = [[GIFImageFactory alloc] init];
ImageFactory *fac = [[JPGImageFactory alloc] init];
ImageLoader *imageoadler =[fac getImageLoader];
[imageoadler loadImage];
抽象工厂模式

抽象工厂模式是工厂模式的进一步延伸,提供了创建了一系列相关或相互依赖对象的接口。在抽象工厂模式中,每个具体的工厂都提供了多个工厂方法(>=2)用来创建不同类型的具体对象。

适用场景:
和工厂模式一样客户端无需知道创建对象的类,只需要知道具体工厂类。
需要一组对象共同完成某种功能时,并且可能存在多组对象完成不同功能的情况。
系统结构稳定,不会频繁的增加对象。(增加新的产品族符合开闭原则,但增加新的产品等级结构,需要修改所有的工厂角色,包括抽象工厂类,违背了开闭原则)

某软件公司欲推出一款新的手机游戏软件,该软件能够支持iOS、Android和Windows Mobile等多个智能手机操作系统平台,针对不同的手机操作系统,该游戏软件提供了不同的游戏操作控制(OperationController)类和游戏界面控制(InterfaceController)类,并提供相应的工厂类来封装这些类的初始化过程。软件要求具有较好的扩展性以支持新的操作系统平台,为了满足上述需求,试采用抽象工厂模式对其进行设计。

  1. 创建抽象产品类OperationController和InterfaceController
@interface OperationController : NSObject

- (void)control;

@end

@interface InterfaceController : NSObject

- (void)display;

@end
  1. 创建具体产品类,每个系统的操作控制和界面控制,并重写父类方法
// 操作控制器
#import "WindowsOperationController.h"

@implementation WindowsOperationController

- (void)control {
    NSLog(@"windows 控制器");
}

@end

#import "iOSOperationController.h"

@implementation iOSOperationController

- (void)control {
    NSLog(@"iOS控制器");
}

@end

#import "AndroidOperationController.h"

@implementation AndroidOperationController

- (void)control {
    NSLog(@"Android控制器");
}

@end

// 界面控制器
#import "WindowsInterfaceController.h"

@implementation WindowsInterfaceController

- (void)display {
    NSLog(@"渲染Windows界面");
}

@end

#import "iOSInterfaceController.h"

@implementation iOSInterfaceController

- (void)display {
    NSLog(@"渲染iOS界面");
}

@end

#import "AndroidInterfaceController.h"

@implementation AndroidInterfaceController

- (void)display {
    NSLog(@"渲染Android界面");
}

@end
  1. 创建抽象工厂类
@interface SystemFactory : NSObject

- (OperationController *)getOperation;

- (InterfaceController *)getUI;

@end
  1. 创建具体工厂类
#import "WindowsFactory.h"
#import "WindowsInterfaceController.h"
#import "WindowsOperationController.h"

@implementation WindowsFactory

- (OperationController *)getOperation {
    return [WindowsOperationController new];
}

- (InterfaceController *)getUI {
    return [WindowsInterfaceController new];
}

@end

#import "iOSFactory.h"
#import "iOSInterfaceController.h"
#import "iOSOperationController.h"

@implementation iOSFactory

- (OperationController *)getOperation {
    return [iOSOperationController new];
}

- (InterfaceController *)getUI {
    return [iOSInterfaceController new];
}

@end

#import "AndroidFactory.h"
#import "AndroidInterfaceController.h"
#import "AndroidOperationController.h"

@implementation AndroidFactory

- (OperationController *)getOperation {
    return [AndroidOperationController new];
}

- (InterfaceController *)getUI {
    return [AndroidInterfaceController new];
}

@end
  1. 测试方法
//    SystemFactory *system = [[iOSFactory alloc] init];
//    SystemFactory *system = [[AndroidFactory alloc] init];
SystemFactory *system = [[WindowsFactory alloc] init];
InterfaceController *ui = [system getUI];
OperationController *opera = [system getOperation];
[opera control];
[ui display];

相关文章

  • 设计模式

    常用的设计模式有,单例设计模式、观察者设计模式、工厂设计模式、装饰设计模式、代理设计模式,模板设计模式等等。 单例...

  • 设计模式笔记汇总

    目录 设计原则 “依赖倒置”原则 未完待续... 设计模式 设计模式——策略模式 设计模式——装饰者模式 设计模式...

  • 设计模式

    《C#设计模式》 《C#设计模式》-设计模式概述 《C#设计模式》-面向对象设计原则 《C#设计模式》-单例模式 ...

  • 浅谈JS的一些设计模式

    @(书籍阅读)[JavaScript, 设计模式] 常见设计模式 设计模式简介 设计模式概念解读 设计模式的发展与...

  • 前端设计模式

    JS设计模式一:工厂模式jS设计模式二:单例模式JS设计模式三:模块模式JS设计模式四:代理模式JS设计模式五:职...

  • 设计模式之工厂模式

    设计模式之工厂模式 标签(空格分隔): 设计模式 工厂模式 设计模式的感念 设计模式的应用 工厂设计模式的产生 工...

  • JavaJavascript基础进阶(十七)JS中常用的设计模式

    单利设计模式、构造原型设计模式、发布订阅设计模式、promise设计模式 单利模式 构造原型设计模式 最贴近OOP...

  • 设计模式 - 目录

    设计模式01 - 单例模式 设计模式02 - 工厂模式 设计模式03 - 建造者模式 设计模式04 - 适配器模式...

  • 第1章 设计模式概述

    一、设计模式的概念 二、设计模式的历史 三、设计模式的要素 四、设计模式的分类 ■ 创建型设计模式 ■ 结构型设计...

  • iOS设计模式(3)适配器模式

    设计模式系列文章 《iOS设计模式(1)简单工厂模式》《iOS设计模式(2)工厂模式》《iOS设计模式(4)抽象工...

网友评论

      本文标题:设计模式

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