Re: Factory pattern for classes

2020-08-10 Thread Martin via Digitalmars-d-learn
On Sunday, 9 August 2020 at 15:56:31 UTC, Ali Çehreli wrote: module deneme; import std.stdio; interface I { void methodName(); } ... I getClassById(uint id) { if (id == 0) { return cast(A)Object.factory("deneme.A"); } else if(id == 1) { return

Re: Factory pattern for classes

2020-08-10 Thread Ali Çehreli via Digitalmars-d-learn
On 8/10/20 8:38 AM, lexxn wrote: Btw is it possible to pass a property to the constructor, if I've one declared, in the factory? I'm talking about this piece cast(A)Object.factory("deneme.A") I think you mean "parameter". No, Object.factory creates the object with its default constructor.

Re: Factory pattern for classes

2020-08-10 Thread lexxn via Digitalmars-d-learn
On Sunday, 9 August 2020 at 15:56:31 UTC, Ali Çehreli wrote: On 8/9/20 7:27 AM, lexxn wrote: I getClassById(uint id) { if (id == 0) { return cast(A)Object.factory("deneme.A"); } else if(id == 1) { return cast(B)Object.factory("deneme.B"); } else { return

Re: Factory pattern for classes

2020-08-09 Thread lexxn via Digitalmars-d-learn
On Sunday, 9 August 2020 at 15:56:31 UTC, Ali Çehreli wrote: On 8/9/20 7:27 AM, lexxn wrote: module deneme; import std.stdio; interface I { void methodName(); } class A : I { void methodName() { writeln("A"); } } class B : I { void methodName() { writeln("B"); } } class C

Re: Factory pattern for classes

2020-08-09 Thread Steven Schveighoffer via Digitalmars-d-learn
On 8/9/20 10:27 AM, lexxn wrote: On Sunday, 9 August 2020 at 12:24:05 UTC, Steven Schveighoffer wrote: Object getClassById(uint id) {     if (id == 0) {     return new A;     } else if(id == 1) {     return new B;     } else {     return new C;     } } I assume that the correct

Re: Factory pattern for classes

2020-08-09 Thread Steven Schveighoffer via Digitalmars-d-learn
On 8/9/20 10:58 AM, lexxn wrote: On Sunday, 9 August 2020 at 12:24:05 UTC, Steven Schveighoffer wrote: If you know what your class is going to be, I'd just import the file that contains it and avoid the whole Object.factory deal. It's going to go away anyways. Not sure if it's a good idea

Re: Factory pattern for classes

2020-08-09 Thread Ali Çehreli via Digitalmars-d-learn
On 8/9/20 7:27 AM, lexxn wrote: > I assume that the correct syntax for the getClassById is > Object getClassById(uint id) { > if (id == 0) { > return new A(); > } else if (id == 1) { > return new B(); > } else { > return new C(); > } > } > or maybe

Re: Factory pattern for classes

2020-08-09 Thread lexxn via Digitalmars-d-learn
On Sunday, 9 August 2020 at 12:24:05 UTC, Steven Schveighoffer wrote: If you know what your class is going to be, I'd just import the file that contains it and avoid the whole Object.factory deal. It's going to go away anyways. Not sure if it's a good idea in my case. I'm going to be using

Re: Factory pattern for classes

2020-08-09 Thread lexxn via Digitalmars-d-learn
On Sunday, 9 August 2020 at 12:24:05 UTC, Steven Schveighoffer wrote: On 8/9/20 5:16 AM, lexxn wrote: I'm trying to get the factory pattern going with classes class A {} class B {} class C {} auto getClassById(uint id) {     if (id == 0) {     return cast(A)Object.factory("A");     }

Re: Factory pattern for classes

2020-08-09 Thread Steven Schveighoffer via Digitalmars-d-learn
On 8/9/20 5:16 AM, lexxn wrote: I'm trying to get the factory pattern going with classes class A {} class B {} class C {} auto getClassById(uint id) {     if (id == 0) {     return cast(A)Object.factory("A");     } else if(id == 1) {     return cast(B)Object.factory("B");     }