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

如何给网站增加关键词铜川网站seo

如何给网站增加关键词,铜川网站seo,别样网图片素材网站,北京南站附近景点我们讨论的六种设计模式都是iOS开发中常用的模式。下面我将分别解释每种模式的概念、使用场景以及在Objective-C中的实现方式。 1. 单例模式(Singleton Pattern) 意图:确保一个类只有一个实例,并提供一个全局访问点。 使用场景&…

我们讨论的六种设计模式都是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/wzjs/297652.html

相关文章:

  • 专门做二手手机的网站吗网络服务有限公司
  • 重庆 机械有限公司 沙坪坝网站建设沈阳今日新闻头条
  • 兰州响应式网站建设简述如何优化网站的方法
  • 个人动漫网站怎么做页面windows优化大师好不好
  • 珠海网站设计网络优化个人怎么做网站
  • 日本做任务赚钱的网站有哪些南宁百度seo软件
  • 企业网站建设对网络营销的影响网上营销是做什么的
  • 网站建设哪家好 上海网络推广是什么工作
  • 关于建设网站的图片seo网站优化系统
  • 彩票网站开发制作全网整合营销推广系统
  • iis发布php网站免费发布信息
  • 大连学网站制作软件开发培训班
  • dw做网站的实用特效企业培训课程体系
  • 网站建设和优全媒体运营师报名入口
  • 做网站是否要备案三亚网络推广
  • 局域网内建网站百度首页百度
  • 电商大数据平台建设方案seo需要什么技术
  • 个人做企业 网站seo前线
  • 女性pose拍照seo文章是什么
  • 阿里云最低服务器可以做几个网站seo优化网站模板
  • 网站没备案怎么做广告联盟百度官方客服平台
  • 徐州企业做网站网站建网站建设网站
  • 继续网站建设百度霸屏推广一般多少钱
  • 贵州做团队培训的网站指数函数和对数函数
  • magento 做的最牛逼的中文网站新手如何涨1000粉
  • 襄阳电商网站建设手机管家一键优化
  • 东莞做网站找微客巴巴aso优化{ }贴吧
  • 江西南昌网站开发网络推广的基本方法
  • 门户做网站搜索引擎优化策略不包括
  • 免费b站不收费企业推广方法