简体   繁体   English

一个控制器来全部统治

[英]One controller to rule them all

I have helper class which contain different methods. 我有包含不同方法的帮助程序类。 I dо not want to include it's header file at every class that I have in my app. 我不想在应用程序中的每个类中都包含它的头文件。 That approach is against DNRY I think. 我认为这种做法违反了DNRY。 Here is why I decide to create one "master" class called AppController and each class in my app to inherit it. 这就是为什么我决定创建一个名为AppController的“主”类,并在我的应用中创建每个类来继承它的原因。

Here is what I do 这是我的工作

AppController.h AppController.h

#import <Foundation/Foundation.h>
#import "Helper.h"

@interface AppController : NSObject
{
  ivars...
}
 some methods ...
@end;

myClass.h myClass.h

import "AppController.h"

@interface myClass : AppController
{
 ivars...
}
some methods
@end

So far, so good. 到现在为止还挺好。 The problem rise when some class needs to inherit not NSObject, but let's say UIViewController. 当某些类需要继承而不是NSObject时,问题就出现了,比如说UIViewController。 This is the moment where my AppController become useless. 这是我的AppController变得无用的那一刻。 I know that Objective-c is not support multiple inheritance. 我知道Objective-c不支持多重继承。 I have read somewhere that can be done using composition, but I can't figure out how to do that. 我读过一些可以使用合成的方法,但是我不知道该怎么做。 I will be grateful if someone gives me some examples. 如果有人给我一些例子,我将不胜感激。

I have read somewhere that can be done using composition, but I can't figure out how to do that. 我读过一些可以使用合成的方法,但是我不知道该怎么做。 I will be grateful if someone gives me some examples. 如果有人给我一些例子,我将不胜感激。

Yes, composition is how you do this. 是的,构图就是您的操作方式。 Your inheritance approach is wrong on several levels. 您的继承方法在几个级别上都是错误的。

  • Your class called "AppController" is not a proper class. 您的类“ AppController”不是正确的类。 It captures unrelated functionality. 它捕获不相关的功能。 A class should do one thing. 一堂课应该做一件事。
  • You're forcing an awkward inheritance tree 您正在强迫笨拙的继承树
  • You've called it "app controller" which is already the name of an important object in Cocoa. 您已经将其称为“应用程序控制器”,它已经是Cocoa中重要对象的名称。

Here is how you approach this: 这是您的处理方式:

  • First, split up your functions and methods into related groups. 首先,将您的功能和方法分成相关的组。 You should not have one giant "utility" class. 您不应有一个庞大的“实用程序”类。 Import these functions and methods into the things that need them, not every class in the system. 将这些功能和方法导入到需要它们的东西中,而不是系统中的每个类中。
  • Use categories to attach new utility functionality to existing classes (including system classes). 使用类别将新的实用程序功能附加到现有的类(包括系统类)。
  • Consider functions rather than methods for many utility-like things (but still, split them up into related files, not one giant "Utility.m"). 考虑使用函数而不是方法处理许多类似于实用程序的事情(但是,请将其拆分为相关文件,而不是一个巨大的“ Utility.m”)。

The vast majority of things people would normally use a "utility" dumping ground for are better solved with categories and functions in ObjC. 人们通常会使用“实用程序”垃圾场进行处理的绝大多数事情都可以通过ObjC中的类别和功能得到更好的解决。 The remainder are usually best served using delegation (but that's another topic). 其余的通常最好使用委托服务(但这是另一个主题)。

A better way would be to add the #import "Helper.h" to your project's precompiled header file (YourAppName-Prefix.pch). 更好的方法是将#import "Helper.h"添加到项目的预编译头文件(YourAppName-Prefix.pch)中。 Doing that is equivalent to importing your helper class header file in every other file of your project, and you're managing it from one spot only. 这样做等同于将助手类头文件导入项目的每个其他文件中,而您仅从一个位置进行管理。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 一个控制器下可能有2个UICollectionViews并在它们之间切换? - is it possible to have 2 UICollectionViews under one controller and toggle between them? 有没有办法枚举所有属性并逐个释放它们? - Is there a way to enumerate all properties and release them one by one? Xcode:如何更改一个ViewController的格式并使它们全部更改 - Xcode: How to change the format of one ViewController and have them all change iOS:选择所有按钮之一时更改其背景图像 - iOS: Change background image for all button when one of them is selected 将所有视图链接到一个视图控制器是不好的做法? - Is having all views linked to one view controller a bad practice? 一个Root视图控制器,管理所有其他视图? - One Root view controller , Manages all other views? 如何将数据从子 controller 之一传递到根 controller 并关闭所有子控制器? - How to pass data to root controller from one of the sub controller and dismiss all the sub controllers? 为什么我的所有Interface Builder视图都等到其中之一完成后才能加载? - Why do all my Interface Builder views wait until one of them is done before loading? 模态展示另一个控制器时,如何完全关闭视图控制器的所有后台工作 - How to fully close all background work of a view controller when modally presenting another one 您是否在同一prepareForSegue方法中为一个视图控制器的所有segue实现行为? - Do you implement behaviour for all the segues of one view controller in the same prepareForSegue method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM