1

我正在用Objective C编写一个类,它有一个带有同一类实例的ivar ...

@interface State : NSObject {
NSString *name
NSSet *neighboringStates    // this is a set of State objects
}

州 *california 将具有包含 *oregon、*washington 和 *nevada 的 *neighboringStates。这三个州中的每一个在其邻国中都有*加利福尼亚州。

我希望指针引用相同的实例,以便每个状态只实例化一次,并且在另一个状态下的方法调用期间对状态的任何更改都会影响内存中的同一个对象。

这行得通吗?我什至不确定这个术语是什么,所以很难用谷歌搜索它。

我感谢您的帮助!

编辑:

为了让您更清楚我正在尝试做什么,我想生成状态排列,以便每个状态仅访问一次,并且排列列表中的每个下一个状态必须从相邻/相邻状态到当前一。因此,一旦我从加利福尼亚州开始并前往俄勒冈州,因为我知道我当前的路径包含加利福尼亚州,所以我将加利福尼亚州从我的可用状态中删除。我通过获取当前状态的相邻状态集并删除当前路径中的任何状态来跟踪可用状态。

4

3 回答 3

2

您所描述的内容没有理由不起作用,尽管在代码中设置这一切会很长而且很无聊:

State * california = [State stateWithName:@"California"];
State * oregon = [State stateWithName:@"Oregon"];
State * nevada = [State stateWithName:@"Nevada"];
// And so on, forty-seven more times.
// Stick them in an array or something?
// Now use all those instances that you've just created to set up 
// the neighboring states.
[california setNeighboringStates:[NSSet setWithObjects:oregon, nevada, nil];
[oregon setNeighboringStates:[NSSet setWithObjects:washington, california, idaho, nevada, nil];
// And so on, forty-seven more times.

但是,当您拥有这样的数据时,它在应用程序的生命周期内是固定的,您需要考虑将其粘贴到某种数据文件中,然后在运行时将其取消归档。

于 2012-02-27T06:19:47.727 回答
2

创建一个Country类,该类也用作State工厂。

因此,States不要创造其他States,他们从Country他们所属的那里得到他们。

同样,客户端不会创建States,而是从Country.

State来自 s 的请求Country还可以在返回之前验证 aState是否已正确初始化以供公众使用——因为您不能“简单地”初始化 48 个相互依赖项 ( neighboringStates)。

于 2012-02-27T06:13:54.487 回答
1

我不确定你的问题到底是什么。有一种称为 Singleton 的设计模式可确保您只能拥有给定类的一个实例。这不完全是您想要的,但是有一个称为 Multiton 的 Singleton 模式的变体,它为给定的键(在您的情况下为状态名称)保留类的单个实例。这就是[NSNull null](单例)或NSNumber(多例)的工作方式:

NSLog(@"%i", [NSNull null] == [NSNull null]); // 1
NSLog(@"%i", [NSNumber numberWithInt:42] == [NSNumber numberWithInt:42]); // 1

但是这些模式很容易被误用,如果没有它们就可以解决问题可能会好得多。让所有的状态指针指向同一个实例是没有问题的。也就是说,只有创建整个对象图的问题。如果您保持属性可变,这很容易:

State *oregon = [[State alloc] initWithName:@"Oregon"];
State *washington = [[State alloc] initWithName:@"Washington"];
[oregon setNeighbouringStates:[NSSet setWithObject:washington]];
[washington setNeighbouringStates:[NSSet setWithObject:oregon]];

等等。对您的问题更好的解决方案可能是不同的数据模型,不同的方式来表示相邻关系。但这取决于您的要求。

您在评论中说您在字典中有状态。键是状态名称,值是相邻状态名称的数组?如果是这种情况,您可以执行以下操作:

typedef State* (^StateBuilder)(NSString *stateName);

NSDictionary *input = …;
NSMutableDictionary *states = [NSMutableDictionary dictionary];

StateBuilder getState = ^(NSString *stateName) {
    State *state = [states objectForKey:stateName];
    if (!state) {
        state = [[State alloc] initWithName:stateName];
        [states setObject:state forKey:stateName];
    }
    return state;
};

for (NSString *stateName in [input allKeys]) {
    State *state = getState(stateName);
    NSMutableSet *neighbours = [NSMutableSet set];
    for (NSString *neighbourName in [input objectForKey:stateName])
        [neighbours addObject:getState(neighbourName)];
    [state setNeighbours:neighbours];
}

结果将在[states allValues]. 当然,block voodoo不是必须的,你可以把states字典变成实例变量,用正则方法代替block。

于 2012-02-27T06:14:43.313 回答