Using Composition and Implementation in state Design Pattern
I read this link enter link description here,
to learn State Desing Patern.
interface class:
public interface State {
void doAction();
}
onState class:
public class TVStartState implements State {
@Override
public void doAction() {
System.out.println("TV is turned ON");
}
}
offState:
public class TVStopState implements State {
@Override
public void doAction() {
System.out.println("TV is turned OFF");
}
}
TvContext Class:
public class TVContext implements State {
private State tvState;
public void setState(State state) {
this.tvState=state;
}
public State getState() {
return this.tvState;
}
@Override
public void doAction() {
this.tvState.doAction();
}
}
test Class :
public static void main(String args) {
TVContext context = new TVContext();
State tvStartState = new TVStartState();
State tvStopState = new TVStopState();
context.setState(tvStartState);
context.doAction();
context.setState(tvStopState);
context.doAction();
}
Now I have two questions :
1- why TVContext Class implements
State and has Composition
toghether ?
is a bug in OO ?
because for example Cat inherits from Animal class and has_a animal together (in this case).
2-If The final programmer in this TestClass pass context to context.setState()
instead of tvStartState or tvStopState , Program successfully compiles but error in run_time.
For the second question in State Design Pattern, instead of inheritance, same name method
can be used. but int Decoration Design Pattern not.
java oop design-patterns implements object-composition
add a comment |
I read this link enter link description here,
to learn State Desing Patern.
interface class:
public interface State {
void doAction();
}
onState class:
public class TVStartState implements State {
@Override
public void doAction() {
System.out.println("TV is turned ON");
}
}
offState:
public class TVStopState implements State {
@Override
public void doAction() {
System.out.println("TV is turned OFF");
}
}
TvContext Class:
public class TVContext implements State {
private State tvState;
public void setState(State state) {
this.tvState=state;
}
public State getState() {
return this.tvState;
}
@Override
public void doAction() {
this.tvState.doAction();
}
}
test Class :
public static void main(String args) {
TVContext context = new TVContext();
State tvStartState = new TVStartState();
State tvStopState = new TVStopState();
context.setState(tvStartState);
context.doAction();
context.setState(tvStopState);
context.doAction();
}
Now I have two questions :
1- why TVContext Class implements
State and has Composition
toghether ?
is a bug in OO ?
because for example Cat inherits from Animal class and has_a animal together (in this case).
2-If The final programmer in this TestClass pass context to context.setState()
instead of tvStartState or tvStopState , Program successfully compiles but error in run_time.
For the second question in State Design Pattern, instead of inheritance, same name method
can be used. but int Decoration Design Pattern not.
java oop design-patterns implements object-composition
what is the errorProgram successfully compiles but error in run_time.
? can you post it here?
– Deadpool
Jan 1 at 10:46
1
Please, no images containing text. If you have an error stack trace, copy and paste it into your question and format using the{}
button.
– RealSkeptic
Jan 1 at 11:18
add a comment |
I read this link enter link description here,
to learn State Desing Patern.
interface class:
public interface State {
void doAction();
}
onState class:
public class TVStartState implements State {
@Override
public void doAction() {
System.out.println("TV is turned ON");
}
}
offState:
public class TVStopState implements State {
@Override
public void doAction() {
System.out.println("TV is turned OFF");
}
}
TvContext Class:
public class TVContext implements State {
private State tvState;
public void setState(State state) {
this.tvState=state;
}
public State getState() {
return this.tvState;
}
@Override
public void doAction() {
this.tvState.doAction();
}
}
test Class :
public static void main(String args) {
TVContext context = new TVContext();
State tvStartState = new TVStartState();
State tvStopState = new TVStopState();
context.setState(tvStartState);
context.doAction();
context.setState(tvStopState);
context.doAction();
}
Now I have two questions :
1- why TVContext Class implements
State and has Composition
toghether ?
is a bug in OO ?
because for example Cat inherits from Animal class and has_a animal together (in this case).
2-If The final programmer in this TestClass pass context to context.setState()
instead of tvStartState or tvStopState , Program successfully compiles but error in run_time.
For the second question in State Design Pattern, instead of inheritance, same name method
can be used. but int Decoration Design Pattern not.
java oop design-patterns implements object-composition
I read this link enter link description here,
to learn State Desing Patern.
interface class:
public interface State {
void doAction();
}
onState class:
public class TVStartState implements State {
@Override
public void doAction() {
System.out.println("TV is turned ON");
}
}
offState:
public class TVStopState implements State {
@Override
public void doAction() {
System.out.println("TV is turned OFF");
}
}
TvContext Class:
public class TVContext implements State {
private State tvState;
public void setState(State state) {
this.tvState=state;
}
public State getState() {
return this.tvState;
}
@Override
public void doAction() {
this.tvState.doAction();
}
}
test Class :
public static void main(String args) {
TVContext context = new TVContext();
State tvStartState = new TVStartState();
State tvStopState = new TVStopState();
context.setState(tvStartState);
context.doAction();
context.setState(tvStopState);
context.doAction();
}
Now I have two questions :
1- why TVContext Class implements
State and has Composition
toghether ?
is a bug in OO ?
because for example Cat inherits from Animal class and has_a animal together (in this case).
2-If The final programmer in this TestClass pass context to context.setState()
instead of tvStartState or tvStopState , Program successfully compiles but error in run_time.
For the second question in State Design Pattern, instead of inheritance, same name method
can be used. but int Decoration Design Pattern not.
java oop design-patterns implements object-composition
java oop design-patterns implements object-composition
edited Jan 1 at 14:34
hamidreza haajhoseini
asked Jan 1 at 10:42


