mercredi 21 novembre 2018

"Creator" pattern to configure inherited objects

I have the following object structure:

class Annotation;
class LabelAnnotation: inherits Annotation;
class TextAnnotation: inherits LabelAnnotation;

I would like to use "creator" objects to do some initialization on these object (This initialization depends on external settings so I don't want to do it in the constructor of these objects.)

In particular, when creating a LabelAnnotation I would like to do:

fontSize = AppDefaults.fontSize

So I'm writing a "creator":

class LabelAnnotationCreator {
    LabelAnnotation create() {
        annotation = LabelAnnotation()
        annotation.fontSize = AppDefaults.fontSize
        return annotation;
    }
}

Now, I would like to create a TextAnnotationCreator. This is where I'm stuck: I can't use the LabelAnnotationCreator because it would create an instance of a LabelAnnotation, but on the other hand, I want to benefit from the initialization performed by the LabelAnnotationCreator.

class TextAnnotationCreator {
    TextAnnotation create() {
        annotation = TextAnnotation()
        // I'm stuck here:
        // can't do LabelAnnotationCreator().create()… ???
        return annotation;
    }
}

Obviously, this isn't the right pattern but I'm not sure how to find the correct one.

Thanks!

Aucun commentaire:

Enregistrer un commentaire