Search Results

Search found 6142 results on 246 pages for 'singleton pattern'.

Page 4/246 | < Previous Page | 1 2 3 4 5 6 7 8 9 10 11 12  | Next Page >

  • C#: How to kill a singleton window

    - by Anonymous Coward
    Hi Everyone I'm working on a WPF application which should be utilizable with two monitors. In the main window is a button which detaches a part of the content in a second window wich can then be used on the other minitor. That second window I implemented as a singleton. That works quite good except that the second window doesn't get destoryed on application shutdown which means that the app keeps running in the background. Regarding that problem I'd like to know if a singleton is the right way to do this and if not what would be the right way. If it is, how do I get rid of the instance and why can't I access the singleton instance from app.xaml.cs? Thanks for your help.

    Read the article

  • How to test Guice Singleton?

    - by 01
    Guice Singletons are weird for me First I thought that IService ser = Guice.createInjector().getInstance(IService.class); System.out.println("ser=" + ser); ser = Guice.createInjector().getInstance(IService.class); System.out.println("ser=" + ser); will work as singleton, but it returns ser=Service2@1975b59 ser=Service2@1f934ad its ok, it doesnt have to be easy. Injector injector = Guice.createInjector(); IService ser = injector.getInstance(IService.class); System.out.println("ser=" + ser); ser = injector.getInstance(IService.class); System.out.println("ser=" + ser); works as singleton ser=Service2@1975b59 ser=Service2@1975b59 So i need to have static field with Injector(Singleton for Singletons) how do i pass to it Module for testing?

    Read the article

  • Class design question (Disposable and singleton behavior)

    - by user137348
    The Repository class has singleton behavior and the _db implements the disposable pattern. As excepted the _db object gets disposed after the first call and because of the singleton behavior any other call of _db will crash. [ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)] public class Repository : IRepository { private readonly DataBase _db; public Repository(DataBase db) { _db = db; } public int GetCount() { using(_db) { return _db.Menus.Count(); } } public Item GetItem(int id) { using(_db) { return _db.Menus.FirstOrDefault(x=>x.Id == id); } } } My question is, is there any way to design this class to work properly without removing the singleton behavior? The Repositoryclass will be serving big amount of requests.

    Read the article

  • Way of knowing who called a singleton - objective C

    - by Cyril
    Hello, I am designing a game with several levels. I have a CCLayer defined as a singleton (called MasterScene) where I handle the pause page, transition page, player's score banner,... all the things common to all levels. So in each level, when the user pushes the pause button, a call is made to the singleton to display the CClayer corresponding to the pause page. My problem is that I want to know who called the singleton (which level) t. Is there a way of doing that ? Thanks

    Read the article

  • PHP - static DB class vs DB singleton object

    - by Marco Demaio
    I don't want to create a discussion about singleton better than static or better than global, etc. I read dozens of questions about it on SO, but I couldn't come up with an answer to this SPECIFIC question, so I hope someone could now illuminate me buy answering this question with one (or more) real simple EXAMPLES, and not theoretical discussions. In my app I have the typical DB class needed to perform tasks on DB without having to write everywhere in code mysql_connect/mysql_select_db/mysql... (moreover in future I might decide to use another type of DB engine in place of mySQL so obviously I need a class of abstration). I could write the class either as a static class: class DB { private static $connection = FALSE; //connection to be opened //DB connection values private static $server = NULL; private static $usr = NULL; private static $psw = NULL; private static $name = NULL; public static function init($db_server, $db_usr, $db_psw, $db_name) { //simply stores connections values, withour opening connection } public static function query($query_string) { //performs query over alerady opened connection, if not open, it opens connection 1st } ... } or as a Singletonm class: class DBSingleton { private $inst = NULL; private $connection = FALSE; //connection to be opened //DB connection values private $server = NULL; private $usr = NULL; private $psw = NULL; private $name = NULL; public static function getInstance($db_server, $db_usr, $db_psw, $db_name) { //simply stores connections values, withour opening connection if($inst === NULL) $this->inst = new DBSingleton(); return $this->inst; } private __construct()... public function query($query_string) { //performs query over already opened connection, if connection is not open, it opens connection 1st } ... } Then after in my app if I wanto to query the DB i could do //Performing query using static DB object DB:init(HOST, USR, PSW, DB_NAME); DB::query("SELECT..."); //Performing query using DB singleton $temp = DBSingleton::getInstance(HOST, USR, PSW, DB_NAME); $temp->query("SELECT..."); My simple brain sees Singleton has got the only advantage to avoid declaring as 'static' each method of the class. I'm sure some of you could give me an EXAMPLE of real advantage of singleton in this specific case. Thanks in advance.

    Read the article

  • linq2sql: singleton or using, best practices

    - by zerkms
    what is the preferred practice when linq2sql using (in asp.net mvc applications): to create "singleton" for DataContext like: partial class db { static db _db = new db(global::data.Properties.Settings.Default.nanocrmConnectionString, new AttributeMappingSource()); public static db GetInstance() { return _db; } } or to retrieve new instance when it needed within using: using (db _db = new db()) { ... } the usage of using brings some limitations on code. so I prefer to use singleton one. is it weird practice?

    Read the article

  • Singleton with eager initialization

    - by jesper
    I have class X that takes much time to initialize itself. I want to make that class singleton and force its creation when rails application starts. I've made the singleton: class X @@instance = nil def self.instance if @@instance.nil? @@instance = X.new puts 'CREATING' end return @@instance end private_class_method :new end The problem is that every time I use this class I see 'CREATING' in logs. I've tried to put creation of class in initializers directory but it doesn't work either.

    Read the article

  • Example of Singleton pattern

    - by Supereme
    Hi, Can anybody tell me a good example of Singleton pattern? Also I've one doubt to ask, Is the following scenario is that of singleton pattern: When we have many printers connected in LAN but only one printer queue?

    Read the article

  • Singleton & Multithreading in Java

    - by vivek jagtap
    What is the preferred way to work with Singleton class in multithreaded environment? Suppose if I have 3 thread, and all they try to access getInstance() method of singleton class at the same time - What would happen if no synchronization is maintained? Is it good practice to use synchronized getInstance() method or use synchronized block inside getInstance(). Please advise if there is any other way out.

    Read the article

  • Identity Map Pattern and the Entity Framework

    - by nikolaosk
    This is going to be the seventh post of a series of posts regarding ASP.Net and the Entity Framework and how we can use Entity Framework to access our datastore. You can find the first one here , the second one here and the third one here , the fourth one here , the fifth one here and the sixth one here . I have a post regarding ASP.Net and EntityDataSource. You can read it here .I have 3 more posts on Profiling Entity Framework applications. You can have a look at them here , here and here . In...(read more)

    Read the article

  • Optimal communication pattern to update subscribers

    - by hpc
    What is the optimal way to update the subscriber's local model on changes C on a central model M? ( M + C - M_c) The update can be done by the following methods: Publish the updated model M_c to all subscribers. Drawback: if the model is big in contrast to the change it results in much more data to be communicated. Publish change C to all subscribes. The subscribers will then update their local model in the same way as the server does. Drawback: The client needs to know the business logic to update the model in the same way as the server. It must be assured that the subscribed model stays equal to the central model. Calculate the delta (or patch) of the change (M_c - M = D_c) and transfer the delta. Drawback: This requires that calculating and applying the delta (M + D_c = M_c) is an cheap/easy operation. If a client newly subscribes it must be initialized. This involves sending the current model M. So method 1 is always required. Think of playing chess as a concrete example: Subscribers send moves and want to see the latest chess board state. The server checks validity of the move and applies it to the chess board. The server can then send the updated chessboard (method 1) or just send the move (method 2) or send the delta (method 3): remove piece on field D4, put tower on field D8.

    Read the article

  • [PHP] Making a good singleton registry class structure which hold your objects

    - by Saif Bechan
    I am working on a web application in PHP. I have a singleton class called registry. This class will hold all the objects i need throughout my application, such as loader classes, template classes, database, classes, etc. When an object of the registry class is created I send it an array with the classes it need to load: // Create the registry $registry = registry::singleton(); // Store those core objects $registry->storeObjects(Array('session','db','page','template','errors')); In this example I only put some of the classes, to get the basic idea. Now I have some classes in the registry that use each other. For example the 'errors' object uses the 'page' object. Now I was wondering if it is a good practice to make an instance of the registry object in the errors object. Like this; class errors{ private $registry; public function __construct(){ $this->registry = registry::singleton(); } } So there is an instance of the registry object, inside an object of the registry object. This does not sound like a good idea to me. Anyone have a suggestion how to model such a thing?

    Read the article

  • Call from a singleton class to a function which in turn calls that class's method

    - by dare2be
    Hello, I am still looking for a way to phrase it properly (I'm not a native speaker...). So I have this class SQL which implements the singleton pattern (for obvious reasons) and I also have this function, checkUsr(), which queries the database using one of SQL's methods. Everything works fine as long as I don't call checkUsr() from within the SQL class. When I do so, the scripts just exits and a blank page is displayed - no errors are returned, no exception is thrown... What's happening? And how do I work around this problem? EDIT: class SQL { public static function singleton() { static $instance; if(!isset($instance)) $instance = new SQL; return $instance; } public function tryLoginAuthor( $login, $sha1 ) { (...) } } function checkUsr() { if (!isset($_SESSION['login']) || !isset($_SESSION['sha1'])) throw new Exception('Not logged in', 1); $SQL = SQL::singleton(); $res = $SQL->tryLoginAuthor($_SESSION['login'], $_SESSION['sha1']); if (!isset($res[0])) throw new Exception('Not logged in', 1); }

    Read the article

  • What is the best way to create a Singleton Webservice in PHP?

    - by ChronoFish
    Hello, We have a need to access a DB that only allows one connection at a time. This screams "singleton" to me. The catch of course is that the singleton connection will be exposed (either directly or indirectly) via a web-service (most probable a SOAP based web-service - located on a separate server from the calling app(s) ) - which means that there may be more than one app / instance attempting to connect to the singleton class. In PHP, what is the best way to create a global singleton or a web-service singleton? TIA

    Read the article

  • Advantages of Singleton Class over Static Class?

    Point 1)Singleton We can get the object of singleton and then pass to other methods.Static Class We can not pass static class to other methods as we pass objectsPoint 2) Singleton In future, it is easy to change the logic of of creating objects to some pooling mechanism. Static Class Very difficult to implement some pooling logic in case of static class. We would need to make that class as non-static and then make all the methods non-static methods, So entire your code needs to be changed.Point3:) Singleton Can Singletone class be inherited to subclass? Singleton class does not say any restriction of Inheritence. So we should be able to do this as long as subclass is also inheritence.There's nothing fundamentally wrong with subclassing a class that is intended to be a singleton. There are many reasons you might want to do it. and there are many ways to accomplish it. It depends on language you use.Static Class We can not inherit Static class to another Static class in C#. Think about it this way: you access static members via type name, like this: MyStaticType.MyStaticMember(); Were you to inherit from that class, you would have to access it via the new type name: MyNewType.MyStaticMember(); Thus, the new item bears no relationships to the original when used in code. There would be no way to take advantage of any inheritance relationship for things like polymorphism. span.fullpost {display:none;}

    Read the article

  • Advantages of Singleton Class over Static Class?

    Point 1) Singleton We can get the object of singleton and then pass to other methods. Static Class We can not pass static class to other methods as we pass objects Point 2) Singleton In future, it is easy to change the logic of of creating objects to some pooling mechanism. Static Class Very difficult to implement some pooling logic in case of static class. We would need to make that class as non-static and then make all the methods non-static methods, So entire your code needs to be changed. Point3:) Singleton Can Singletone class be inherited to subclass? Singleton class does not say any restriction of Inheritence. So we should be able to do this as long as subclass is also inheritence.There's nothing fundamentally wrong with subclassing a class that is intended to be a singleton. There are many reasons you might want to do it. and there are many ways to accomplish it. It depends on language you use. Static Class We can not inherit Static class to another Static class in C#. Think about it this way: you access static members via type name, like this: MyStaticType.MyStaticMember(); Were you to inherit from that class, you would have to access it via the new type name: MyNewType.MyStaticMember(); Thus, the new item bears no relationships to the original when used in code. There would be no way to take advantage of any inheritance relationship for things like polymorphism. span.fullpost {display:none;}

    Read the article

  • Singleton constructor question

    - by gillyb
    Hi, I created a Singleton class in c#, with a public property that I want to initialize when the Singleton is first called. This is the code I wrote : public class BL { private ISessionFactory _sessionFactory; public ISessionFactory SessionFactory { get { return _sessionFactory; } set { _sessionFactory = value; } } private BL() { SessionFactory = Dal.SessionFactory.CreateSessionFactory(); } private object thisLock = new object(); private BL _instance = null; public BL Instance { get { lock (thisLock) { if (_instance == null) { _instance = new BL(); } return _instance; } } } } As far as I know, when I address the Instance BL object in the BL class for the first time, it should load the constructor and that should initialize the SessionFactory object. But when I try : BL.Instance.SessionFactory.OpenSession(); I get a Null Reference Exception, and I see that SessionFactory is null... why?

    Read the article

  • Zend Framework - Database Table Singleton

    - by Sonny
    I have found myself doing this in my code to 'cache' the work done when instantiating my Zend_Db_Table models: if (Zend_Registry::isRegistered('x_table')) { $x_table = Zend_Registry::get('x_table'); } else { $x_table = new Default_Model_DbTable_X; Zend_Registry::set('x_table', $x_table); } It bothered me that this method isn't very DRY and it dawned on me today that a singleton pattern would probably be a better way to do this. Problem is, I've never written a singleton class. When I did some web searches, I found some offhand comments about Zend_Db_Table singletons, but no real examples. I already have meta-data caching configured. How do I make my Zend_Db_Table models singletons? Are there pitfalls or downsides?

    Read the article

  • Is extending a singleton class wrong?

    - by Anwar Shaikh
    I am creating a logger for an application. I am using a third party logger library. In which logger is implemented as singleton. I extended that logger class because I want to add some more static functions. In these static functions I internally use the instance (which is single) of Logger(which i inherited). I neither creates instance of MyLogger nor re-implemented the getInstance() method of super class. But I am still getting warnings like destructor of MyLogger can not be created as parent class (Loggger) destructor is not accessible. I want to know, I am I doing something wrong? Inheriting the singleton is wrong or should be avoided??

    Read the article

  • iphone singleton object synchronization

    - by user127091
    I'm working on an iphone app but this is probably a general question. I have a singleton Model class and there would be scenarios where multiple NSOperations (threads) would exist and work with the singleton object. If they all call the same method in this object, do i need to have some locking mechanism? Or can this method be executed only one at a time? I do not have a computer science background but my guess is that all threads would have their CALL to the same address (this method). Also can you please suggest a good beginner programming book that discusses general programming concepts. I don't have the brains for Knuth kinda books.

    Read the article

  • Singleton design potential leak

    - by iBrad Apps
    I have downloaded a library off of github and have noticed that in the main singleton of the library there is a possible leak in this bit of code: +(DDGameKitHelper*) sharedGameKitHelper { @synchronized(self) { if (instanceOfGameKitHelper == nil) { [[DDGameKitHelper alloc] init]; } return instanceOfGameKitHelper; } return nil; } Now obviously there is no release or autorelease anywhere so I must do it but how and in what way properly? I have looked at various Singleton design patterns on the Internet and they just assign, in this case, instanceOfGameKitHelper to the alloc and init line. Anyway how would I properly fix this? Thanks!

    Read the article

< Previous Page | 1 2 3 4 5 6 7 8 9 10 11 12  | Next Page >