0

你好堆垛机,

我正在尝试使用 TDD 在 iOS 应用程序中实现 MVC,但我不断获得模型和控制器之间以及控制器和视图之间的循环依赖关系。我想紧密匹配图中所示的 Cocoa MVC 模式。Apple 文档的 7.2(https://developer.apple.com/library/ios/documentation/general/conceptual/CocoaEncyclopedia/Model-View-Controller/Model-View-Controller.html)。

问题源于我对 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

示例视图.m

//exampleView.m
#import "ExampleView.h"
#import "ExampleViewController.h"

@interface exampleView ()

@property (nonatomic) UILabel *label;
@property (nonatomic) UIButton *button;
@property (nonatomic) exampleViewController* VC;

@end

@implementation exampleView
// use your imagination...
@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

模型

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

@end

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

4

1 回答 1

2

设计 MVC 系统的正常方式是让事物“向下”或“横向”依赖(例如,视图依赖于控制器,控制器依赖于模型,模型仅依赖于其他模型)。这在 Apple 框架中的视图控制器中有些糊涂,但仍然广泛适用。让模型依赖于控制器在这里很奇怪——为什么有必要这样做?听起来可能有一些不必要的耦合。

于 2013-09-30T22:59:39.710 回答