News: The Java platform is the chosen language for our projects. May 22, 2012, 01:39:09 pm
Welcome, Guest. Please login or register. *

Singleton are evil
Pages: 1
  Send this topic  |  Print  

  Singleton are evil
Author Message
0 Members and 2 Guests are viewing this topic.
biffone
Jr. Member
**
Offline Offline

Posts: 23


View Profile
« on: August 27, 2007, 10:37:55 pm »

Hi

I've looked closer in the code and i see a lot of singletons ( ok not a lot but the some main components are singleton ). The evil part  with this singletons is that singleton link very hard modules. The modules are not independent.   
I can give you some examples : InputManager it is a singleton and ResourseManager it is a singleton.

So if i want to break the link between the other modules and InputManager or ResourseManager it will be impossible because this classes are link static ( using singleton ) .  The module must work with other modules using interface . In this case it will be more easy to decouple modules . In this case we can change very easy the modules ( also at runtime ) and it will be easy to unit test each module ( because  each module doesn't know the implementation of the module which work , it know only the interface of that module)

For example ResourseManager must be an interface . Every module that use ResourceManager must not know and must not be linked with the implementation of this interface .  When we will unit test (using Junit) the module we will inject a mock  ResourceManage used for that test.
An example . For example : I want to test   a module that use   a Sprite . In the actual coding the module it will ask the ResourceManager to get the sprite . The ResourceManager it will go to file and will read the sprite . But happening if i want to test my module in the case when the Sprite is not good( a parsing problem or an IOException ) . In this case it is impossible to test because my module it is link static to the resource manager. In normal case when i want to unit test ( Junit ) my case ( ResourceManager throw an IOException ) i will inject in my module a ResourceManager that throw every time  an IOException .

A class that used ResourceManager must look something like this


class A{

private ResourceManager resourceManager;

public void setResourceManager(ResourceManager resourceManager){
this.resourceManager=resourceManager;
}

................... the class code

}
 

public interface ResourceManager{


}

So the module doesn't tried to find the the implementation of the REsourceManager , the ResourceManager implementation will be injected in the module.

This technique it is called "Inversion of control" http://en.wikipedia.org/wiki/Inversion_of_Control
The catch of this technique is that : it is very nice but who will inject my modules that i need in my module. this it will be done by a bottom layer that it is used to inject component.


What do you think ?

I look at the singleton i think i see a mistake . The method getInstance() must be synchronized . if it is not synchronized  the Singleton are not thread safe  . 2 thread can create 2 instance


Logged
2playgames
OpenWar Project Founder
Administrator
Sr. Member
***
Offline Offline

Posts: 857


Busy busy busy busy busy


View Profile WWW
« Reply #1 on: August 28, 2007, 10:34:55 am »

Good information, I will try to figure something out. From next week on I will be following courses on OO programming at school , so I can further improve my understanding of this stuff.
Logged




biffone
Jr. Member
**
Offline Offline

Posts: 23


View Profile
« Reply #2 on: August 28, 2007, 11:33:43 am »

Ok

I will wait to you to implement the new architecture.

In this time i will try to implement a first version of the sound module .

Give me a sign when you have the signature of the interface .

Logged
biffone
Jr. Member
**
Offline Offline

Posts: 23


View Profile
« Reply #3 on: August 28, 2007, 11:49:21 am »

Some Inversion of control tutorial :

http://javaboutique.internet.com/tutorials/loose/
http://www.devx.com/Java/Article/27583
http://www.martinfowler.com/articles/injection.html#InversionOfControl
Logged

Pages: 1
  Send this topic  |  Print  
 

Jump to: