singleton

Good OO design - The Singleton Design Pattern

青春壹個敷衍的年華 提交于 2019-12-18 10:54:29
问题 All of a sudden I'm having a bit of an OO crisis. Over the last couple of years I've made quite good use of Singleton objects. I used them in many places. For example, in designing an MVC Java application, I'd create a Singleton 'SystemRegistry' class to store the model and view classes (I've only worked on simple apps and the need for multiple views never came up). When I create my model and view objects (which weren't singletons, just normal objects), I'd do something like: SystemRegistry

How do I implement convenient logging without a Singleton?

随声附和 提交于 2019-12-18 09:56:15
问题 My current implementation, simplified: #include <string> #include <memory> class Log { public: ~Log() { // closing file-descriptors, etc... } static void LogMsg( const std::string& msg ) { static std::unique_ptr<Log> g_singleton; if ( !g_singleton.get() ) g_singleton.reset( new Log ); g_singleton->logMsg( msg ); } private: Log() { } void logMsg( const std::string& msg ) { // do work } }; In general, I am satisfied with this implementation because: lazy instantiation means I don't pay unless I

iOS : How to reference a music background from a singleton class?

大憨熊 提交于 2019-12-18 09:49:44
问题 I have done a singleton class called MyBgMusic.h & MyBgMusic.m. How to reference that singleton class to my SecondViewController or the rest of the XIB. Here is my singleton class: H file : #import <Foundation/Foundation.h> #import <AVFoundation/AVAudioPlayer.h> @interface MyBgMusic : UIViewController <AVAudioPlayerDelegate> { AVAudioPlayer *player; UIButton *playBgMusic; } @property (nonatomic, retain) IBOutlet AVAudioPlayer *player; @property (nonatomic, retain) IBOutlet UIButton

Application singleton use in Android

会有一股神秘感。 提交于 2019-12-18 09:01:43
问题 I have a facebook initialize sdk call and I need it to initialize the moment application is launched: I want to use my Application class to do that. for example: public class App extends Application { @Override public void onCreate() { super.onCreate(); FacebookSdk.sdkInitialize(getApplicationContext()); } } I have the main activity with the facebook login button: public class MainActivity extends AppCompatActivity { @BindView(R.id.login_button) LoginButton loginButton; private

The best singleton pattern since Java 5

眉间皱痕 提交于 2019-12-18 08:58:06
问题 Since Java 5 it is said that the best way to create a singleton is by a single-element enum type. Example: public enum SuperSingleton implements Zooma{ INSTANCE; /** */ public void fightTheBattle(){ System.out.println("I am fighting the battle!!!"); } @Override public void runningWild() { //This is method implemented from the Zooma interface. } } According to Joshua Bloch, the single-element enum type singleton is; more concise provides the serialization machinery for free and provides an

C++: Undefined reference to instance in Singleton class

天涯浪子 提交于 2019-12-18 07:46:10
问题 I'm currently trying to implement a factory as a singleton. I practically used the textbook example of the Singleton pattern. Here's the .h file: namespace oxygen{ class ImpFactory{ public: static boost::shared_ptr<ImpFactory> GetInstance(); private: static boost::shared_ptr<ImpFactory> mInstance; }; and here's the .cpp file: #include "impfactory.h" using namespace oxygen; using namespace boost; shared_ptr<ImpFactory> ImpFactory::GetInstance(){ if (mInstance.get() == 0) mInstance = shared_ptr

Strange System.__Canon exception

被刻印的时光 ゝ 提交于 2019-12-18 05:46:09
问题 I have a windows service that uses a singleton class ThreadQueue<T> . When the service starts it makes a call to ThreadQueue<string>.Start() this class then accepts and queues tasks limiting the concurrency to a configurable number of threads. ThreadQueue<string>.Start() is called once and only once when the service starts up. On occasion, after a few hours of the service running i receive the following exception: Application: myservice.exe Framework Version: v4.0.30319 Description: The

When/why does my Java singleton instance get destroyed?

眉间皱痕 提交于 2019-12-18 04:12:42
问题 I have an android app that is setup to start a Java activity (call it MyJavaActivity), which in turn launches a NativeActivity. When the NativeActivity finishes it returns back to MyJavaActivity. I also have a Java singleton class (call it MyJavaSingleton) which I would like to stay in memory throughout my application's lifecycle. I set some of the singleton class's member variables from my NativeActivity (using JNI), which can later be retrieved by MyJavaActivity. The problem is,

Best way to implement singleton in a console application C#?

有些话、适合烂在心里 提交于 2019-12-18 04:09:28
问题 I have a console application that is server based. I only want 1 instance of it running at once for a particular server (this is regardless of the user who might be running it). I need to add a check to make sure only 1 instance of it is running, I can already do this by checking the running processes on the server, but is this best practice? Since I am constantly looking for ways to improve coding styles and stay up to date, is there a better way to do this lately? If you're thinking - "If

PHP Threads and Synchronization

核能气质少年 提交于 2019-12-18 03:41:53
问题 I'm new to PHP, so to get started I've decided to implement a singleton. While I am able to recreate the singleton pattern in php, but I am not sure how to implement double-checked locking. Is that even possible/needed in PHP. I have read somewhere that PHP is not multithreaded? Can someone confirm that? If it is multithreaded, can someone explain to me how lock() or synchronize() work in PHP? Thanks, Henry 回答1: Share-nothing Architecture PHP has a Share-nothing Architecture: Like HTTP, each