日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

mvc2 mvc_迅捷的MVC

發(fā)布時間:2024/1/8 c/c++ 48 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mvc2 mvc_迅捷的MVC 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

mvc2 mvc

Prerequisites:

先決條件:

  • Some understanding of OO terminology and practices

    對OO術(shù)語和實踐的一些理解

Terminology

術(shù)語

Model: Where data, and logic that manipulates the data is stored. Perhaps model objects, or networking code is stored here.Think of this as the what of the App.

模型:數(shù)據(jù)和操作數(shù)據(jù)的邏輯存儲在何處。 也許模型對象,或網(wǎng)絡(luò)代碼存儲的這個here.Think作為在App的東西

View: Present information to the user. Views are, well, UIViews and their subclasses. Think of it as the UI components that have to be controlled by the controller.

查看:向用戶顯示信息。 視圖是UIView及其子類。 可以將其視為必須由控制器控制的UI組件。

Controller: Sits between the view and the model, tying them together (usually using the delegate pattern). The controller is not tightly bound to a concrete view, and communicates via a protocol to an abstraction. An example of this is the way that a UITableView communicates with its data source through the UITableViewDataSource protocol. Think of it as the how of the App. The primary job of the controller is to format the data from the model for the view to display.

控制器:位于視圖和模型之間,將它們綁在一起(通常使用委托模式)。 控制器未緊密綁定到具體視圖,而是通過協(xié)議與抽象進(jìn)行通信。 一個示例就是UITableView通過UITableViewDataSource協(xié)議與其數(shù)據(jù)源進(jìn)行通信的方式。 你可以把它作為應(yīng)用程序的方式 。 控制器的主要工作是格式化模型中的數(shù)據(jù)以供視圖顯示。

Mediator:

調(diào)解員:

The Communication

溝通交流

In MVC the controller can talk to the model (publicly available functionality).

在MVC中,控制器可以與模型對話(公開可用的功能)。

Perfectly acceptable communication from the controller to the model 從控制器到模型的完全可接受的通信

The controller needs to be able to talk to the view through outlets. Equally a view can communicate back to a controller (for example through target-action, or a delegate). This means that the views can be reused, as they are loosely coupled with any particular controller. Views do not own the data they are displaying — this is the use of datasource for UITableView.

控制器需要能夠通過插座與視圖對話。 同樣,視圖可以與控制器通訊(例如,通過目標(biāo)動作或委托)。 這意味著視圖可以重用,因為它們與任何特定的控制器松散地耦合在一起。 視圖不擁有它們所顯示的數(shù)據(jù),這是對UITableView的數(shù)據(jù)源的使用。

Communication from the controller to the view is typical 從控制器到視圖的通信是典型的

However, we should STOP any potential communication from the model to the view. This really makes sense because views are often particular instances of (for example) a UISlider and a UISlider has no idea of the logic from a calculator (or whatever is being built).

但是,我們應(yīng)該停止從模型到視圖的任何潛在通信。 這確實很有意義,因為視圖通常是(例如)UISlider的特定實例,而UISlider不了解計算器(或正在構(gòu)建的任何東西)的邏輯。

Models should not communicate with the view 模型不應(yīng)與視圖進(jìn)行通信

The MVC model tends to go with one screen.

MVC模型傾向于只使用一個屏幕。

Communicate with multiple MVCs — treat each view as a single MVC.

與多個MVC通信-將每個視圖視為一個MVC。

Example

This is going to step through 4 examples to retrieve data from a simple API. Each will use a different method to do so. These are tied in a Tab Bar Controller.

本文將逐步介紹4個示例,以從簡單的API檢索數(shù)據(jù)。 每個人都將使用不同的方法來這樣做。 這些都綁在標(biāo)簽欄控制器中。

Each view controller has a button, and once pressed

每個視圖控制器都有一個按鈕,一旦按下

Method 1 — API call within the view controller

方法1-視圖控制器中的API調(diào)用

This method is commonly used in tutorials and through simple examples of MVC.

此方法通常在教程中以及通過簡單的MVC示例使用。

Unfortunately the view controller needs to know about the baseUrl that is being accessed.

不幸的是,視圖控制器需要知道正在訪問的baseUrl。

