473,903 Members | 3,914 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Singleton Pattern & Serialization

Hi,

How would I make a class implementing a Singleton Pattern serializable.
My problem is the Singleton Pattern 'requires' the constructor to be Private
and serialization requires the constructor to be Public.

Thanks,

Michael
Nov 21 '05 #1
4 2008
You can make the constructor protected:
Imports System.Runtime. Serialization

....

Protected Sub New(ByVal info As SerializationIn fo, ByVal context As
StreamingContex t)
MyBase.New(info , context)
End Sub


Shariq
sh****@shariqkh an.com
"Michael Maes" <mi*****@merlot .com> wrote in message
news:69******** *************** ***********@mic rosoft.com...
Hi,

How would I make a class implementing a Singleton Pattern serializable.
My problem is the Singleton Pattern 'requires' the constructor to be
Private
and serialization requires the constructor to be Public.

Thanks,

Michael

Nov 21 '05 #2
Of course,

how silly of me not to think about that!

Thanks a 1.000 times Shariq.

Kind regards,

Michael

"Shariq Khan" wrote:
You can make the constructor protected:
Imports System.Runtime. Serialization

....

Protected Sub New(ByVal info As SerializationIn fo, ByVal context As
StreamingContex t)
MyBase.New(info , context)
End Sub


Shariq
sh****@shariqkh an.com
"Michael Maes" <mi*****@merlot .com> wrote in message
news:69******** *************** ***********@mic rosoft.com...
Hi,

How would I make a class implementing a Singleton Pattern serializable.
My problem is the Singleton Pattern 'requires' the constructor to be
Private
and serialization requires the constructor to be Public.

Thanks,

Michael


Nov 21 '05 #3
Michael,
See the second article of the following 3 part series on Serialization on
how to serialize & deserialize a Singleton. You need to create a
Serialization Helper class that implements IObjectRerefere nce. The class
being serialized needs to use SerializationIn fo.SetType to indicate that
this helper class is being written instead of the actual class. The helper
class's GetRealObject method returns the instance of the singleton....

The following three part article covers binary serialization in detail:
http://msdn.microsoft.com/msdnmag/issues/02/04/net/
http://msdn.microsoft.com/msdnmag/issues/02/07/net/
http://msdn.microsoft.com/msdnmag/issues/02/09/net/

Hope this helps
Jay
"Michael Maes" <mi*****@merlot .com> wrote in message
news:69******** *************** ***********@mic rosoft.com...
| Hi,
|
| How would I make a class implementing a Singleton Pattern serializable.
| My problem is the Singleton Pattern 'requires' the constructor to be
Private
| and serialization requires the constructor to be Public.
|
| Thanks,
|
| Michael
Nov 21 '05 #4
Hi Jay,

Thanks for the links.
I am indeed still facing issues re the Scope of the Constructor.
I think me problem lies in the fact that the Classes Inherit from a
Base-Class and use an XmlSerializer.

I'm going to try out the approach of the article (today).

Thanks,

Michael

"Jay B. Harlow [MVP - Outlook]" wrote:
Michael,
See the second article of the following 3 part series on Serialization on
how to serialize & deserialize a Singleton. You need to create a
Serialization Helper class that implements IObjectRerefere nce. The class
being serialized needs to use SerializationIn fo.SetType to indicate that
this helper class is being written instead of the actual class. The helper
class's GetRealObject method returns the instance of the singleton....

The following three part article covers binary serialization in detail:
http://msdn.microsoft.com/msdnmag/issues/02/04/net/
http://msdn.microsoft.com/msdnmag/issues/02/07/net/
http://msdn.microsoft.com/msdnmag/issues/02/09/net/

Hope this helps
Jay
"Michael Maes" <mi*****@merlot .com> wrote in message
news:69******** *************** ***********@mic rosoft.com...
| Hi,
|
| How would I make a class implementing a Singleton Pattern serializable.
| My problem is the Singleton Pattern 'requires' the constructor to be
Private
| and serialization requires the constructor to be Public.
|
| Thanks,
|
| Michael

Nov 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3481
by: Alicia Roberts | last post by:
Hello everyone, I have been researching the Singleton Pattern. Since the singleton pattern uses a private constructor which in turn reduces extendability, if you make the Singleton Polymorphic what sort of problems/issues should be considered? Also, I see that a singleton needs to be set up with certain data such as file name, database URL etc. What issues are involved in this, and how would you do this? If someone knows about the...
3
2508
by: Alicia Roberts | last post by:
Hello everyone, I have been researching the Singleton Pattern. Since the singleton pattern uses a private constructor which in turn reduces extendability, if you make the Singleton Polymorphic what sort of problems/issues should be considered? Also, I see that a singleton needs to be set up with certain data such as file name, database URL etc. What issues are involved in this, and how would you do this? If someone knows about the...
11
2179
by: Daniel Billingsley | last post by:
Let's say I'm writing a business app and I want there to be only one instance of the Customer object for each particular customer (representing a database record) being edited. Would it be possible to extend the Singleton pattern to handle this? Assuming that my Customer class follows the Singleton pattern (particularly Skeet's 4th version) I'm thinking if I add private static SomeCollectionType customers;
21
2477
by: Sharon | last post by:
I wish to build a framework for our developers that will include a singleton pattern. But it can not be a base class because it has a private constructor and therefore can be inherit. I thought maybe a Template can be use for that, but C# does not support Templates (will be C# generics in mid 2005). Does anyone have a solution on how the singleton pattern can be written, in C#, as a framework/ infrastructure class, so users can use this...
18
6699
by: David | last post by:
Hello, I'm writing a C# class library that may be called by non-.Net applications. The main class of this class libray is a ServicedComponent so that non-.Net application can call this class as a standard COM+ component. Now, I want that class to be a singleton. Once loaded, the values moslty never change. I tried using this pattern: public sealed class Singleton {
2
1809
by: Chris Murphy via DotNetMonster.com | last post by:
Hey guys, I've been hitting a brick wall with a problem I've come accross in developing an application. Background: The application uses one primary class that I'm trying to implement with the singleton pattern in mind. The design pattern seems to be working properly (complex and simple variations of it) Serialzation/Deserialzation seems to work okay. I run into a problem when I attempt to load the file back into the
14
5159
by: rob | last post by:
I have two different classes singleton1 and singleton2 that both are singletons. Both classes implement the same functions. Therefore, I would like to have an interface containing these functions. The problem is that an interface does not allow static functions and an abstract class does not work in my case because multiple inheritence is not allowed. So how do I go after doing this? Thanks
3
18272
weaknessforcats
by: weaknessforcats | last post by:
Design Pattern: The Singleton Overview Use the Singleton Design Pattern when you want to have only one instance of a class. This single instance must have a single global point of access. That is, regardless of where the object is hidden, everyone needs access to it. The global point of access is the object's Instance() method. Individual users need to be prevented from creating their own instances of the Singleton.
6
1220
by: Monty | last post by:
Hello, I have a singleton settings class (.Net 2.0 framework) that I serialize/deserialize to XML. On my settings class is a shared list of integers. If I have two numbers in my list and I deserialize my class successive times, the count of integers in my list grows by 2 each time when I would expect it to remain at 2. When I run this code (below) and click the button multiple times, my immediate window shows the following results: 2 4...
0
9999
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11282
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10873
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10983
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10500
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9684
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6088
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4308
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3324
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.