Download Softwaretechnik ¨Uberblick I - FB3 - Uni Bremen
Transcript
Entwurfsmuster: Entwurfsmuster Singleton
Entwurfsmuster Singleton (Einzelstück)
Lösung (Muster für das Erzeugen von Instanzen):
Singleton
- static instance: Singleton
- static . . .
+ static Singleton getInstance()
- Singleton()
+ ...
Rainer Koschke (Uni Bremen)
Softwaretechnik
Sommersemester 2006
316 / 395
Entwurfsmuster: Entwurfsmuster Singleton
Code
1
public final
class Singleton {
2
private static Singleton instance ;
// s p e i c h e r t e i n z i g e I n s t a n z
3
4
5
p r i v a t e S i n g l e t o n ( ) {}
// kann von a u ß e r h a l b n i c h t b e n u t z t werden
6
7
8
// l i e f e r t e i n z i g e I n s t a n z
public synchronized s t a t i c Singleton getInstance () {
i f ( i n s t a n c e == n u l l ) { // l a z y i n s t a n t i a t i o n
i n s t a n c e = new S i n g l e t o n ( ) ;
}
return instance ;
}
9
10
11
12
13
14
15
16
}
Rainer Koschke (Uni Bremen)
Softwaretechnik
Sommersemester 2006
317 / 395