|
基于MVC,我将项目程序分为了三个group,使用快捷键Command+option+N新建三个group,并且命名为Model、View、Controller,同时将相应的源文件拖入相应的分组,如下图: group分组单例模式(The Singleton Pattern)单例模式能够确保指定类只有一个实例,并且全局可以访问到该实例。苹果官方有大量使用这个模式,比如:UserDefaults.standard、UIApplication.shared、UIScreen.main等等,但是,单例也不能够滥用,因为单例一旦创建他的内存将存在于整个应用程序的一生,直到应用程序被关闭,内存才被释放。本例中:BookAPI类是单例用来处理书籍数据 final class BookAPI: NSObject { private var persistencyManager: PersistencyManager private var httpClient: HTTPClient /// 单例 static let sharedInstance = BookAPI() override init() { // 线程安全 persistencyManager = PersistencyManager() httpClient = HTTPClient() super.init() } /// 返回书籍数量 /// /// - Returns: book数组 func getBooks() -> [Book] { return persistencyManager.books } /// 保存书籍数据 func saveBooks() { persistencyManager.saveBooks() } /// 删除指定位置的绘本 /// /// - Parameter index: index 0 func deleteBookAt(index: Int) { httpClient.postRequest(url: "delete/book", params: ["book_id": ""]) { (res) in if res { // 服务器删除成功,删除本地的绘本 persistencyManager.deleteBookAt(index: index) } else { print("删除绘本失败,请稍后再试") } } } }
外观模式(the Facade Pattern) 外观模式的形象例子外观设计模式为复杂的子系统提供单一的接口,只公开一个简单统一的API,而不是将一组类及其API暴露给用户。 Facade PatternAPI的用户完全不知道下面的复杂性。 这种模式在使用大量类时非常理想,特别是当它们复杂使用或难以理解时。外观模式将使用系统的代码与您隐藏的类的接口和实现相分离; 它也减少了外部代码对子系统内部工作的依赖性。 如果外观下面的类改变,那么外部类可以保留相同的API。例如,如果您希望替换后台服务器地址,您将不必更改您的API的代码。在本例中如何使用外观设计模式:目前我们有PersistencyManager来获取本地书籍数据,保存书籍数据;HTTPClient来处理网络请求。我们项目中的其他类将不会知道这个底层逻辑。如下图我们将在BookAPI暴露以下方法来供外部调用: 公共方法而persistencyManager、httpClient为BookAPI的私有成员变量,不暴露给外部调用; 私有成员变量装饰设计模式(The Decorator Design Patte) 装饰模式的形象例子装饰设计模式可以在不修改其代码的情况下,动态地向对象添加行为和责任。 就扩展功能来说,装饰设计模式相比于生成子类更为灵活。在Objective-C中,这种模式有两种非常常见的实现:Category(类别)和Delegation(委派)。swift中使用extension为相应的类添加扩展。以下三种情况考虑使用这一模式: - 想要在不影响其他对象的情况下,移动台、透明的方式给单个对象添加职责;
- 想要扩张一个类的行为,却做不到。类定义可能被隐藏,无法进行子类话;或者对类的每个行为的扩展,为支持美中功能组合,将产生大量的子类;
- 对类的职责的扩展是可选的。
extension本例中的book封面,后台返回的字段"pic": "/image/20160826/d51285bb636281dce6974313eaf6f15d.png"只是一个路径,前面的域名https://xxxxxx-aliyun.firstleap.cn需要我们这边统一拼接,同时我们使用的阿里云存储服务,有对图片进行处理(包括图片压缩,减少图片分辨率),且iPhone和iPad的处理还不一样。 我们对String添加一个扩展(oc中为category),File/New/File...,选择Swift File,命名为String+Aliyun,添加以下代码: // 判断型号 let isPad = ( UI_USER_INTERFACE_IDIOM() == .pad) let fileHost = "https://xxxxxx-aliyun.firstleap.cn" extension String { func aliyunThumb() -> String { if isPad { return "(fileHost)(self)" + "!iPadThumb" } else { return "(fileHost)(self)" + "!thumb" } } }
Delegation本例中的BookView作为UICollectionView的子类,有两个方法你必须实现,那就是 // 有多少个item public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int // 每个item需要现实的信息内容 public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
这两个方法都是有BookView的代理ViewController中实现,苹果的UIKit中有很多都是代理委托模式(UITableView, UITextView, UITextField, UIWebView, UIAlert, UIActionSheet, UICollectionView, UIPickerView, UIGestureRecognizer, UIScrollView) ViewController中实现BookView的代理方法如下: extension ViewController: UICollectionViewDataSource, UICollectionViewDelegate { func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return books.count } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "BookCell", for: indexPath) as! BookCell if indexPath.item > books.count - 1 { cell.coverName.isHidden = true cell.bookCover.isHidden = true cell.isReadImageView.isHidden = true } else { cell.coverName.isHidden = false cell.bookCover.isHidden = false cell.isReadImageView.isHidden = false cell.book = books[indexPath.item] } return cell } func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { let alertVc = UIAlertController(title: "要删除这本绘本么", message: "", preferredStyle: .alert) let sureAction = UIAlertAction(title: "确定删除", style: .default, handler: { (_) in }) alertVc.addAction(sureAction) let cancelAction = UIAlertAction(title: "取消", style: .default, handler: nil) alertVc.addAction(cancelAction) present(alertVc, animated: true, completion: nil) } }
备忘录模式(The Memento Pattern) (编辑:52刷机网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|