这是Objective-C协议的正确使用吗?

时间:2022-09-06 20:11:05

I'm building an app with, for the purposes of this question, three custom classes: AppDelegate, ListWindowController and ViewOptionsWindowController. AppDelegate keeps retains properties of the single instances of ListWindowController and ViewOptionsWindowController.

我正在构建一个应用程序,为了这个问题的目的,有三个自定义类:AppDelegate,ListWindowController和ViewOptionsWindowController。 AppDelegate保留了ListWindowController和ViewOptionsWindowController的单个实例的属性。

When the user selects a particular menu item, an action in AppDelegate is executed that instantiates the ViewOptionsWindowController. Changes made in this controller's window need to be reflected in ListWindowController's window (either adding or removing a column from the window's table view).

当用户选择特定菜单项时,执行AppDelegate中的实例,该实例将实例化ViewOptionsWindowController。在此控制器窗口中进行的更改需要反映在ListWindowController的窗口中(从窗口的表视图中添加或删除列)。

So, I've defined a protocol, ViewOptionsChanged, which has two required methods, -addColumn and -removeColumn (with parameters to indicate what to add or remove). I've indicated that ListViewController conforms to this protocol, and when instantiating ViewOptionsWindowController am passing AppDelegate's instance of ListViewController. The declaration of ViewOptionsWindowController's init method is:

所以,我已经定义了一个协议ViewOptionsChanged,它有两个必需的方法-addColumn和-removeColumn(带有参数来指示要添加或删除的内容)。我已经指出ListViewController符合这个协议,并在实例化ViewOptionsWindowController时传递AppDelegate的ListViewController实例。 ViewOptionsWindowController的init方法的声明是:

- (id)initWithListController:(id <ViewOptionsChanged>)listController;

so that the only facts that ViewOptionsWindowController knows about the listController parameter is that it conforms to this protocol.

因此,ViewOptionsWindowController了解listController参数的唯一事实是它符合此协议。

So, my question is, is this the proper use of Objective-C protocols? Or would some other design pattern be more appropriate?

所以,我的问题是,这是否正确使用Objective-C协议?或者其他一些设计模式会更合适吗?

2 个解决方案

#1


2  

Yes .. but I think it is a tad confusing. I'd make listView a proper delegate of viewOptions

是..但我认为这有点令人困惑。我将listView作为viewOptions的正确委托

maybe name the protocol ViewOptionsDelegate and the methods viewOptions:changedTo: and put the logic to add/remove columns in the listController?

也许命名协议ViewOptionsDelegate和方法viewOptions:changedTo:并把逻辑添加/删除listController中的列?

feels more cocoa like and really logic about columns don't belong with viewOptions.

感觉更像可可,并且关于列的逻辑真的不属于viewOptions。

but ultimately -- even if you dont agree -- it is used ok IMO.

但最终 - 即使你不同意 - 它也可以用于IMO。

#2


1  

Delegation can be used for this, but, if this is a Mac app, consider bindings instead. Much less code, somewhat less entanglement, can be harder to debug. On either Mac or iOS, you could at least use KVO; simple to debug, somewhat less entanglement.

委托可以用于此,但是,如果这是一个Mac应用程序,请考虑绑定。更少的代码,更少的纠缠,可能更难调试。在Mac或iOS上,你至少可以使用KVO;调试简单,纠缠不那么简单。

#1


2  

Yes .. but I think it is a tad confusing. I'd make listView a proper delegate of viewOptions

是..但我认为这有点令人困惑。我将listView作为viewOptions的正确委托

maybe name the protocol ViewOptionsDelegate and the methods viewOptions:changedTo: and put the logic to add/remove columns in the listController?

也许命名协议ViewOptionsDelegate和方法viewOptions:changedTo:并把逻辑添加/删除listController中的列?

feels more cocoa like and really logic about columns don't belong with viewOptions.

感觉更像可可,并且关于列的逻辑真的不属于viewOptions。

but ultimately -- even if you dont agree -- it is used ok IMO.

但最终 - 即使你不同意 - 它也可以用于IMO。

#2


1  

Delegation can be used for this, but, if this is a Mac app, consider bindings instead. Much less code, somewhat less entanglement, can be harder to debug. On either Mac or iOS, you could at least use KVO; simple to debug, somewhat less entanglement.

委托可以用于此,但是,如果这是一个Mac应用程序,请考虑绑定。更少的代码,更少的纠缠,可能更难调试。在Mac或iOS上,你至少可以使用KVO;调试简单,纠缠不那么简单。