Factory pattern for classes

Steven Schveighoffer schveiguy at gmail.com
Sun Aug 9 16:02:32 UTC 2020


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 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 I'm wrong.

If the constructor has no parameters, classes can be new'd without 
parentheses, so your modification results in identical code as my example.

-Steve


More information about the Digitalmars-d-learn mailing list