File tree Expand file tree Collapse file tree
pattern/src/com/premaseem Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11package com .premaseem ;
22
3+ import java .util .Scanner ;
4+
35/*
46@author: Aseem Jain
57@title: Design Patterns with Java 9
68@link: https://premaseem.wordpress.com/category/computers/design-patterns/
7- @copyright: 2018 Packt Publication
89*/
910public class Client {
1011 public static void main (String [] args ) {
11- System .out .println ("Singleton cook example " );
12+ System .out .println ("Strategy Design pattern - Mountaineering app " );
13+ System .out .println ("Lets decide the strategy to cross mountain crevasse " );
14+ Scanner scanner = new Scanner (System .in );
15+ System .out .println ("Enter the distance of crevasse ..." );
16+ int distance = scanner .nextInt ();
17+
18+ Context context = new Context ();
19+ CrevasseCrossingStrategy crossingStrategy = context .getCrevasseCrossingStrategy (distance );
20+ System .out .println ("Using Strategy : " + crossingStrategy .getClass ().getSimpleName ());
21+ crossingStrategy .crossCrevasse ();
22+
1223 }
1324}
Original file line number Diff line number Diff line change 1+ package com .premaseem ;
2+
3+ /*
4+ @author: Aseem Jain
5+ @title: Design Patterns with Java 9
6+ @link: https://premaseem.wordpress.com/category/computers/design-patterns/
7+ */
8+ public class Context {
9+
10+ CrevasseCrossingStrategy crevasseCrossingStrategy ;
11+
12+ public CrevasseCrossingStrategy getCrevasseCrossingStrategy (Integer distance ) {
13+ if (distance < 3 ){
14+ crevasseCrossingStrategy = new JumpCrossStrategy ();
15+ } else {
16+ crevasseCrossingStrategy = new RopeCrossStrategy ();
17+ }
18+ return crevasseCrossingStrategy ;
19+ }
20+ }
Original file line number Diff line number Diff line change 1+ package com .premaseem ;
2+
3+ /*
4+ @author: Aseem Jain
5+ @title: Design Patterns with Java 9
6+ @link: https://premaseem.wordpress.com/category/computers/design-patterns/
7+ */
8+ interface CrevasseCrossingStrategy {
9+ // Algorithm is collection of steps
10+ void crossCrevasse ();
11+ }
12+
13+ class JumpCrossStrategy implements CrevasseCrossingStrategy {
14+
15+ // Different algorithm or steps are defined in each sub class
16+ @ Override
17+ public void crossCrevasse () {
18+ System .out .println ("Move 10 steps back" );
19+ System .out .println ("Start running as fast as possible" );
20+ System .out .println ("Jump over the Crevasse" );
21+ System .out .println ("Stop and Anchor your self" );
22+ }
23+ }
24+
25+ class RopeCrossStrategy implements CrevasseCrossingStrategy {
26+
27+ // Different algorithm or steps are defined in each sub class
28+ @ Override
29+ public void crossCrevasse () {
30+ System .out .println ("Use Kernmantle rope" );
31+ System .out .println ("Tie rope with Anchor and through it on other side" );
32+ System .out .println ("Fix carabiner and fix it in rope" );
33+ System .out .println ("Cross using rope and untie yourself" );
34+ }
35+ }
36+
37+
You can’t perform that action at this time.
0 commit comments