dimanche 1 février 2015

In java, why is Observable's setChanged() protected?

If a java class, say classA, is extending classB, it cannot extend Observable. But if the setChanged() function were public, instead of protected, we can use composition to create an instance of Observable in classA.


I wonder what was the thought process behind making setChanged() protected.


In java, why is Observable's setChanged() protected?

If a java class, say classA, is extending classB, it cannot extend Observable. But if the setChanged() function were public, instead of protected, we can use composition to create an instance of Observable in classA.


I wonder what was the thought process behind making setChanged() protected.


thread safe singleton pattern design for review in c++

Below are two design of thread safe singleton design pattern . I would appreciate if anybody share their thought on both designs and point out if there is any issue. Design 1 follow the DCL (double check locking) and Design 2 based on assumption that local static variable can be initialized only once, which seems to be true for only for c++0x compiler. so in this case if same code is targeted to other compiler like c99 then what needs to change in current design. Please enlighten on this.


Design 1



#include <iostream>

#include<pthread.h>

using namespace std;

class Singleton
{

public:

static Singleton* getInstance();
void fun()
{
cout<<"singleton";
}
private:

Singleton() {std::cout << "Ctor\n";};

~Singleton() {std::cout << "Dtor\n";};

Singleton(const Singleton&);

const Singleton* operator=(const Singleton&);

static Singleton* instance;

static pthread_mutex_t myMutex;
};
Singleton* Singleton::getInstance()
{
if(instance==0)
{
pthread_mutex_lock(&myMutex);

if(instance==0)
{

cout<<"only need to come here once";

instance=new Singleton();
}

pthread_mutex_unlock(&myMutex);

}
return instance;
}

Singleton* Singleton::instance=0;

pthread_mutex_t Singleton::myMutex=PTHREAD_MUTEX_INITIALIZER;;

int main()
{

Singleton *s1 = Singleton::getInstance();
s1->fun();
return 0;

}


Design 2:



#include <iostream>

#include<pthread.h>

using namespace std;

class Singleton
{

public:

static Singleton& getInstance();
void fun()
{
cout<<"SIngleton";

}


private:

Singleton() {std::cout << "Ctor\n";};
~Singleton() {std::cout << "Dtor\n";};

Singleton(const Singleton&);
const Singleton* operator=(const Singleton&);
static pthread_mutex_t myMutex;

};



Singleton &Singleton::getInstance()
{
pthread_mutex_lock(&myMutex);
static Singleton instance;
pthread_mutex_unlock(&myMutex);
return instance;
}


pthread_mutex_t Singleton::myMutex=PTHREAD_MUTEX_INITIALIZER;;

int main()
{
Singleton &s1 = Singleton::getInstance();
s1.fun();
return 0;

}

thread safe singleton pattern design for review in c++

Below are two design of thread safe singleton design pattern . I would appreciate if anybody share their thought on both designs and point out if there is any issue. Design 1 follow the DCL (double check locking) and Design 2 based on assumption that local static variable can be initialized only once, which seems to be true for only for c++0x compiler. so in this case if same code is targeted to other compiler like c99 then what needs to change in current design. Please enlighten on this.


Design 1



#include <iostream>

#include<pthread.h>

using namespace std;

class Singleton
{

public:

static Singleton* getInstance();
void fun()
{
cout<<"singleton";
}
private:

Singleton() {std::cout << "Ctor\n";};

~Singleton() {std::cout << "Dtor\n";};

Singleton(const Singleton&);

const Singleton* operator=(const Singleton&);

static Singleton* instance;

static pthread_mutex_t myMutex;
};
Singleton* Singleton::getInstance()
{
if(instance==0)
{
pthread_mutex_lock(&myMutex);

if(instance==0)
{

cout<<"only need to come here once";

instance=new Singleton();
}

pthread_mutex_unlock(&myMutex);

}
return instance;
}

Singleton* Singleton::instance=0;

pthread_mutex_t Singleton::myMutex=PTHREAD_MUTEX_INITIALIZER;;

int main()
{

Singleton *s1 = Singleton::getInstance();
s1->fun();
return 0;

}


Design 2:



#include <iostream>

#include<pthread.h>

using namespace std;

class Singleton
{

public:

static Singleton& getInstance();
void fun()
{
cout<<"SIngleton";

}


private:

Singleton() {std::cout << "Ctor\n";};
~Singleton() {std::cout << "Dtor\n";};

Singleton(const Singleton&);
const Singleton* operator=(const Singleton&);
static pthread_mutex_t myMutex;

};



Singleton &Singleton::getInstance()
{
pthread_mutex_lock(&myMutex);
static Singleton instance;
pthread_mutex_unlock(&myMutex);
return instance;
}


pthread_mutex_t Singleton::myMutex=PTHREAD_MUTEX_INITIALIZER;;

int main()
{
Singleton &s1 = Singleton::getInstance();
s1.fun();
return 0;

}

Trying to understand the accepted answer - Thread Safe Map of Queues

Thread safe Map of Queues


The above question has a design implementation VERY similar to my own. I understand why it's not thread safe, but the accepted "pattern" is throwing me for a loop. I don't quite get how to implement it or what it does in relationship to the question.


Trying to understand the accepted answer - Thread Safe Map of Queues

Thread safe Map of Queues


The above question has a design implementation VERY similar to my own. I understand why it's not thread safe, but the accepted "pattern" is throwing me for a loop. I don't quite get how to implement it or what it does in relationship to the question.


samedi 31 janvier 2015

Eliminating re-implementation of methods from the built class in Builder, while keeping it with nested calling

I have a class that has to be checked for being set for all of the variables, while more variables that must be set could be added:



class A{
setVar1();
setVar2();
setVar3();
}


I have to check that all three sets were called before building it. How can I create a Builder that:


A. won't have to re-call the above methods again (like implementing inside builder another setVar1() setVar2() which will call those from A - this is a hell for future maintenance to me to implement each of those setters twice). So bad example for me here would be:



class ABuilder{
build(){...}
builderSetVar1(){...} //HELL NO!
builderSetVar2(){...} //NO NO NO!!!
...
}


B. Will check before building that all must have members are set (the user of the package, won't be able to have incomplete A)


C. Will allow nested one-line building of A (Builder...SetVar1(...)....SetVar2(...).build())


Is it even possible to achieve all of those three goals? I tried too many possibilities and in every solution


All in object oriented.