1.

有一道iOS面试题,iOS中都有什么设计模式?很少有答案说包括adapter。

gof 书中adapter模式有以下内容:

实现:

。。。

b ) 使 用 代 理 对 象 在这种方法中, T r e e D i s p l a y 将访问树结构的请求转发到代理对象。 T r e e D i s p l a y 的 客 户 进 行 一 些 选 择 , 并 将 这 些 选 择 提 供 给 代理 对 象 , 这 样 客 户 就 可 以 对 适 配 加 以控制,如下图所示。

例 如 , 有 一 个 D i r e c t o r y B r o w s e r , 它 像 前 面 一 样 使 用 T r e e D i s p l a y 。 D i r e c t o r y B r o w s e r 可能 为匹配 T r e e D i s p l a y 和 层 次 目 录 结 构 构 造 出 一 个 较 好 的 代 理 。 在 S m a l l t a l k 或 O b j e c t i v e C 这样的 动态类型语言中,该方法只需要一个接口对适配器注册代理即可。然后 T r e e D i s p l a y 简单地将 请求转发给代理对象。 N E X T S T E P [ A d d 9 4 ] 大 量 使 用 这 种 方 法 以 减 少 子 类 化 。

后来instagram开源了IGListKit,https://github.com/Instagram/IGListKit。就是Adapter模式。

@interface IGListAdapter : NSObject

