BAEL-1997 state design pattern in Java
This commit is contained in:
parent
ffbeb78ee1
commit
e872f156be
@ -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();
|
||||||
|
}
|
||||||
|
}
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
@ -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 + '}';
|
||||||
|
}
|
||||||
|
}
|
@ -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");
|
||||||
|
}
|
||||||
|
}
|
@ -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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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");
|
||||||
|
}
|
||||||
|
}
|
@ -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");
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
package com.baeldung.state.states;
|
||||||
|
|
||||||
|
import com.baeldung.state.context.TradeContext;
|
||||||
|
|
||||||
|
public interface TradeBehavior {
|
||||||
|
void trade(TradeContext ctx);
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user