File tree Expand file tree Collapse file tree 10 files changed +123
-20
lines changed
flux/src/main/java/com/iluwatar Expand file tree Collapse file tree 10 files changed +123
-20
lines changed Original file line number Diff line number Diff line change 33public class App {
44
55 public static void main ( String [] args ) {
6- System .out .println ( "Hello World!" );
6+ // initialize
7+ MenuStore menuStore = new MenuStore ();
8+ Dispatcher .getInstance ().registerStore (menuStore );
9+ ContentStore contentStore = new ContentStore ();
10+ Dispatcher .getInstance ().registerStore (contentStore );
11+ MenuView menuView = new MenuView ();
12+ menuStore .registerView (menuView );
13+ ContentView contentView = new ContentView ();
14+ contentStore .registerView (contentView );
15+
16+ // render initial view
17+ menuView .render ();
18+ contentView .render ();
19+
20+ // user clicks another menu item
21+ // this triggers action dispatching and eventually causes views to render with new content
22+ menuView .itemClicked (MenuItem .COMPANY );
723 }
824}
Original file line number Diff line number Diff line change 22
33public enum Content {
44
5- PRODUCTS , COMPANY ;
5+ PRODUCTS ("Products - This page lists the company's products." ), COMPANY ("Company - This page displays information about the company." );
6+
7+ private String title ;
68
9+ private Content (String title ) {
10+ this .title = title ;
11+ }
12+
13+ @ Override
14+ public String toString () {
15+ return title ;
16+ }
717}
Original file line number Diff line number Diff line change 11package com .iluwatar ;
22
3- public class ContentStore implements Store {
3+ public class ContentStore extends Store {
4+
5+ private Content content = Content .PRODUCTS ;
46
57 @ Override
68 public void onAction (Action action ) {
7- // TODO Auto-generated method stub
8-
9+ if (action .getType ().equals (ActionType .CONTENT_CHANGED )) {
10+ ContentAction contentAction = (ContentAction ) action ;
11+ content = contentAction .getContent ();
12+ notifyChange ();
13+ }
14+ }
15+
16+ public Content getContent () {
17+ return content ;
918 }
10-
1119}
Original file line number Diff line number Diff line change 22
33public class ContentView implements View {
44
5+ private Content content = Content .PRODUCTS ;
6+
57 @ Override
68 public void storeChanged (Store store ) {
7- // TODO Auto-generated method stub
8-
9+ ContentStore contentStore = (ContentStore ) store ;
10+ content = contentStore .getContent ();
11+ render ();
912 }
1013
14+ @ Override
15+ public void render () {
16+ System .out .println (content .toString ());
17+ }
1118}
Original file line number Diff line number Diff line change @@ -12,7 +12,7 @@ public class Dispatcher {
1212 private Dispatcher () {
1313 }
1414
15- public Dispatcher getInstance () {
15+ public static Dispatcher getInstance () {
1616 return instance ;
1717 }
1818
@@ -21,6 +21,20 @@ public void registerStore(Store store) {
2121 }
2222
2323 public void menuItemSelected (MenuItem menuItem ) {
24-
24+ dispatchAction (new MenuAction (menuItem ));
25+ switch (menuItem ) {
26+ case HOME :
27+ case PRODUCTS :
28+ default :
29+ dispatchAction (new ContentAction (Content .PRODUCTS ));
30+ break ;
31+ case COMPANY :
32+ dispatchAction (new ContentAction (Content .COMPANY ));
33+ break ;
34+ }
35+ }
36+
37+ private void dispatchAction (Action action ) {
38+ stores .stream ().forEach ((store ) -> store .onAction (action ));
2539 }
2640}
Original file line number Diff line number Diff line change 22
33public enum MenuItem {
44
5- HOME , PRODUCTS , COMPANY ;
5+ HOME ("Home" ), PRODUCTS ("Products" ), COMPANY ("Company" );
6+
7+ private String title ;
68
9+ MenuItem (String title ) {
10+ this .title = title ;
11+ }
12+
13+ @ Override
14+ public String toString () {
15+ return title ;
16+ }
717}
Original file line number Diff line number Diff line change 11package com .iluwatar ;
22
3- public class MenuStore implements Store {
3+ public class MenuStore extends Store {
44
5+ private MenuItem selected = MenuItem .HOME ;
6+
57 @ Override
68 public void onAction (Action action ) {
7- // TODO Auto-generated method stub
8-
9+ if (action .getType ().equals (ActionType .MENU_ITEM_SELECTED )) {
10+ MenuAction menuAction = (MenuAction ) action ;
11+ selected = menuAction .getMenuItem ();
12+ notifyChange ();
13+ }
14+ }
15+
16+ public MenuItem getSelected () {
17+ return selected ;
918 }
10-
1119}
Original file line number Diff line number Diff line change 22
33public class MenuView implements View {
44
5+ private MenuItem selected = MenuItem .HOME ;
6+
57 @ Override
68 public void storeChanged (Store store ) {
7- // TODO Auto-generated method stub
8-
9+ MenuStore menuStore = (MenuStore ) store ;
10+ selected = menuStore .getSelected ();
11+ render ();
912 }
1013
14+ @ Override
15+ public void render () {
16+ for (MenuItem item : MenuItem .values ()) {
17+ if (selected .equals (item )) {
18+ System .out .println (String .format ("* %s" , item .toString ()));
19+ } else {
20+ System .out .println (item .toString ());
21+ }
22+ }
23+ }
24+
25+ public void itemClicked (MenuItem item ) {
26+ Dispatcher .getInstance ().menuItemSelected (item );
27+ }
1128}
Original file line number Diff line number Diff line change 11package com .iluwatar ;
22
3- public interface Store {
3+ import java .util .LinkedList ;
4+ import java .util .List ;
45
5- public void onAction ( Action action );
6+ public abstract class Store {
67
8+ private List <View > views = new LinkedList <>();
9+
10+ public abstract void onAction (Action action );
11+
12+ public void registerView (View view ) {
13+ views .add (view );
14+ }
15+
16+ protected void notifyChange () {
17+ views .stream ().forEach ((view ) -> view .storeChanged (this ));
18+ }
719}
Original file line number Diff line number Diff line change 33public interface View {
44
55 public void storeChanged (Store store );
6-
6+
7+ public void render ();
78}
You can’t perform that action at this time.
0 commit comments