hamidreza haajhoseinihamidreza haajhoseini
196215
196215
what is the errorProgram successfully compiles but error in run_time.
? can you post it here?
– Deadpool
Jan 1 at 10:46
1
Please, no images containing text. If you have an error stack trace, copy and paste it into your question and format using the{}
button.
– RealSkeptic
Jan 1 at 11:18
add a comment |
what is the errorProgram successfully compiles but error in run_time.
? can you post it here?
– Deadpool
Jan 1 at 10:46
1
Please, no images containing text. If you have an error stack trace, copy and paste it into your question and format using the{}
button.
– RealSkeptic
Jan 1 at 11:18
what is the error
Program successfully compiles but error in run_time.
? can you post it here?– Deadpool
Jan 1 at 10:46
what is the error
Program successfully compiles but error in run_time.
? can you post it here?– Deadpool
Jan 1 at 10:46
1
1
Please, no images containing text. If you have an error stack trace, copy and paste it into your question and format using the
{}
button.– RealSkeptic
Jan 1 at 11:18
Please, no images containing text. If you have an error stack trace, copy and paste it into your question and format using the
{}
button.– RealSkeptic
Jan 1 at 11:18
add a comment |
1 Answer
1
active
oldest
votes
- Why
TVContext
class implementsState
and has composition together?
The example is incorrect, TVContext
should not implement interface State
. From the UML diagram for State Design Pattern we can see that class Context
only
compose an attribute that implements interface State
.
- If the final programmer in this TestClass pass context to
context.setState()
instead tvStartState or tvStopState , program successfully compiles but errors in run_time.
The reason it compiles is because context is implementing interface State
, but it fails in run-time with a java.lang.StackOverflowError
because function context.setState()
is recursively invoking itself with no exit condition. Removing the interface State
from TVContext
class fix this issue.
In Decorator Design Pattern the intent is to add new behavior to the Component class. That is why inheritance is used to add new methods to the Component.
In State Design Pattern the intent is to change the behavior of the Context class. For example if we implement it using inheritance with an abstract class instead of the interface the operation on the concrete state classes need to override the operation defined in the abstract class. That is why an interface makes more sense in this case.
good answer. "In State Design Pattern the intent is to change the behavior of the Context class" if its internal state was changed
– Andrew Tobilko
Jan 1 at 11:35
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%2f53994807%2fusing-composition-and-implementation-in-state-design-pattern%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
- Why
TVContext
class implementsState
and has composition together?
The example is incorrect, TVContext
should not implement interface State
. From the UML diagram for State Design Pattern we can see that class Context
only
compose an attribute that implements interface State
.
- If the final programmer in this TestClass pass context to
context.setState()
instead tvStartState or tvStopState , program successfully compiles but errors in run_time.
The reason it compiles is because context is implementing interface State
, but it fails in run-time with a java.lang.StackOverflowError
because function context.setState()
is recursively invoking itself with no exit condition. Removing the interface State
from TVContext
class fix this issue.
In Decorator Design Pattern the intent is to add new behavior to the Component class. That is why inheritance is used to add new methods to the Component.
In State Design Pattern the intent is to change the behavior of the Context class. For example if we implement it using inheritance with an abstract class instead of the interface the operation on the concrete state classes need to override the operation defined in the abstract class. That is why an interface makes more sense in this case.
good answer. "In State Design Pattern the intent is to change the behavior of the Context class" if its internal state was changed
– Andrew Tobilko
Jan 1 at 11:35
add a comment |
- Why
TVContext
class implementsState
and has composition together?
The example is incorrect, TVContext
should not implement interface State
. From the UML diagram for State Design Pattern we can see that class Context
only
compose an attribute that implements interface State
.
- If the final programmer in this TestClass pass context to
context.setState()
instead tvStartState or tvStopState , program successfully compiles but errors in run_time.
The reason it compiles is because context is implementing interface State
, but it fails in run-time with a java.lang.StackOverflowError
because function context.setState()
is recursively invoking itself with no exit condition. Removing the interface State
from TVContext
class fix this issue.
In Decorator Design Pattern the intent is to add new behavior to the Component class. That is why inheritance is used to add new methods to the Component.
In State Design Pattern the intent is to change the behavior of the Context class. For example if we implement it using inheritance with an abstract class instead of the interface the operation on the concrete state classes need to override the operation defined in the abstract class. That is why an interface makes more sense in this case.
good answer. "In State Design Pattern the intent is to change the behavior of the Context class" if its internal state was changed
– Andrew Tobilko
Jan 1 at 11:35
add a comment |
- Why
TVContext
class implementsState
and has composition together?
The example is incorrect, TVContext
should not implement interface State
. From the UML diagram for State Design Pattern we can see that class Context
only
compose an attribute that implements interface State
.
- If the final programmer in this TestClass pass context to
context.setState()
instead tvStartState or tvStopState , program successfully compiles but errors in run_time.
The reason it compiles is because context is implementing interface State
, but it fails in run-time with a java.lang.StackOverflowError
because function context.setState()
is recursively invoking itself with no exit condition. Removing the interface State
from TVContext
class fix this issue.
In Decorator Design Pattern the intent is to add new behavior to the Component class. That is why inheritance is used to add new methods to the Component.
In State Design Pattern the intent is to change the behavior of the Context class. For example if we implement it using inheritance with an abstract class instead of the interface the operation on the concrete state classes need to override the operation defined in the abstract class. That is why an interface makes more sense in this case.
- Why
TVContext
class implementsState
and has composition together?
The example is incorrect, TVContext
should not implement interface State
. From the UML diagram for State Design Pattern we can see that class Context
only
compose an attribute that implements interface State
.
- If the final programmer in this TestClass pass context to
context.setState()
instead tvStartState or tvStopState , program successfully compiles but errors in run_time.
The reason it compiles is because context is implementing interface State
, but it fails in run-time with a java.lang.StackOverflowError
because function context.setState()
is recursively invoking itself with no exit condition. Removing the interface State
from TVContext
class fix this issue.
In Decorator Design Pattern the intent is to add new behavior to the Component class. That is why inheritance is used to add new methods to the Component.
In State Design Pattern the intent is to change the behavior of the Context class. For example if we implement it using inheritance with an abstract class instead of the interface the operation on the concrete state classes need to override the operation defined in the abstract class. That is why an interface makes more sense in this case.
edited Jan 1 at 11:57


Pshemo
95.4k15131193
95.4k15131193
answered Jan 1 at 11:27


Victor OrtizVictor Ortiz
1665
1665
good answer. "In State Design Pattern the intent is to change the behavior of the Context class" if its internal state was changed
– Andrew Tobilko
Jan 1 at 11:35
add a comment |
good answer. "In State Design Pattern the intent is to change the behavior of the Context class" if its internal state was changed
– Andrew Tobilko
Jan 1 at 11:35
good answer. "In State Design Pattern the intent is to change the behavior of the Context class" if its internal state was changed
– Andrew Tobilko
Jan 1 at 11:35
good answer. "In State Design Pattern the intent is to change the behavior of the Context class" if its internal state was changed
– Andrew Tobilko
Jan 1 at 11:35
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%2f53994807%2fusing-composition-and-implementation-in-state-design-pattern%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 is the error
Program successfully compiles but error in run_time.
? can you post it here?– Deadpool
Jan 1 at 10:46
1
Please, no images containing text. If you have an error stack trace, copy and paste it into your question and format using the
{}
button.– RealSkeptic
Jan 1 at 11:18