mercredi 28 novembre 2018

Design pattern to use when the application wants to know which concrete class it got

I have a class structure like the on below.

    class Technology;
    class BasicTechnology : public Technology;
    class BasicChildTechnology : public BasicTechnology;
    class ConcreteChildTechnology1 : public BasicChildTechnology;//concretechildtech1
    class ConcreteChildTechnology2 : public BasicChildTechnology;//concretechildtech2
    class ConcreteChildTechnology3 : public BasicChildTechnology;//concretechildtech3
...
    class ConcreteChildTechnologyN : public BasicChildTechnology;//concretechildtechN

The ConcreteChildTechnologyN/3/2/1 has an isValid(String selector) method, which is as shown below,

public isValid(String selector){
 return "concretechildtech1".Equals(selector);
}

Now in the client code is,

Technology tech = getTechnologyObject("concretechildtech1"); //tech becomes an instance of ConcreteChildTechnology1

How should I implement getTechnologyObject() in this case?.

Thought of using the abstract factory pattern, but doubtful of that. or even create a Facade and create the concrete child based on the input argument? The problem is, only the ConcreteChildTechnology1 knows whether the input string (concretechildtech1) belongs to him or not; via isValid() method.

Again if I start to create N objects every time to check the validity, that will cause and overhead, because 1)the system is running in a very low memory environment like mobile and tablets, 2)the number of instance creation is high, 10-100 per minute.

May be make the isValid() a static inline method and create object based on the reply from child objects?

Aucun commentaire:

Enregistrer un commentaire