Using Composition and Implementation in State Design Pattern

hamidreza haajhoseini :

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. enter image description here

For the second question in State Design Pattern, instead of inheritance, same name method can be used. but int Decoration Design Pattern not.

Victor Ortiz :
  1. Why TVContext class implements State 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.

UML diagram for State Design Pattern

  1. 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.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=86689&siteId=1