BAEL-1997 state design pattern in Java

This commit is contained in:
Denis 2018-07-27 21:27:38 +02:00
parent ffbeb78ee1
commit e872f156be
8 changed files with 121 additions and 0 deletions

View File

@ -0,0 +1,21 @@
package com.baeldung.state;
import com.baeldung.state.context.TradeContext;
import com.baeldung.state.states.DayTradeBehavior;
import com.baeldung.state.states.PositionTradeBehavior;
import com.baeldung.state.states.SwingTradeBehavior;
public class ExplicitStateDemo {
public static void main(String[] args) {
TradeContext tradeContext = new TradeContext(new SwingTradeBehavior());
tradeContext.trade();
tradeContext.setBehavior(new PositionTradeBehavior());
tradeContext.trade();
tradeContext.setBehavior(new DayTradeBehavior());
tradeContext.trade();
}
}

View File

@ -0,0 +1,15 @@
package com.baeldung.state;
import com.baeldung.state.context.TradeContext;
import com.baeldung.state.states.FallbackDayTradeBehavior;
public class InternalStateDemo {
public static void main(String[] args) {
TradeContext tradeContext = new TradeContext(new FallbackDayTradeBehavior());
tradeContext.trade();
tradeContext.trade();
tradeContext.trade();
}
}

View File

@ -0,0 +1,29 @@
package com.baeldung.state.context;
import com.baeldung.state.states.TradeBehavior;
public class TradeContext {
private TradeBehavior behavior;
public TradeContext(TradeBehavior behavior) {
this.behavior = behavior;
}
public TradeBehavior getBehavior() {
return behavior;
}
public void setBehavior(TradeBehavior behavior) {
this.behavior = behavior;
}
public void trade() {
behavior.trade(this);
}
@Override
public String toString() {
return "TradeContext{" + "behavior=" + behavior + '}';
}
}

View File

@ -0,0 +1,11 @@
package com.baeldung.state.states;
import com.baeldung.state.context.TradeContext;
public class DayTradeBehavior implements TradeBehavior {
@Override
public void trade(TradeContext ctx) {
System.out.println("Trading method: Day Trade");
}
}

View File

@ -0,0 +1,16 @@
package com.baeldung.state.states;
import com.baeldung.state.context.TradeContext;
public class FallbackDayTradeBehavior implements TradeBehavior {
@Override
public void trade(TradeContext ctx) {
System.out.println("Trading method: Day Trade");
if (Math.random() > 0.4) {
ctx.setBehavior(new SwingTradeBehavior());
System.out.println("Day trading would not go well, falling back to Swing Trade");
}
}
}

View File

@ -0,0 +1,11 @@
package com.baeldung.state.states;
import com.baeldung.state.context.TradeContext;
public class PositionTradeBehavior implements TradeBehavior {
@Override
public void trade(TradeContext ctx) {
System.out.println("Trading method: Position Trade");
}
}

View File

@ -0,0 +1,11 @@
package com.baeldung.state.states;
import com.baeldung.state.context.TradeContext;
public class SwingTradeBehavior implements TradeBehavior {
@Override
public void trade(TradeContext ctx) {
System.out.println("Trading method: Swing Trade");
}
}

View File

@ -0,0 +1,7 @@
package com.baeldung.state.states;
import com.baeldung.state.context.TradeContext;
public interface TradeBehavior {
void trade(TradeContext ctx);
}