/**
The view controller that houses the adapter.
*/
@property (nonatomic, nullable, weak) UIViewController *viewController; /**
The collection view used with the adapter.
*/
@property (nonatomic, nullable, weak) UICollectionView *collectionView; /**
The object that acts as the data source for the adapter.
*/
@property (nonatomic, nullable, weak) id <IGListAdapterDataSource> dataSource; /**
The object that receives top-level events for section controllers.
*/
@property (nonatomic, nullable, weak) id <IGListAdapterDelegate> delegate; /**
The object that receives `UICollectionViewDelegate` events. @note This object *will not* receive `UIScrollViewDelegate` events. Instead use scrollViewDelegate.
*/
@property (nonatomic, nullable, weak) id <UICollectionViewDelegate> collectionViewDelegate; /**
The object that receives `UIScrollViewDelegate` events.
*/
@property (nonatomic, nullable, weak) id <UIScrollViewDelegate> scrollViewDelegate; /**
The updater for the adapter.
*/
@property (nonatomic, strong, readonly) id <IGListUpdatingDelegate> updater; /**
A bitmask of experiments to conduct on the adapter.
*/
@property (nonatomic, assign) IGListExperiment experiments; /**
Initializes a new `IGListAdapter` object. @param updater An object that manages updates to the collection view.
@param viewController The view controller that will house the adapter.
@param workingRangeSize The number of objects before and after the viewport to consider within the working range. @return A new list adapter object. @note The working range is the number of objects beyond the visible objects (plus and minus) that should be
notified when they are close to being visible. For instance, if you have 3 objects on screen and a working range of 2,
the previous and succeeding 2 objects will be notified that they are within the working range. As you scroll the list
the range is updated as objects enter and exit the working range. To opt out of using the working range, use `initWithUpdater:viewController:` or provide a working range of `0`.
*/
- (instancetype)initWithUpdater:(id <IGListUpdatingDelegate>)updater
viewController:(nullable UIViewController *)viewController
workingRangeSize:(NSInteger)workingRangeSize NS_DESIGNATED_INITIALIZER; /**
Initializes a new `IGListAdapter` object with a working range of `0`. @param updater An object that manages updates to the collection view.
@param viewController The view controller that will house the adapter. @return A new list adapter object.
*/
- (instancetype)initWithUpdater:(id <IGListUpdatingDelegate>)updater
viewController:(nullable UIViewController *)viewController; /**
Perform an update from the previous state of the data source. This is analogous to calling
`-[UICollectionView performBatchUpdates:completion:]`. @param animated A flag indicating if the transition should be animated.
@param completion The block to execute when the updates complete.
*/
- (void)performUpdatesAnimated:(BOOL)animated completion:(nullable IGListUpdaterCompletion)completion; /**
Perform an immediate reload of the data in the data source, discarding the old objects. @param completion The block to execute when the reload completes.
*/
- (void)reloadDataWithCompletion:(nullable IGListUpdaterCompletion)completion; /**
Reload the list for only the specified objects. @param objects The objects to reload.
*/
- (void)reloadObjects:(NSArray *)objects; /**
Query the section controller at a given section index. Constant time lookup. @param section A section in the list. @return A section controller or `nil` if the section does not exist.
*/
- (nullable IGListSectionController *)sectionControllerForSection:(NSInteger)section; /**
Query the section index of a list. Constant time lookup. @param sectionController A list object. @return The section index of the list if it exists, otherwise `NSNotFound`.
*/
- (NSInteger)sectionForSectionController:(IGListSectionController *)sectionController; /**
Returns the section controller for the specified object. Constant time lookup. @param object An object from the data source. @return A section controller or `nil` if `object` is not in the list. @see `-[IGListAdapterDataSource listAdapter:sectionControllerForObject:]`
*/
- (__kindof IGListSectionController * _Nullable)sectionControllerForObject:(id)object; /**
Returns the object corresponding to the specified section controller in the list. Constant time lookup. @param sectionController A section controller in the list. @return The object for the specified section controller, or `nil` if not found.
*/
- (nullable id)objectForSectionController:(IGListSectionController *)sectionController; /**
Returns the object corresponding to a section in the list. Constant time lookup. @param section A section in the list. @return The object for the specified section, or `nil` if the section does not exist.
*/
- (nullable id)objectAtSection:(NSInteger)section; /**
Returns the section corresponding to the specified object in the list. Constant time lookup. @param object An object in the list. @return The section index of `object` if found, otherwise `NSNotFound`.
*/
- (NSInteger)sectionForObject:(id)object; /**
Returns a copy of all the objects currently driving the adapter. @return An array of objects.
*/
- (NSArray *)objects; /**
An unordered array of the currently visible section controllers. @return An array of section controllers.
*/
- (NSArray<IGListSectionController *> *)visibleSectionControllers; /**
An unordered array of the currently visible objects. @return An array of objects
*/
- (NSArray *)visibleObjects; /**
An unordered array of the currently visible cells for a given object. @param object An object in the list @return An array of collection view cells.
*/
- (NSArray<UICollectionViewCell *> *)visibleCellsForObject:(id)object; /**
Scrolls to the specified object in the list adapter. @param object The object to which to scroll.
@param supplementaryKinds The types of supplementary views in the section.
@param scrollDirection An option indicating the direction to scroll.
@param scrollPosition An option that specifies where the item should be positioned when scrolling finishes.
@param animated A flag indicating if the scrolling should be animated.
*/
- (void)scrollToObject:(id)object
supplementaryKinds:(nullable NSArray<NSString *> *)supplementaryKinds
scrollDirection:(UICollectionViewScrollDirection)scrollDirection
scrollPosition:(UICollectionViewScrollPosition)scrollPosition
animated:(BOOL)animated; /**
Returns the size of a cell at the specified index path. @param indexPath The index path of the cell. @return The size of the cell.
*/
- (CGSize)sizeForItemAtIndexPath:(NSIndexPath *)indexPath; /**
Returns the size of a supplementary view in the list at the specified index path. @param elementKind The kind of supplementary view.
@param indexPath The index path of the supplementary view. @return The size of the supplementary view.
*/
- (CGSize)sizeForSupplementaryViewOfKind:(NSString *)elementKind
atIndexPath:(NSIndexPath *)indexPath; /**
:nodoc:
*/
- (instancetype)init NS_UNAVAILABLE; /**
:nodoc:
*/
+ (instancetype)new NS_UNAVAILABLE; @end

2.

AVIMCMD 只需实现 packToSendData

