objective c - Singleton Instance vs Class Methods -
While recently I have seen two really popular singleton patterns, with the purpose-written and written works in various libraries. One version brings a singleton example and calls its frequency methods and other versions only highlight class methods and does not give you an example of working at any time. Everyone has a summary of the purpose of accessing the same resource (Storekit, CoreData, Pars API etc.) For example, here is the former approach used in the MkStoreKit:
// Start a singleton during the app booth [Mkstore manager shared manager] // Some time later in the app [[MKStore Manager Share Manager] Purchase Feature: KfairID on Complex: ^ (NSSTING * Purchased Feature) {NSLog (@ "Purchased:% @", Purchased Feature); } Canceled: ^ {NSLog (@ "User Cancellation Transaction"); }]; Or alternatively NSUserDefaults, UIApplication etc. Other approaches can be seen in a magical record or with a parse API here:
// Configure API credentials sometime in the app boot [parset apps id: @ "123456" Client's: @ "123456"]; // PFObject * testObject = [PFObject objectWithClassName: @ "TestObject"] after some time; [TestObject setObject: @ "bar" for that: @ "foo"]; [TestObject save]; What are some of the professional and cons of two approaches, and one of them is fundamental better than the other?
Some screen property (performance difference is probably irrelevant) to retrieve the shared frequency, but do I have to screw myself in some other way, for example, testability-wise?
Thanks
There are two different ways of implementing a approach based on class approaches:
- Make a single example using all hidden classes, and hide their methods behind the methods of the wrapper class with the same signature, or
- Do the methods of the class that All work done
The implications of the first implementation is that you can do with a singleton, you What can be done with a hidden singleton:
- Using a subclass becomes a possibility
- It is easy to switch between this example in walking
- State instance resides in instance
- Initialization follows the familiar pattern
If you go for an implementation that does not use singleton, You will count on the stable variable to maintain your current status. This is a valid option, but the initial pattern is different (maybe even using sending_once ), if you implement something ugly if without trusting the conditions Can not change between them, and using a subclass becomes very difficult. Implementation is somewhat easy before testing another because you have a different implementation of singleton for testing, perhaps through the back door; With a stable-based implementation, this route can not be taken.
In short, I will use the singleton-based solution, along with the hidden singleton behind the single "mask" which provides access to the method of Ekton. I will not use an implementation where all the states should be kept in the stable variable.
Comments
Post a Comment