“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!
java design-patterns prototype factory builder
add a comment |
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!
java design-patterns prototype factory builder
What do you want to benefit from the initialization of LabelAnnotationCreator object?
– Maxim Fedorov
Nov 21 '18 at 11:41
annotation.fontSize = AppDefaults.fontSize
– Kamchatka
Nov 21 '18 at 11:42
do both of TextAnnotationCreator class and LavelAnnotationCreator have fontSize property?
– Maxim Fedorov
Nov 21 '18 at 11:45
The fontSize property is not in the creator class, it's on the LabelAnnotation class. The TextAnnotation class has it as well because it inherits the LabelAnnotation class.
– Kamchatka
Nov 21 '18 at 12:25
add a comment |
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!
java design-patterns prototype factory builder
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!
java design-patterns prototype factory builder
java design-patterns prototype factory builder
asked Nov 21 '18 at 11:35
KamchatkaKamchatka
2,25623062
2,25623062
What do you want to benefit from the initialization of LabelAnnotationCreator object?
– Maxim Fedorov
Nov 21 '18 at 11:41
annotation.fontSize = AppDefaults.fontSize
– Kamchatka
Nov 21 '18 at 11:42
do both of TextAnnotationCreator class and LavelAnnotationCreator have fontSize property?
– Maxim Fedorov
Nov 21 '18 at 11:45
The fontSize property is not in the creator class, it's on the LabelAnnotation class. The TextAnnotation class has it as well because it inherits the LabelAnnotation class.
– Kamchatka
Nov 21 '18 at 12:25
add a comment |
What do you want to benefit from the initialization of LabelAnnotationCreator object?
– Maxim Fedorov
Nov 21 '18 at 11:41
annotation.fontSize = AppDefaults.fontSize
– Kamchatka
Nov 21 '18 at 11:42
do both of TextAnnotationCreator class and LavelAnnotationCreator have fontSize property?
– Maxim Fedorov
Nov 21 '18 at 11:45
The fontSize property is not in the creator class, it's on the LabelAnnotation class. The TextAnnotation class has it as well because it inherits the LabelAnnotation class.
– Kamchatka
Nov 21 '18 at 12:25
What do you want to benefit from the initialization of LabelAnnotationCreator object?
– Maxim Fedorov
Nov 21 '18 at 11:41
What do you want to benefit from the initialization of LabelAnnotationCreator object?
– Maxim Fedorov
Nov 21 '18 at 11:41
annotation.fontSize = AppDefaults.fontSize
– Kamchatka
Nov 21 '18 at 11:42
annotation.fontSize = AppDefaults.fontSize
– Kamchatka
Nov 21 '18 at 11:42
do both of TextAnnotationCreator class and LavelAnnotationCreator have fontSize property?
– Maxim Fedorov
Nov 21 '18 at 11:45
do both of TextAnnotationCreator class and LavelAnnotationCreator have fontSize property?
– Maxim Fedorov
Nov 21 '18 at 11:45
The fontSize property is not in the creator class, it's on the LabelAnnotation class. The TextAnnotation class has it as well because it inherits the LabelAnnotation class.
– Kamchatka
Nov 21 '18 at 12:25
The fontSize property is not in the creator class, it's on the LabelAnnotation class. The TextAnnotation class has it as well because it inherits the LabelAnnotation class.
– Kamchatka
Nov 21 '18 at 12:25
add a comment |
1 Answer
1
active
oldest
votes
what do you think about this:
class TextAnnotation {
private final int someOtherArgs;
private final int fontSize;
public TextAnnotation(LabelAnnotation labelAnnotation, int someOtherArgs) {
this(someOtherArgs, labelAnnotation.getFontSize());
}
public TextAnnotation(int someOtherArgs, int fontSize) {
this.someOtherArgs= someOtherArgs;
this.fontSize = fontSize;
}
}
create a constructor on TextAnnotation
that builds a object from a LabelAnnotation
configuration. Then you can use it like this:
TextAnnotation text = new TextAnnotation(someArgs,fontSize);
or using your creator
class TextAnnotationCreator {
TextAnnotation create() {
return
new TextAnnotation(
new LabelAnnotationCreator().create(),
someOtherArgs
);
}
}
These decorators are really elegant but unfortunately, I do need the LabelAnnotation and TextAnnotation as concrete types.
– Kamchatka
Nov 22 '18 at 13:53
I changed my answer. Now i suggest to use a constructor that builds an object from other object configuration
– elbraulio
Nov 22 '18 at 14:13
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53411221%2fcreator-pattern-to-configure-inherited-objects%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
what do you think about this:
class TextAnnotation {
private final int someOtherArgs;
private final int fontSize;
public TextAnnotation(LabelAnnotation labelAnnotation, int someOtherArgs) {
this(someOtherArgs, labelAnnotation.getFontSize());
}
public TextAnnotation(int someOtherArgs, int fontSize) {
this.someOtherArgs= someOtherArgs;
this.fontSize = fontSize;
}
}
create a constructor on TextAnnotation
that builds a object from a LabelAnnotation
configuration. Then you can use it like this:
TextAnnotation text = new TextAnnotation(someArgs,fontSize);
or using your creator
class TextAnnotationCreator {
TextAnnotation create() {
return
new TextAnnotation(
new LabelAnnotationCreator().create(),
someOtherArgs
);
}
}
These decorators are really elegant but unfortunately, I do need the LabelAnnotation and TextAnnotation as concrete types.
– Kamchatka
Nov 22 '18 at 13:53
I changed my answer. Now i suggest to use a constructor that builds an object from other object configuration
– elbraulio
Nov 22 '18 at 14:13
add a comment |
what do you think about this:
class TextAnnotation {
private final int someOtherArgs;
private final int fontSize;
public TextAnnotation(LabelAnnotation labelAnnotation, int someOtherArgs) {
this(someOtherArgs, labelAnnotation.getFontSize());
}
public TextAnnotation(int someOtherArgs, int fontSize) {
this.someOtherArgs= someOtherArgs;
this.fontSize = fontSize;
}
}
create a constructor on TextAnnotation
that builds a object from a LabelAnnotation
configuration. Then you can use it like this:
TextAnnotation text = new TextAnnotation(someArgs,fontSize);
or using your creator
class TextAnnotationCreator {
TextAnnotation create() {
return
new TextAnnotation(
new LabelAnnotationCreator().create(),
someOtherArgs
);
}
}
These decorators are really elegant but unfortunately, I do need the LabelAnnotation and TextAnnotation as concrete types.
– Kamchatka
Nov 22 '18 at 13:53
I changed my answer. Now i suggest to use a constructor that builds an object from other object configuration
– elbraulio
Nov 22 '18 at 14:13
add a comment |
what do you think about this:
class TextAnnotation {
private final int someOtherArgs;
private final int fontSize;
public TextAnnotation(LabelAnnotation labelAnnotation, int someOtherArgs) {
this(someOtherArgs, labelAnnotation.getFontSize());
}
public TextAnnotation(int someOtherArgs, int fontSize) {
this.someOtherArgs= someOtherArgs;
this.fontSize = fontSize;
}
}
create a constructor on TextAnnotation
that builds a object from a LabelAnnotation
configuration. Then you can use it like this:
TextAnnotation text = new TextAnnotation(someArgs,fontSize);
or using your creator
class TextAnnotationCreator {
TextAnnotation create() {
return
new TextAnnotation(
new LabelAnnotationCreator().create(),
someOtherArgs
);
}
}
what do you think about this:
class TextAnnotation {
private final int someOtherArgs;
private final int fontSize;
public TextAnnotation(LabelAnnotation labelAnnotation, int someOtherArgs) {
this(someOtherArgs, labelAnnotation.getFontSize());
}
public TextAnnotation(int someOtherArgs, int fontSize) {
this.someOtherArgs= someOtherArgs;
this.fontSize = fontSize;
}
}
create a constructor on TextAnnotation
that builds a object from a LabelAnnotation
configuration. Then you can use it like this:
TextAnnotation text = new TextAnnotation(someArgs,fontSize);
or using your creator
class TextAnnotationCreator {
TextAnnotation create() {
return
new TextAnnotation(
new LabelAnnotationCreator().create(),
someOtherArgs
);
}
}
edited Nov 22 '18 at 14:12
answered Nov 21 '18 at 13:47
elbraulioelbraulio
742214
742214
These decorators are really elegant but unfortunately, I do need the LabelAnnotation and TextAnnotation as concrete types.
– Kamchatka
Nov 22 '18 at 13:53
I changed my answer. Now i suggest to use a constructor that builds an object from other object configuration
– elbraulio
Nov 22 '18 at 14:13
add a comment |
These decorators are really elegant but unfortunately, I do need the LabelAnnotation and TextAnnotation as concrete types.
– Kamchatka
Nov 22 '18 at 13:53
I changed my answer. Now i suggest to use a constructor that builds an object from other object configuration
– elbraulio
Nov 22 '18 at 14:13
These decorators are really elegant but unfortunately, I do need the LabelAnnotation and TextAnnotation as concrete types.
– Kamchatka
Nov 22 '18 at 13:53
These decorators are really elegant but unfortunately, I do need the LabelAnnotation and TextAnnotation as concrete types.
– Kamchatka
Nov 22 '18 at 13:53
I changed my answer. Now i suggest to use a constructor that builds an object from other object configuration
– elbraulio
Nov 22 '18 at 14:13
I changed my answer. Now i suggest to use a constructor that builds an object from other object configuration
– elbraulio
Nov 22 '18 at 14:13
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53411221%2fcreator-pattern-to-configure-inherited-objects%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What do you want to benefit from the initialization of LabelAnnotationCreator object?
– Maxim Fedorov
Nov 21 '18 at 11:41
annotation.fontSize = AppDefaults.fontSize
– Kamchatka
Nov 21 '18 at 11:42
do both of TextAnnotationCreator class and LavelAnnotationCreator have fontSize property?
– Maxim Fedorov
Nov 21 '18 at 11:45
The fontSize property is not in the creator class, it's on the LabelAnnotation class. The TextAnnotation class has it as well because it inherits the LabelAnnotation class.
– Kamchatka
Nov 21 '18 at 12:25