当前位置: 首页 > news >正文

6种iOS开发中常用的设计模式

我们讨论的六种设计模式都是iOS开发中常用的模式。下面我将分别解释每种模式的概念、使用场景以及在Objective-C中的实现方式。

1. 单例模式(Singleton Pattern)

意图:确保一个类只有一个实例,并提供一个全局访问点。
使用场景:需要频繁使用且资源消耗大的对象,或者需要全局唯一状态的场景(如配置管理、日志记录、网络管理器等)。
Objective-C实现

// .h文件
@interface MySingleton : NSObject
+ (instancetype)sharedInstance;
@end
// .m文件
@implementation MySingleton
+ (instancetype)sharedInstance {static MySingleton *instance = nil;static dispatch_once_t onceToken;dispatch_once(&onceToken, ^{instance = [[MySingleton alloc] init];});return instance;
}
@end

注意:使用dispatch_once保证线程安全,且防止多次创建。

2. 委托模式(Delegate Pattern)

意图:允许一个对象(委托方)将某些任务交给另一个对象(委托对象)处理。这是一种对象间通信的模式。
使用场景:常见于UIKit框架(如UITableViewDelegate,UITextFieldDelegate),用于事件回调或数据请求。
实现步骤

  1. 定义委托协议(Protocol)。
  2. 在委托方中声明一个弱引用(weak)的delegate属性。
  3. 在需要时向delegate发送消息。
  4. 委托对象实现协议方法。
    示例
// 定义协议
@protocol TaskDelegate <NSObject>
- (void)taskDidComplete;
@end
// 委托方
@interface TaskHandler : NSObject
@property (nonatomic, weak) id<TaskDelegate> delegate;
- (void)startTask;
@end
@implementation TaskHandler
- (void)startTask {// 任务完成后回调[self.delegate taskDidComplete];
}
@end
// 委托对象
@interface ViewController : UIViewController <TaskDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {[super viewDidLoad];TaskHandler *handler = [[TaskHandler alloc] init];handler.delegate = self;
}
- (void)taskDidComplete {NSLog(@"Task completed!");
}
@end

3. 观察者模式(Observer Pattern)

意图:定义对象间的一种一对多的依赖关系,当一个对象状态改变时,所有依赖它的对象都得到通知并自动更新。
使用场景:数据模型与视图之间的同步,跨模块状态通知。
Objective-C实现方式

  • KVO (Key-Value Observing): 内建于NSObject,用于监听属性变化。
  • NSNotificationCenter: 广播机制,任意对象可以发送通知,多个观察者可以监听。
    KVO示例
// 被观察对象
@interface ObservedObject : NSObject
@property (nonatomic, strong) NSString *name;
@end
// 观察者
[self.observedObject addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew context:nil];
// 实现回调
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {if ([keyPath isEqualToString:@"name"]) {NSLog(@"Name changed to: %@", change[NSKeyValueChangeNewKey]);}
}
// 移除观察者(在dealloc中)
- (void)dealloc {[self.observedObject removeObserver:self forKeyPath:@"name"];
}

NSNotificationCenter示例

// 发送通知
[[NSNotificationCenter defaultCenter] postNotificationName:@"DataUpdated" object:nil];
// 监听通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleDataUpdated:) name:@"DataUpdated" object:nil];
// 处理通知
- (void)handleDataUpdated:(NSNotification *)notification {// 更新UI或数据
}
// 移除监听(在dealloc中)
- (void)dealloc {[[NSNotificationCenter defaultCenter] removeObserver:self];
}

4. 工厂方法模式(Factory Method Pattern)

意图:定义一个创建对象的接口,但让子类决定实例化哪一个类。工厂方法使得一个类的实例化延迟到其子类。
使用场景:需要创建一系列相关或依赖对象,但不想指定具体类时。
Objective-C实现

// 抽象产品
@interface Product : NSObject
@end
// 具体产品A
@interface ProductA : Product
@end
// 具体产品B
@interface ProductB : Product
@end
// 工厂基类
@interface Factory : NSObject
+ (Product *)createProduct;
@end
@implementation Factory
+ (Product *)createProduct {// 默认实现,或者抛出异常(因为子类必须重写)[NSException raise:@"Abstract Method" format:@"Subclasses must override"];return nil;
}
@end
// 具体工厂A
@interface FactoryA : Factory
@end
@implementation FactoryA
+ (Product *)createProduct {return [[ProductA alloc] init];
}
@end
// 具体工厂B
@interface FactoryB : Factory
@end
@implementation FactoryB
+ (Product *)createProduct {return [[ProductB alloc] init];
}
@end
// 使用
Product *productA = [FactoryA createProduct];
Product *productB = [FactoryB createProduct];

5. 策略模式(Strategy Pattern)

意图:定义一系列算法,将每个算法封装起来,并使它们可以互相替换。策略模式让算法独立于使用它的客户端而变化。
使用场景:需要动态切换算法或行为(如排序算法、支付方式)。
Objective-C实现

