Aktywne Wpisy
koronawirus +895
Dzisiaj jest zajebiście ważny dla mnie dzień, z dumą chciałbym się pochwalić że w wieku 28 lat dołączyłem do progu ponad 30k na ręke pomimo zajebistych przeciwności losu. 10 lat temu miałem wypadek, który uczynil mnie inwalidą do końca życia, kierowca auta wbił się we mnie na głównej drodze i połamał mi całą miednicę, ledwo uszedłem z życiem. 3 lata leżałem na plecach, wyrzucili mnie ze szkoły przez co musiałem kończyć liceum
zarzutkin +939
private static Singleton unikalnaInstancja;
private Singleton(){}
public static synchronized Singleton tworzenieInstancji(){
if(unikalnaInstancja=null){
unikalnaInstancja = new Singleton;
}
return unikalnaInstancja;
}
#programowanie #java #wzorceprojektowe #singleton
@plusiarz: no to singleton czy 3 obiekty, zdecyduj sie cumplu bo zakreciles mi w glowie ( ͡° ͜ʖ ͡°)
Komentarz usunięty przez autora
public
```**```
```**```
class
```**```
Singleton {
```**```
private
```**```
```**```
static
```**```
Singleton unikalnaInstancja;
```**```
private
```**```
```**```
static
```**```
```**```
int
```**```
singletonCount = 0;
```**```
private
```**```
Singleton(){}
```**```
public
```**```
```**```
static
```**```
```**```
synchronized
```**```
```**```
void
```**```
tworzenieInstancji(Singleton singletonRef) {
```**```
if
```**```
(singletonRef ==
```**```
null
```**```
) && (singletonCount <3) singletonRef =
```**```
new
```**```
Singleton();
``````
}
.
w każdym razie takie coś w mocno pokręconej implementacji ActiveRecord ;)
public
```**```
```**```
class
```**```
Singleton {
```**```
private
```**```
```**```
static
```**```
```**```
int
```**```
singletonCounter = 0;
```**```
private
```**```
Singleton(){}
```**```
public
```**```
```**```
static
```**```
```**```
synchronized
```**```
```**```
void
```**```
tworzenieInstancji(Singleton singletonRef) {
``````
```**```
if
```**```
(singletonRef ==
```**```
null
```**```
) && (singletonCount <3) {
singletonRef =
```**```
new
```**```
Singleton();
singletonCounter++;
}
}
``````
}
Gdzie getInstanceIterator() zwraca Iterator for kolekcji z wybranym przez nas Strategy dobioru kolejnych instancji.
@Pruderyjny: ciekawe ale dość skomplikowane rozwiązanie, również dzięki
Komentarz usunięty przez autora
public
```**```
```**```
class
```**```
SingletonTest {
``````
```**```
public
```**```
```**```
static
```**```
```**```
void
```**```
main(String[] args) {
Singleton s1 =
```**```
null
```**```
;
Singleton s2 =
```**```
null
```**```
;
Singleton s3 =
```**```
null
```**```
;
Singleton s4 =
```**```
null
```**```
;
Singleton.tworzenieInstancji(s1);
Singleton.tworzenieInstancji(s2);
Singleton.tworzenieInstancji(s3);
Singleton.tworzenieInstancji(s4);
}
}
``````
```**```
class
```**```
Singleton {
```**```
private
```**```
```**```
static
```**```
```**```
int
```**```
singletonCounter = 0;
```**```
private
http://wykop-code.appspot.com/