“Creator” pattern to configure inherited objects
up vote
0
down vote
favorite
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 |
up vote
0
down vote
favorite
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 at 11:41
annotation.fontSize = AppDefaults.fontSize
– Kamchatka
Nov 21 at 11:42
do both of TextAnnotationCreator class and LavelAnnotationCreator have fontSize property?
– Maxim Fedorov
Nov 21 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 at 12:25
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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 at 11:35
Kamchatka
2,14622761
2,14622761
What do you want to benefit from the initialization of LabelAnnotationCreator object?
– Maxim Fedorov
Nov 21 at 11:41
annotation.fontSize = AppDefaults.fontSize
– Kamchatka
Nov 21 at 11:42
do both of TextAnnotationCreator class and LavelAnnotationCreator have fontSize property?
– Maxim Fedorov
Nov 21 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 at 12:25
add a comment |
What do you want to benefit from the initialization of LabelAnnotationCreator object?
– Maxim Fedorov
Nov 21 at 11:41
annotation.fontSize = AppDefaults.fontSize
– Kamchatka
Nov 21 at 11:42
do both of TextAnnotationCreator class and LavelAnnotationCreator have fontSize property?
– Maxim Fedorov
Nov 21 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 at 12:25
What do you want to benefit from the initialization of LabelAnnotationCreator object?
– Maxim Fedorov
Nov 21 at 11:41
What do you want to benefit from the initialization of LabelAnnotationCreator object?
– Maxim Fedorov
Nov 21 at 11:41
annotation.fontSize = AppDefaults.fontSize
– Kamchatka
Nov 21 at 11:42
annotation.fontSize = AppDefaults.fontSize
– Kamchatka
Nov 21 at 11:42
do both of TextAnnotationCreator class and LavelAnnotationCreator have fontSize property?
– Maxim Fedorov
Nov 21 at 11:45
do both of TextAnnotationCreator class and LavelAnnotationCreator have fontSize property?
– Maxim Fedorov
Nov 21 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 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 at 12:25
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
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 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 at 14:13
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
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 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 at 14:13
add a comment |
up vote
0
down vote
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 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 at 14:13
add a comment |
up vote
0
down vote
up vote
0
down vote
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 at 14:12
answered Nov 21 at 13:47
elbraulio
3648
3648
These decorators are really elegant but unfortunately, I do need the LabelAnnotation and TextAnnotation as concrete types.
– Kamchatka
Nov 22 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 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 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 at 14:13
These decorators are really elegant but unfortunately, I do need the LabelAnnotation and TextAnnotation as concrete types.
– Kamchatka
Nov 22 at 13:53
These decorators are really elegant but unfortunately, I do need the LabelAnnotation and TextAnnotation as concrete types.
– Kamchatka
Nov 22 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 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 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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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 at 11:41
annotation.fontSize = AppDefaults.fontSize
– Kamchatka
Nov 21 at 11:42
do both of TextAnnotationCreator class and LavelAnnotationCreator have fontSize property?
– Maxim Fedorov
Nov 21 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 at 12:25