// 策略接口
@protocol PaymentStrategy <NSObject>
- (void)payAmount:(NSInteger)amount;
@end
// 具体策略:信用卡支付
@interface CreditCardPayment : NSObject <PaymentStrategy>
@end
@implementation CreditCardPayment
- (void)payAmount:(NSInteger)amount {NSLog(@"Paid %ld via Credit Card", amount);
}
@end
// 具体策略:支付宝支付
@interface AlipayPayment : NSObject <PaymentStrategy>
@end
@implementation AlipayPayment
- (void)payAmount:(NSInteger)amount {NSLog(@"Paid %ld via Alipay", amount);
}
@end
// 上下文(使用策略)
@interface PaymentContext : NSObject
@property (nonatomic, strong) id<PaymentStrategy> strategy;
- (void)executePayment:(NSInteger)amount;
@end
@implementation PaymentContext
- (void)executePayment:(NSInteger)amount {[self.strategy payAmount:amount];
}
@end
// 使用
PaymentContext *context = [[PaymentContext alloc] init];
context.strategy = [[CreditCardPayment alloc] init];
[context executePayment:100]; // 使用信用卡支付
context.strategy = [[AlipayPayment alloc] init];
[context executePayment:200]; // 使用支付宝支付

6. 组合模式(Composite Pattern)

意图:将对象组合成树形结构以表示“部分-整体”的层次结构。组合模式使得用户对单个对象和组合对象的使用具有一致性。
使用场景:表示树形结构(如UIView层次、菜单树)。
Objective-C实现

// 组件接口
@protocol Component <NSObject>
- (void)operation;
@end
// 叶子节点(无子节点)
@interface Leaf : NSObject <Component>
@end
@implementation Leaf
- (void)operation {NSLog(@"Leaf operation");
}
@end
// 复合节点(可包含子节点)
@interface Composite : NSObject <Component>
@property (nonatomic, strong) NSMutableArray<id<Component>> *children;
- (void)addComponent:(id<Component>)component;
- (void)removeComponent:(id<Component>)component;
@end
@implementation Composite
- (instancetype)init {if (self = [super init]) {_children = [NSMutableArray array];}return self;
}
- (void)addComponent:(id<Component>)component {[self.children addObject:component];
}
- (void)removeComponent:(id<Component>)component {[self.children removeObject:component];
}
- (void)operation {// 先执行自身操作NSLog(@"Composite operation");// 再执行所有子节点的操作for (id<Component> child in self.children) {[child operation];}
}
@end
// 使用
Composite *root = [[Composite alloc] init];
Composite *branch1 = [[Composite alloc] init];
Leaf *leaf1 = [[Leaf alloc] init];
Leaf *leaf2 = [[Leaf alloc] init];
[root addComponent:branch1];
[root addComponent:leaf1];
[branch1 addComponent:leaf2];
[root operation]; // 递归调用所有节点的operation方法

总结

  • 单例模式:全局唯一实例。
  • 委托模式:对象间通信(一对一)。
  • 观察者模式:一对多的状态通知(KVO/Notification)。
  • 工厂方法模式:由子类决定创建的对象。
  • 策略模式:动态切换算法。
  • 组合模式:树形结构的部分-整体处理。
    在iOS开发中,这些模式被广泛应用,理解它们有助于构建可维护、可扩展的代码结构。
http://www.dtcms.com/a/264736.html

相关文章:

  • Qt designer坑-布局内子控件的顺序错乱
  • 量化交易学习之自动化交易策略 [freqtrade 框架学习] ,常见问题避坑指南!!!!
  • <u>#12288;#8203;</u> HTML5全角空格,自动换行,半角用#32;#8203;
  • Spring AI Advisor RAG使用指南
  • Android Auto即将带来变革
  • AI大模型:从编码助手到流程重构者——软件开发新范式解析
  • 【前端】1 小时实现 React 简历项目
  • 多种方法实现golang中实现对http的响应内容生成图片
  • MySQL间隙锁详解:解决幻读的「隐形守护者」
  • React 学习(2)
  • 03-JS资料
  • 企业需要什么样的远程桌面管理软件?
  • 不引入变量 异或交换的缺点
  • 替代进口SCA7606【智芯微】国产高精度电流传感器 工业新能源电网专用
  • openai-agents记忆持久化(neo4j)
  • WPF学习笔记(21)ListBox、ListView与控件模板
  • 深入理解 LoRA:大语言模型微调的低秩魔法
  • PyTorch 不支持旧GPU的异常状态与解决方案:CUDNN_STATUS_NOT_SUPPORTED_ARCH_MISMATCH
  • Spring Boot 高并发框架实现方案:数字城市的奇妙之旅
  • 智能物流革命:Spring Boot+AI实现最优配送路径规划
  • Knife4j+Axios+Redis:前后端分离架构下的 API 管理与会话方案
  • 【Java关系映射入门】实战一
  • gin如何返回html
  • Java面试宝典:集合一
  • 生僻字写入oracle后被转为??
  • (一)大语言模型的关键技术<-AI大模型构建
  • 在浏览器输入url,会发送什么事情?
  • HMAC 介绍
  • 在 VMware虚拟机中使用 NAT 网络模式
  • Git 怎么判断是否冲突?