关于惰性实例化和便捷方法 [英] On lazy instantiation and convenience methods

查看:90
本文介绍了关于惰性实例化和便捷方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您有一个Singleton常量类,您想在整个应用程序中使用该实例的实例.

Assume you have a Singleton Constants class, instance of which you'd like to use throughout your application.

someClass中,因此我们可以引用[Constants instance] someCleverConstant];

In someClass, therefore we can reference [Constants instance] someCleverConstant];

键入它很快就会变旧,并且获得实例的快捷方式将是很好的.

Typing this gets old really quick and it would be nice to get a shortcut to the instance.

  • someClass中,我们可以声明@property (nonatomic, weak, readonly) Constants *constants;
  • 实例的获取者
  • In someClass, we can declare @property (nonatomic, weak, readonly) Constants *constants;
  • And a getter to the instance
-(Constants*) constants {
  if (constants == nil) 
    constants = [Constants instance];
  return constants;
}

通过这种方式在someClass中,因此我们可以改为引用constants.someCleverConstant;

This way in someClass, therefore we can reference constants.someCleverConstant; instead

对此有一些疑问:

  • 我描述的是一种合理的方法吗?
  • 声明属性weak是否正确?
  • 我所描述的内容是否存在性能问题?直接调用实例实际上会更好吗?
  • 考虑一下您有20个类的情况,每个类都需要它自己的指向Constants实例的指针.那么这种方法行得通吗?
  • Is what i described a reasonable approach?
  • Is it correct to declare a property weak?
  • Is there any performance concerns with what i have described? Would it actually be better to call instance directly?
  • Consider a situation where you have 20 classes, each needing it's own pointer to Constants instance. Would this approach work then?

谢谢您的时间.

推荐答案

您可以创建指向单例的全局指针,例如NSApp表示[NSApplication sharedApplication].

You could just create a global pointer to your singleton, like NSApp for [NSApplication sharedApplication].

大概你已经有了类似的东西

Presumably you've already got something like

static Constants * defaultInstance = nil;

位于实现文件顶部.如果删除static,并在标头中声明变量(在.m文件中保留定义):

at the top of your implementation file. If you remove the static, and declare the variable in your header (keeping the definition in the .m file):

@interface Constants : NSObject
// etc.
@end

extern Constants * defaultInstance;

然后,您可以在导入头文件的任何文件中通过名称defaultInstance(不过可能要更改该名称)访问单例实例(无论如何,您都必须这样做).您必须在程序的早期就调用单例设置方法(+instance或其他方法),例如-applicationDidFinishLaunching,以确保在使用指针之前已设置了指针.

You can then access the singleton instance via the name defaultInstance (probably want to change that name, though) in any file that imports the header (which you must be doing anyways). You'll have to call your singleton setup method (+instance or whatever) somewhere very early in your program, such as -applicationDidFinishLaunching to be sure that the pointer is set before you use it.

  • 我所描述的是一种合理的方法吗?
  • Is what I described a reasonable approach?

我认为还有其他更好的方法,如上文和Paul.s的回答所述.

I think there are other, better approaches, described above and in Paul.s's answer.

  • 声明属性weak是否正确?
  • Is it correct to declare a property weak?

是的,具有此指针的类不需要拥有它,因为单例拥有它自己;

Yes, the class that has this pointer doesn't need to own it, because the singleton owns itself;

  • 我所描述的内容是否存在性能问题?直接调用实例实际上会更好吗?
  • Is there any performance concerns with what i have described? Would it actually be better to call instance directly?

无论是[Constants instance]还是self.constants,您都在发送消息.第一次执行self.constants时,您正在执行两次.不过,这都不是真正的问题.

Either way, [Constants instance] or self.constants you're doing a message send. The first time you do self.constants, you're doing two. None of this should be a real concern, though.

  • 考虑一下您有20个类的情况,每个类都需要它自己的指向Constants实例的指针.那么这种方法行得通吗?
  • Consider a situation where you have 20 classes, each needing it's own pointer to Constants instance. Would this approach work then?

对我来说,这似乎很笨拙和不雅致.

To me, it seems unwieldy and inelegant.

这篇关于关于惰性实例化和便捷方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