- (void)sendCustomC2CMsg:(AVIMCMD *)cmd toUser:(id<IMUserAble>)recv succ:(TIMSucc)succ fail:(TIMFail)fail
{
if (cmd && recv)
{
TIMConversation *conv = [[TIMManager sharedInstance] getConversation:TIM_C2C receiver:[recv imUserId]]; TIMCustomElem *elem = [[TIMCustomElem alloc] init];
elem.data = [cmd packToSendData]; TIMMessage *timMsg = [[TIMMessage alloc] init];
[timMsg addElem:elem]; [timMsg setPriority:TIM_MSG_PRIORITY_HIGH]; [conv sendMessage:timMsg succ:^{ if (cmd.userAction == AVIMCMD_PACustomMsg)
{
[self onRecvC2CSender:[IMAPlatform sharedInstance].host customMsg:elem];
} if (succ)
{
succ();
}
} fail:^(int code, NSString *msg) { DebugLog(@"发送消息失败:%@", timMsg); if (fail)
{
fail(code, msg);
}
}];
}
} ... - (NSData *)packToSendData
{
NSMutableDictionary *post = [NSMutableDictionary dictionary];
[post setObject:@(self.userAction) forKey:@"userAction"]; if (self.actionParam && self.actionParam.length > )
{
[post setObject:self.actionParam forKey:@"actionParam"];
}
if (self.msgId && self.msgId.length > )
{
[post setObject:self.msgId forKey:@"id"];
}
if (self.type && self.type.length>)
{
[post setObject:self.type forKey:@"type"];
} if (_nickName && _nickName.length > )
{
[post setObject:_nickName forKey:@"NickName"];
} if (_content && _content.length > )
{
[post setObject:_content forKey:@"Content"];
} if (_headUrl && _headUrl.length > )
{
[post setObject:_headUrl forKey:@"HeadUrl"];
} if (_company && _company.length > )
{
[post setObject:_company forKey:@"Company"];
} if (_roomId && _roomId.length > )
{
[post setObject:_roomId forKey:@"RoomId"];
} if (_muteUserId && _muteUserId.length > )
{
[post setObject:_muteUserId forKey:@"MuteUserId"];
} NSLog(@"发送的参数:%@", post);
if ([NSJSONSerialization isValidJSONObject:post])
{
NSError *error = nil;
NSData *data = [NSJSONSerialization dataWithJSONObject:post options:NSJSONWritingPrettyPrinted error:&error];
if(error)
{
// DebugLog(@"[%@] Post Json Error: %@", [self class], post);
return nil;
} // DebugLog(@"AVIMCMD content is %@", post);
return data;
}
else
{
// DebugLog(@"[%@] AVIMCMD is not valid: %@", [self class], post);
return nil;
}
}

TIMCustomElem *转 TCShowLiveMsg*