We can do better!

我們可以做得更好!

Learning from the separation of concerns, we want to have the HTTP moved outside the view controller

從關(guān)注點分離中學(xué)習(xí),我們希望將HTTP移動到視圖控制器之外

So we set up a TightlyCoupledHTTPManager class which will be called from the view controller.

因此,我們設(shè)置了一個TightlyCoupledHTTPManager類,該類將從視圖控制器中調(diào)用。

Unfortunately there is no way to get the data out to the view controller, for example in the implementation below:

不幸的是,無法將數(shù)據(jù)發(fā)送到視圖控制器,例如在以下實現(xiàn)中:

httpManager.data.count will always be 0. This is because it takes time to make the API call, and as we move through the code above there isn’t time to make sure the data is populated. Any solution (using a timer to wait) is flaky and does not guarantee that the data is populated at any point in time.

httpManager.data.count始終為0。這是因為進(jìn)行API調(diào)用會花費一些時間,并且隨著我們遍歷上面的代碼,沒有時間來確保填充數(shù)據(jù)。 任何解決方案(使用計時器等待)都是不穩(wěn)定的,并且不能保證在任何時間點都會填充數(shù)據(jù)。

Method 2 — Delegate pattern

方法2-委托模式

This pattern means that you can receive the data in any class that conforms to the relevant protocol, that is in this case:

此模式意味著您可以在符合相關(guān)協(xié)議的任何類中接收數(shù)據(jù),在這種情況下:

The new HTTPManager (DelegationHTTPManager) calls the delegate when it has completed:

新的HTTPManager(DelegationHTTPManager)在完成后會調(diào)用委托:

Meaning that the function didDownloadBreaches is called by the delegate.

意味著該函數(shù)didDownloadBreaches由委托調(diào)用。

However for this to happen we have to set the view controller as the delegate:

但是,要做到這一點,我們必須將視圖控制器設(shè)置為委托:

Method 3 — Closures

方法3 —閉包

This ClosureHTTP Manager uses the new Result type introduced in Swift 5.

此ClosureHTTP Manager使用Swift 5中引入的新Result類型。

The callback can be used from a closure, which is then used to update the UI. We may not be on the main thread after using the API call, so we need to make sure any work on the UI is on the proper thread.

可以從閉包中使用該回調(diào),然后該閉包用于更新UI。 使用API??調(diào)用后,我們可能不在主線程上,因此我們需要確保UI上的所有工作都在正確的線程上。

The delegate pattern should be a one-to-one relationship. Therefore if we want to have a number of view controllers who conform to the protocol this is probably not the correct solution.

委托模式應(yīng)該是一對一的關(guān)系。 因此,如果我們希望有許多符合協(xié)議的視圖控制器,那么這可能不是正確的解決方案。

Method 4 — NSNotifications

方法4 — NSNotifications

A notification means that multiple views can listen to notifications when the data has been received.

通知意味著當(dāng)接收到數(shù)據(jù)時,多個視圖可以偵聽通知。

We can name the notification within an extension.

我們可以在擴(kuò)展名中命名通知。

and we set up listeners for the notifications

我們?yōu)橥ㄖO(shè)置了監(jiān)聽器

and prepare to receive the notification

并準(zhǔn)備接收通知

This is then supported by a NotificationHTTPManager:

然后,NotificationHTTPManager支持此功能:

Issues:

問題:

The various iterations of HTTPManager are solely responsible for carrying out the HTTP call. If you wish to decode the JSON too, more work needs to take place and where should it take place?

HTTPManager的各種迭代僅負(fù)責(zé)執(zhí)行HTTP調(diào)用。 如果您也希望解碼JSON,則需要進(jìn)行更多工作,并且應(yīng)該在哪里進(jìn)行?

A git link might help to make this all a little bit clearer.

git鏈接可能有助于使這一切更加清晰。

Want to get in contact? User the link here:

想聯(lián)系嗎? 在此處使用鏈接:

翻譯自: https://medium.com/swift-coding/mvc-in-swift-a9b1121ab6f0

mvc2 mvc

總結(jié)

以上是生活随笔為你收集整理的mvc2 mvc_迅捷的MVC的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。