473,385 Members | 1,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

single(ton) interface

Hi all,

I have had the following problem in the past and am interested in
what
various (other) solutions exist to solve this:

- Code requiring services dictate an interface to the services
required.
- It only requires one instance to provide those services.
- Services are only realized/bound at runtime.

A typical example of this is an AbstractFactory:

A library requires the services of the factory to build the range of
products
that it requires. How is concrete instance of this factory realized?

Conservatively we in the past used this mechanism:

class Factory
{
public:
static void setConcreteFactory( Factory& impl );
static Factory* get(); //or instance(), if you like
virtual std::auto_ptr<XmakeX() const = 0;
//etc...
protected:
~Factory();
private:
static Factory* impl_;
};

Looking at this, I realized it is not far from this:

//NOTE!!! Only one instance of implementation exist. IsA Singleton
interface!
class Factory
{
public:
static Factory* get(); //or instance(), if you like
virtual std::auto_ptr<XmakeX() const = 0;
//etc...

protected:
Factory( Factory& impl );
~Factory();

private:
static Factory* interface_;
};

Where get and the constructor would typically be implemented like
this:

Factory* Factory::get()
{
assert( interface_ ); //Could use less crude mechanism...
return interface_;
}

//We only want one instance
Factory::Factory( Factory & impl )
{
assert( interface_ == 0 ); //Only one instance :-)
interface_ = &impl;
}
Factory::~Factory(){ interface_ = 0; }

This (lets call it) pattern, is easy to generalize like this:

template <class T>
class SingleInterface
{
public:
static T* get();

protected:
SingleInterface( T& impl );
~SingleInterface();

private:
static SingleInterface* interface_;
};

class Factory : public SingleInterface<Factory>
{
public:
virtual std::auto_ptr<XmakeX() const = 0;

protected:
Factory( Factory& ); //Base encapsulate rules and indicates
intent?
~Factory();
};

What other solutions do you have to this, and what is your critic? :-)

Regards,

Werner

Jul 24 '07 #1
6 1300

werasm wrote:
template <class T>
class SingleInterface
{
public:
static T* get();

protected:
SingleInterface( T& impl );
~SingleInterface();

private:
static SingleInterface* interface_;
This should read...

static T* interface_;
};
Jul 24 '07 #2
werasm wrote:
werasm wrote:
>template <class T>
class SingleInterface
{
public:
static T* get();

protected:
SingleInterface( T& impl );
~SingleInterface();

private:
static SingleInterface* interface_;

This should read...

static T* interface_;
>};
I just noticed something here. If you use like this, you can get a crash
(forgot the name of this, its in the faq). You should rather do like this:

static T* interface_()
{
T* interface_;
return interface_;
}
Jul 24 '07 #3

anon wrote:

static T* interface_()
{
T* interface_;
return interface_;
Yes, this would certainly give you a crash :-). I think you should
reread.
}
Jul 24 '07 #4
On Jul 24, 1:49 pm, werasm <wer...@gmail.comwrote:
Hi all,

I have had the following problem in the past and am interested in
what various (other) solutions exist to solve this:

[Singleton and Factory stuff snipped]

Werner
"Modern C++ Design" by Andrei Alexandrescu has beautiful
implementations of both Factory and Singleton. The author has created
a library called Loki that contains the code he describes in this
book, so you could try googling for that. I recommend the book in any
case, it's truly mind-expanding stuff.

Jul 24 '07 #5
werasm wrote:
werasm wrote:
>template <class T>
class SingleInterface
{
public:
static T* get();

protected:
SingleInterface( T& impl );
~SingleInterface();

private:
static SingleInterface* interface_;

This should read...

static T* interface_;
>};

werasm wrote:
anon wrote:

>static T* interface_()
{
T* interface_;
return interface_;

Yes, this would certainly give you a crash :-). I think you should
reread.
>}
No, the point was to use interface_() instead of interface_

Using uninitialized interface_ will give you crash as well. What I was
looking was this:
http://www.parashift.com/c++-faq-lit...html#faq-10.12

I had a singleton template similar to your, when one day (when my
compiler was upgraded) my singleton started causing seg faults.
Jul 25 '07 #6

anon wrote:

No, the point was to use interface_() instead of interface_

Using uninitialized interface_ will give you crash as well. What I was
looking was this:
http://www.parashift.com/c++-faq-lit...html#faq-10.12
Accepted. In that case this is how it would look.
template <class T>
class SingleInterface
{
public:
static T* get();

protected:
SingleInterface( T& impl );
~SingleInterface();

private:
enum eAction{ eCreate, eDestroy, eGet };
static T* doAction( eAction, SingleInterface* impl = 0 );
};

template <class T>
SingleInterface<T>::SingleInterface( T& impl )
{
SingleInterface::doAction( eCreate, &impl );
}

template <class T>
SingleInterface<T>::~SingleInterface()
{
SingleInterface::doAction( eDestroy );
}

template <class T>
T* SingleInterface<T>::doAction( eAction action, SingleInterface*
impl )
{
static SingleInterface* inst( 0 );
if( action == eGet )
{
assert( inst );
return inst;
}
else if( action == eCreate )
{
assert( inst == 0 );
inst = impl;
return inst;
}
else
{
assert( action == eDestroy );
assert( inst );
inst = 0;
return inst;
}
}

template <class T>
T* SingleInterface<T>::get()
{
return SingleInterface::doAction( eGet );
}

BTW, I've only used AbstractFactory as an example of an interface
that
library code uses where it is not aware of the actual implementation
at compile time. There are many other examples (especially in
libraries)
where interfaces are not aware of there implementation, and they
specifically only want one instance of an implementation. I suppose
one could have a factory with a singleton creation policy. I'd look
into that. This does seem to solve the problem at hand though. Thanks
Anon for the FAQ. I have read it prior but in this case one needs the
instance
for previous comparison, hence my oversight.

Jul 25 '07 #7

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

Similar topics

5
by: Paul | last post by:
OK...I must be missing something...can someone tell me what I'm not doing properly... First, create an assembly with a single interface in VB.NET as follow Public Interface IDo Function...
3
by: Amit chaturvedi | last post by:
Dear Friends Please tell me following thing Que -: what is the main use of interface in .net Que -: What is difference between abstract class and interface Que -: How to make class in Object...
20
by: Ole Hanson | last post by:
I am accessing my database through an interface, to allow future substitution of the physical datastore - hence I would like to declare in my Interface that my DAL-objects implementing the...
6
by: John Salerno | last post by:
I understand how they work (basically), but I think maybe the examples I'm reading are too elementary to really show their value. Here's one from Programming C#: #region Using directives ...
1
by: theinvisibleGhost | last post by:
Is there an underlying interface anywhere that .NET form controls use to include the Enter, Leave, GotFocus, and LostFocus events? I have a problem where I've got a usercontrol which is being...
1
by: guhar1 | last post by:
A few design issues: 1. If I decide to implement an interface between my "business object layer" and my "data access layer", so as to protect the BOL from changes in DAL, do I provide ONE...
4
by: gabriel | last post by:
Hi Firstly i'd like to thank you for reading this and offer my appreciation for replies in advance. I've recently been writing a program which implements a user-defined API (Robocode to be...
0
by: YellowFin Announcements | last post by:
Introduction Usability and relevance have been identified as the major factors preventing mass adoption of Business Intelligence applications. What we have today are traditional BI tools that...
3
by: ewpatton | last post by:
Hi, I have written a DLL in C++.NET that contains a class inherited from a single abstraction: //------ IThing.h ------ #ifndef __ITHING_H__ #define __ITHING_H__ namespace DLLObject
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...

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.