// 收到C2C自定义消息
- (void)onRecvC2CSender:(id<IMUserAble>)sender customMsg:(TIMCustomElem *)msg
{
id<AVIMMsgAble> cachedMsg = [self cacheRecvC2CSender:sender customMsg:msg];
[self enCache:cachedMsg noCache:^(id<AVIMMsgAble> msg){
。。。 - (id<AVIMMsgAble>)cacheRecvC2CSender:(id<IMUserAble>)sender customMsg:(TIMCustomElem *)msg
{
NSString *datastring = [[NSString alloc] initWithData:msg.data encoding:NSUTF8StringEncoding]; NSLog(@"接收到的消息:%@", datastring);
TCShowLiveMsg *cmsg = [NSObject parse:[TCShowLiveMsg class]
jsonString:datastring]; cmsg.msgText = cmsg.Content;
cmsg.sender = sender;
cmsg.isC2CMsg = YES;
if (!_isPureMode)
{
[cmsg prepareForRender];
}
return cmsg; }
//重写该方法
- (id<AVIMMsgAble>)cacheRecvGroupSender:(id<IMUserAble>)sender customMsg:(TIMCustomElem *)msg;
{
NSString *datastring = [[NSString alloc] initWithData:msg.data encoding:NSUTF8StringEncoding]; NSLog(@"接收到的消息:%@", datastring); TCShowLiveMsg *cmsg = [NSObject parse:[TCShowLiveMsg class] jsonString:datastring];

3.

mvc

https://github.com/Yrocky/NetworkCouplerController/

第9月第15天 设计模式 adapter mvc的更多相关文章

  1. Android 设计模式之MVC模式

    说到Android设计模式的MVC模式,估计很多人都是比较熟悉了,这里深入了解一下MVC到底是怎么回事,以ListView为例子讲解. 一.深入理解MVC概念 MVC即Model-View-Contr ...

  2. python 设计模式之MVC模式

    一.简单介绍 mvc模式  the  model-view-controller pattern mvc模式是一个运用在软件工程中的设计模式.mvc模式脱离了以前简单的web服务设计逻辑,将开发,测试 ...

  3. Python设计模式之MVC模式

    # -*- coding: utf-8 -*- # author:baoshan quotes = ('A man is not complete until he is married. Then ...

  4. 1个月连载30个设计模式真实案例(附源码),挑战年薪60W不是梦

    本文所有内容均节选自<设计模式就该这样学> 本文自2012年10月29日起持续连载,请大家持续关注.... 序言 Design Patterns: Elements of Reusable ...

  5. C++设计模式-Adapter适配器模式(转)

    Adapter适配器模式作用:将一个类的接口转换成客户希望的另外一个接口.Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作. 分为类适配器模式和对象适配器模式. 系统的数据和 ...

  6. java设计模式—Adapter模式

    1.核心意图:     将一个类的接口转换成客户希望的另外一个接口,从而使得原本由于接口不兼容而不能一起工作的类可以一起工作. 该模式的目标是通过一个代理(这里是Adapter),在原来的类(Adap ...

  7. 设计模式Adapter模式的五分钟

    五分钟一个设计模式.来形容叙述的设计模式的最简单方法.看到许多其他设计模式,请点击五分钟一个设计模式系列 http://blog.csdn.net/daguanjia11/article/catego ...

  8. [C# 设计模式] Adapter - 适配器模式(两种)

    Adapter - 适配器模式 序 现实生活中,我们常用到适配器. 你当前打开我这篇文章的笔记本电脑,电源的另一边不正连着一块适配器吗? 你平时想将三口插座插进二口插座里面,不也需要一个适配器吗? 整 ...

  9. 设计模式-Adapter(结构型模式)针对第三方库 函数 接口不匹配的问题,分为类模式 与 对象模式 两种

    以下代码来源: 设计模式精解-GoF 23种设计模式解析附C++实现源码 //****************************类模式的Adaptr*********************** ...

随机推荐

  1. FUNMVP:几张图看懂区块链技术到底是什么?(转载)

    几张图看懂区块链技术到底是什么? 本文转载自:http://www.cnblogs.com/behindman/p/8873191.html “区块链”的概念可以说是异常火爆,好像互联网金融峰会上没人 ...

  2. EOS 权限管理之-权限的使用

    首先,跟大家说声抱歉,由于之前一直在准备EOS上线的一些工作,所以,很长时间没有更新内容.今天正好有时间,也想到了一些题材,就来说一下这个话题.本文完全是个人见解,如有不当之处,欢迎指出. 前提回顾: ...

  3. Android TextView中图文混排设置行间距导致高度不一致问题解决

    最近项目中需要实现一个评论带表情的功能,刚开始一切顺利,非常easy,突然有一天发现文字跟表情混排的时候,TextView中图文高度不一致,excuse...什么鬼,之前明明测试过图文混排,不存在这个 ...

  4. 《当大数据遇见网络:大数据与SDN》

    总体结构: <当大数据遇见网络:大数据与SDN> 摘要 大数据和SDN无论是对于学术界还是工业界来说都极具吸引力.传统上人们都是分别在最前沿工作中研究这两个重要的领域.然而一方面,SDN的 ...

  5. 课堂final发布

    项目组名:奋斗吧兄弟 小组成员:黄兴.李俞寰.栾骄阳.王东涵.杜桥 今天6个小组在课上进行了Final发布,以下是我的一些看法: 1.Nice团队的约跑app: 今天Nice团队给我的最突出的印象就是 ...

  6. Codeforces914G Sum the Fibonacci(FWT)

    FWT大杂烩.跟着模拟做很多次FWT即可. #include<iostream> #include<cstdio> #include<cmath> #include ...

  7. NOIP2018备考——DP专题练习

    P4095 [HEOI2013]Eden 的新背包问题   P2657 [SCOI2009]windy数   P3413 SAC#1 - 萌数   P3205 [HNOI2010]合唱队   P476 ...

  8. 主机 & 虚拟机 & 开发板 相互通信

    @2018年7月10日 成功方法之一: 虚拟机设置为桥接模式,保证三者在同一网段,ping方式测试网络连通性OK

  9. (转)Java随机数

    1 随机数的三种产生方式 本章先讲解Java随机数的几种产生方式,然后通过示例对其进行演示. 广义上讲,Java中的随机数的有三种产生方式: (01). 通过System.currentTimeMil ...

  10. C# 编码规范、命名规则

    1  规范目的 ……………………………………………………… 3 2  适用范围 ……………………………………………………… 3 3  代码注释 ……………………………………………………… 3 3.1  ...