[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-edu-devel
Subject:    Re: [kde-edu-devel] Kalzium-Rewrite
From:       Eva Brucherseifer <eva () kde ! org>
Date:       2003-12-08 8:57:12
[Download RAW message or body]

Hi Carsten,

for this type of problem you usually use the singleton pattern. A Singleton 
exists exactly one time. There are several ways to do it, the standard way 
(taken from the Pattern book [1]) looks like this:


class Data
{
	public:
		static Data* instance()
		{
			if (p_data==0)
				p_data = new Data();
			return p_data;
		}	
		
	protected:
		static Data* p_data;
}


in .cpp file:
static Data* Data::p_data=0;

then you can access the Data everywhere like this:

Data::instance()->myfunction();

There are some variants which return a reference instead of a pointer, so that 
the object cannot be deleted. In KDE the singleton is used in several places, 
though I don't have an example right now. I am sure Simon Hausmann or David 
Faure will be able to help you.

Greetings,
eva

[1] Gamma, et.al: "Design Patterns" or in German: "Entwurfsmuster"

On Sonntag, 7. Dezember 2003 14:50, Carsten Niehaus wrote:
> Hi all
>
> I am currently rewriting Kalzium. I have some reasons for this, eg:
>
> 1.) The current code is a mess
> 2.) For further extending I need a better (--> new) datastructure
> 3.) I want to offer more the just one PSE-widget. For that I need a better
> class-structure
>
> I plan to do something like this:
>
> typedef QValueList<Element*> EList;
> class Data
> {
> 	public:
> 		Data();
> 		~Data();
>
> 		/*
> 		 	This list contains 110 Elements. A Element has all the data
> 		 	Kalzium knows about the chemical elements.
> 		*/
> 		static EList ElementList;
>
> 		/*
> 			some methods to access the data
> 		*/
> };
>
>
>
>
> My problem is that I don't really know how to do that job. My code works so
> far, but only for a non-static EList. That means that the list is created a
> lot of times. I wanted to create a global const static Data-object so that
> I can access that object from everywhere and it is only created once. But
> that simply doesn't work. I would like to hear some input how I should
> handle this problem.
> In short: How do I create a datastructure which is easy to access and
> doesn't wast to much memory? Is DCOP a smart way? I never used it...
>
> There is already a blog online: http://www.kdedevelopers.org/node/view/214
>
> I hope to get some feedback,
> 	Carsten

_______________________________________________
kde-edu-devel mailing list
kde-edu-devel@kde.org
https://mail.kde.org/mailman/listinfo/kde-edu-devel
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic