swift 基础类Swift实现Clean Architecture用例,请参阅https://8thlight.com/blog/uncle-bob/2012/08/13/the-clean-arc

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift 基础类Swift实现Clean Architecture用例,请参阅https://8thlight.com/blog/uncle-bob/2012/08/13/the-clean-arc相关的知识,希望对你有一定的参考价值。

/*
 The MIT License (MIT)
 
 Copyright (c) 2016 Nathan Smith
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included in all
 copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 SOFTWARE.
*/

import RxSwift

/**
 **Application Business Rule**
 
 Taken from [The Clean Architecture by Uncle Bob](https://8thlight.com/blog/uncle-bob/2012/08/13/the-clean-architecture.html):
 
 The software in this layer contains application specific business rules. It encapsulates and implements all of the use cases of the system. These use cases orchestrate the flow of data to and from the entities, and direct those entities to use their enterprise wide business rules to achieve the goals of the use case.
 
 We do not expect changes in this layer to affect the entities. We also do not expect this layer to be affected by changes to externalities such as the database, the UI, or any of the common frameworks. This layer is isolated from such concerns.
 
 We do, however, expect that changes to the operation of the application will affect the use-cases and therefore the software in this layer. If the details of a use-case change, then some code in this layer will certainly be affected.
 
 - parameters:
 - Parameter: Type representing the input to the `UseCase`. If no input is required specify `Void`
 - Return: Type representing the ouput to the `UseCase`. If no output is required specify `Void`
 */
class UseCase<Parameter, Return> {
  /// Callers of `execute` are required to add its subscriber to this `disposeBag` e.g. `.addDisposableTo(useCase.disposeBag)`
  let disposeBag: DisposeBag
  
  private let observeOnScheduler: SchedulerType
  private let subscribeOnScheduler: SchedulerType
  
  /**
   - parameters:
   - disposeBag: Reference counted container for subscriptions, when this `UseCase` is deallocated all ongoing subscriptions will be cancelled
   - observeOnScheduler: Determines what thread observables are executed on. Typically this is a background thread pool e.g. `ConcurrentDispatchQueueScheduler(globalConcurrentQueueQOS: .Background)`
   - subscribeOnScheduler: Determines what thread subscribers are executed on. Typically this is the main thread e.g. `MainScheduler.instance`
   */
  init(disposeBag: DisposeBag, observeOnScheduler: SchedulerType, subscribeOnScheduler: SchedulerType) {
    self.disposeBag = disposeBag
    self.observeOnScheduler = observeOnScheduler
    self.subscribeOnScheduler = subscribeOnScheduler
  }
  
  /**
   Abstract method that subclasses are required to implement. Unfortunately there is yet a method to tigger a compile error if missed.
   - parameters:
   - parameter: Input to the `UseCase`
   */
  internal func buildUseCaseObservable(parameter:Parameter) -> Observable<Return> {
    fatalError("Subclasses need to implement the `buildUseCaseObservable()` method.")
  }
  
  /**
   **Examples:**
   ```
   let useCase = UseCase<Void, Void>(...)
   useCase.execute(()).subscribeNext() {
   // Do something as a result
   }.addDisposableTo(useCase.disposeBag)
   ```
   ```
   let useCase = UseCase<Void, Int>(...)
   useCase.execute(()).subscribeNext() { count in
   print(count)
   }.addDisposableTo(useCase.disposeBag)
   ```
   ```
   let useCase = UseCase<String, Int>(...)
   useCase.execute("Hello World").subscribeNext() { count in
   print(11)
   }.addDisposableTo(useCase.disposeBag)
   ```
   
   - parameter parameter: Input to the `UseCase`. If the type `Parameter` is `Void` the value can be specified as `()`
   */
  func execute(parameter:Parameter) -> Observable<Return> {
    return buildUseCaseObservable(parameter)
      .observeOn(observeOnScheduler)
      .subscribeOn(subscribeOnScheduler)
  }
}

以上是关于swift 基础类Swift实现Clean Architecture用例,请参阅https://8thlight.com/blog/uncle-bob/2012/08/13/the-clean-arc的主要内容,如果未能解决你的问题,请参考以下文章

Swift2.0(17)内存管理ARC

Swift学习笔记-自动引用计数弱引用和无主引用

Swift学习笔记-自动引用计数弱引用和无主引用

Swift 栈和堆

试图理解这个随机错误我得到 //swift

Swift之深入解析内存管理的底层原理