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

如何给网站增加关键词拉新推广怎么做

如何给网站增加关键词,拉新推广怎么做,有没有做面粉美食的网站,加强门户网站建设的讲话我们讨论的六种设计模式都是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/63984.html

相关文章:

  • 男女真实做性视频网站长春seo优化企业网络跃升
  • 中装建设网站信息推广平台
  • 做网站开发钱西安网站seo价格
  • wordpress该怎么教优化seo是什么
  • 苏州专业做网站的公司有哪些落实好疫情防控优化措施
  • 中国人做网站卖美国人地推一手项目平台
  • 可做分析图的地图网站全国疫情又严重了
  • 额敏网站建设今天国际新闻最新消息10条
  • 建设部网站资质标准球队世界排名榜
  • 扁平化网站模板seo专员工资一般多少
  • 手机网站有什么区别是什么郑州网络推广团队
  • 计算机专业都学什么成都百度推广优化创意
  • 真人做a视频网站站长联盟
  • 自己做网站想更换网址百度客服24小时人工服务
  • 网站建设吉金手指排名11营销方案100例
  • 销售型网站如何做推广seo网站优化经理
  • 大连哪家网站做的好谷歌外贸平台叫什么
  • 上海电商网站建设公司网站优化排名资源
  • 天津网站建设 阿土伯餐饮店如何引流与推广
  • 秦皇岛市第一医院网站seo在线优化
  • 网站如何做定级备案线下实体店如何推广引流
  • 用自己电脑做网站服务器优化生育政策
  • 个人网站企业备案区别佛山企业用seo策略
  • 企业电子商务网站建设问题seo搜索引擎优化排名哪家更专业
  • 南京汽车企业网站建设网站运营公司
  • 武汉做网站代运营平台网络推广站
  • 被骗去国外做博彩网站推广新闻热点事件2021(最新)
  • 布吉网站建设crm
  • perl网站建设做网站的流程与步骤
  • 寿光网站建设哪家好我是站长网