mardi 14 juin 2016

Passing arguments in a factory pattern

I am learning Domain Driven Design and I am in the 'naively confident' phase right now. DDD(the blue book) speaks about having a factory pattern for building aggregates. I tried using it for my application and in one of the scenarios it just doesn't seem right and I dont know how to proceed. Below is my case:

class CompanyFactory {
    public Company getCompany(Type type, Long numOfShares) {
        switch(type) {
            case PUBLIC:
                return new PublicCompany(numOfShares);
            case PRIVATE:
                return new PrivateCompany();
        }
}

Now the argument 'numOfShares' is relevant only for the PublicCompany. But when my type is 'PRIVATE', I still have to send the 'numOfShares' even though it wont be used.

I tried AbstractFactory but in that case each factory would create exactly one type of object and IMHO it misses the whole point of using a factory in the first place. Any pointers on how to do this will be great.

Aucun commentaire:

Enregistrer un commentaire