| Thread overview | |||||
|---|---|---|---|---|---|
|
July 24, 2016 Singletons importing each other? | ||||
|---|---|---|---|---|
| ||||
Is there a way for singletons that import each other, use each other's functions?
Like i.e:
------
module sing1;
import sing2;
final class Singleton_1{
static this{
instance = new Singleton_1();
}
static Singleton_1 getInstance(){
return instance;
}
void foo(){
writeln("Sample");
}
void bar2(){
Singleton_2.getInstance().bar();
}
private:
static Singleton_1 instance;
}
-------
module sing2;
import sing1;
final class Singleton_2{
static this(){
instance = new Singleton_2();
}
static Singleton_2 getInstance(){
return instance;
}
void bar(){
Singleton_1.getInstance().foo();
}
private:
static Singleton_2 instance;
}
without triggering the "cycle between modules with constructors/destructors"?
| ||||
July 24, 2016 Re: Singletons importing each other? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jack | On Sunday, 24 July 2016 at 15:07:20 UTC, Jack wrote:
> Is there a way for singletons that import each other, use each other's functions?
>
> Like i.e:
>
> ------
> module sing1;
> import sing2;
> final class Singleton_1{
>
> static this{
> instance = new Singleton_1();
> }
>
> static Singleton_1 getInstance(){
> return instance;
> }
>
> void foo(){
> writeln("Sample");
> }
>
> void bar2(){
> Singleton_2.getInstance().bar();
> }
>
> private:
> static Singleton_1 instance;
> }
>
> -------
> module sing2;
> import sing1;
> final class Singleton_2{
>
> static this(){
> instance = new Singleton_2();
> }
>
> static Singleton_2 getInstance(){
> return instance;
> }
>
> void bar(){
> Singleton_1.getInstance().foo();
> }
>
> private:
> static Singleton_2 instance;
> }
>
> without triggering the "cycle between modules with constructors/destructors"?
- You can use a 3rd module that imports the two that "cycle".
- You can use another singleton implementation that doesn't rely on a static this. e.g a kind of "lazy factory" that control the uniquness.
| |||
July 25, 2016 Re: Singletons importing each other? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to lqjglkqjsg | On Sunday, 24 July 2016 at 15:38:13 UTC, lqjglkqjsg wrote:
> On Sunday, 24 July 2016 at 15:07:20 UTC, Jack wrote:
>> [...]
>
> - You can use a 3rd module that imports the two that "cycle".
> - You can use another singleton implementation that doesn't rely on a static this. e.g a kind of "lazy factory" that control the uniquness.
Oh ok. Thanks for the ideas. Cheers!
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply