简体   繁体   English

类包装器和Java,以及Objective-C中的单例之间有什么区别?

[英]What is the difference between a class wrapper and Java, and a singleton in Objective-C?

I hear the term "Class Wrapper" float around a lot, and I was curious what it is. 我听到“ Class Wrapper”一词泛滥,我很好奇它是什么。 I read explanations on this site about them, and they basically you just sound like a Singleton and Objective-C. 我在本网站上阅读了有关它们的解释,基本上它们听起来像是Singleton和Objective-C。 My two questions are: 1. What is the difference between a singleton in the class wrapper? 我的两个问题是:1.类包装中的单例之间有什么区别? 2. Are class wrappers ever used in iOS projects, or do we use singletons in the iOS world more often? 2.类包装器是否曾在iOS项目中使用过,还是我们在iOS世界中更频繁地使用单例?

I think you are confused on what one of those entities is, they are not similar in the slightest (apart from being a part of software engineering...). 我认为您对这些实体中的一个感到困惑,它们丝毫不相似(除了作为软件工程的一部分...)。

A singleton is a single object (of any class you like) that can be accessed from anywhere in the entire project by any bit of code you like. 单例是单个对象(您喜欢的任何类),可以通过您喜欢的任何代码从整个项目中的任何位置访问它。 It's a single point in space that is reference-able from any other point in space. 它是空间中的单个点,可以从空间中的任何其他点参考。

A "Wrapper Class" is a class that wraps another class (or primitive). “包装器类”是包装另一个类(或基元)的类。 Say you want to pass an int to a function or method, that method modifies that int and you want the calling object to see the change. 假设您要将int传递给函数或方法,则该方法会修改该int,并且您希望调用对象看到更改。 Since ints are passed by value instead of by reference (by default) any changes you make to that int in the called function are ignored by the caller. 由于int是通过值而不是通过引用传递的(默认情况下),因此调用者将忽略在调用函数中对该int所做的任何更改。 So the caller sees no change in the int's value. 因此,调用方看不到int的值有任何变化。 Now if you make an Integer object and make it wrap an int you can now pass that Integer object by reference to functions or methods, modify the Integer object and get the modified value back into the calling function. 现在,如果您制作一个Integer对象并使其包装一个int,则现在可以通过引用函数或方法来传递该Integer对象,修改Integer对象并将修改后的值返回给调用函数。 A Wrapper Class wraps an object (or primitive) into another object to give it different functionality. Wrapper Class将一个对象(或原语)包装到另一个对象中以赋予其不同的功能。

Singletons and class wrappers are two different concepts. 单例和类包装器是两个不同的概念。 A singleton refers to a class whose only role is to have one instance, hence the name. 单例是指一个类,其唯一的作用是拥有一个实例,因此得名。 Say you wanted to build an address book and store multiple contacts within, it would make sense that there only be one address book instance in your application instead of creating multiple address books if the purpose of your application is only to keep track of contacts within a single address book. 假设您要建立一个通讯录并在其中存储多个联系人,那么如果应用程序的目的只是为了跟踪一个联系人中的联系人,那么在您的应用程序中只有一个通讯录实例而不是创建多个通讯录是有意义的单个地址簿。

Within Obj-C class wrappers form the concept of bridging a primitive data type inherited from the C language and having object representations for them. 在Obj-C类包装器中,形成了桥接从C语言继承的原始数据类型并为其提供对象表示的概念。 You gave the example of a float data type. 您给出了一个float数据类型的示例。 In the case of numbers the foundation framework provides the abstract NSNumber class to 'wrap' around a primitive data type such as a float, or int and give you an object representation. 对于数字,基础框架提供了抽象的NSNumber类,以“包装”原始数据类型(例如float或int),并为您提供对象表示形式。 From there you are able to store them in an array, or dictionary or deal with them as normal objects (send messages, extend, etc). 从那里,您可以将它们存储在数组或字典中,或将它们作为普通对象处理(发送消息,扩展等)。 Another great example of a generic class wrapper is the NSValue class that allows you to create object representations for say a CGPoint or CGRect, which are used extensively in iOS. 通用类包装器的另一个很好的例子是NSValue类,它允许您为CGPoint或CGRect创建对象表示,它们在iOS中得到了广泛使用。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM