打破Cocoa iOS中MVC对象之间的循环依赖关系

打破Cocoa iOS中MVC对象之间的循环依赖关系,ios,cocoa,testing,model-view-controller,tdd,Ios,Cocoa,Testing,Model View Controller,Tdd,各位好,斯塔克斯 我试图使用TDD在iOS应用程序中实现MVC,但我一直在模型和控制器之间以及控制器和视图之间获得循环依赖关系。我想与Apple文档()中图7.2所示的Cocoa MVC模式紧密匹配 问题源于我对init的tdd要求:所有MVC对象及其所有依赖项。我需要初始化所有依赖项,以便在测试期间可以替换模拟。下面是我的问题的一个简单例子 视图: 示例视图.h //exampleView.h #import <UIKit/UIKit.h> #import "exampleView

各位好,斯塔克斯

我试图使用TDD在iOS应用程序中实现MVC,但我一直在模型和控制器之间以及控制器和视图之间获得循环依赖关系。我想与Apple文档()中图7.2所示的Cocoa MVC模式紧密匹配

问题源于我对init的tdd要求:所有MVC对象及其所有依赖项。我需要初始化所有依赖项,以便在测试期间可以替换模拟。下面是我的问题的一个简单例子

视图:

示例视图.h

//exampleView.h
#import <UIKit/UIKit.h>
#import "exampleViewController.h"

@interface exampleView : UIView

- (id)initWithFrame:(CGRect)frame andVC:(exampleViewController *)VC;

- (void) updateLabelText:(NSString *)newText;

@end
控制器:

//exampleViewController.h
#import <UIKit/UIKit.h>
#import "ExampleModel.h"
#import "ExampleRootView.h"

@interface ExampleViewController : UIViewController

- (id) initWithView:(exampleView *)view andModel:(ExampleModel*)model;

- (void) userActionButtonTapped();

@end

现在,当尝试初始化这些对象时,麻烦来了。因为它们是循环依赖的,这有点像鸡和蛋的场景。

设计MVC系统的正常方式是“向下”或“横向”(例如,视图取决于控制器,控制器取决于模型,模型只取决于其他模型)。在苹果的框架中,视图控制器有点含糊其辞,但仍然广泛适用。让模型依赖于控制器是一件奇怪的事情——为什么这是必要的?听起来可能存在一些不必要的耦合。

是的,这会起作用,但我不能牺牲在测试期间在VCs中模拟视图和在模型中模拟VCs的能力。@BKTurley:我不确定我是否遵循了。如果你的模型不依赖VC,你就不需要模仿它。在模型中模拟VC的必要性在于耦合的影响。
//exampleViewController.h
#import <UIKit/UIKit.h>
#import "ExampleModel.h"
#import "ExampleRootView.h"

@interface ExampleViewController : UIViewController

- (id) initWithView:(exampleView *)view andModel:(ExampleModel*)model;

- (void) userActionButtonTapped();

@end
//exampleModel.h
#import "exampleViewController.h"
@interface exampleModel : NSObject
-(id)initWithVC:(UIViewController *)VC;
//other model type stuff

@end