Merge pull request #281 from ankurkaushal/master
Reformat according to google style guide
This commit is contained in:
@ -7,13 +7,13 @@ package com.iluwatar.flux.action;
|
||||
*/
|
||||
public abstract class Action {
|
||||
|
||||
private ActionType type;
|
||||
|
||||
public Action(ActionType type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public ActionType getType() {
|
||||
return type;
|
||||
}
|
||||
private ActionType type;
|
||||
|
||||
public Action(ActionType type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public ActionType getType() {
|
||||
return type;
|
||||
}
|
||||
}
|
||||
|
@ -7,6 +7,6 @@ package com.iluwatar.flux.action;
|
||||
*/
|
||||
public enum ActionType {
|
||||
|
||||
MENU_ITEM_SELECTED, CONTENT_CHANGED;
|
||||
|
||||
MENU_ITEM_SELECTED, CONTENT_CHANGED;
|
||||
|
||||
}
|
||||
|
@ -6,17 +6,18 @@ package com.iluwatar.flux.action;
|
||||
*
|
||||
*/
|
||||
public enum Content {
|
||||
|
||||
PRODUCTS("Products - This page lists the company's products."), COMPANY("Company - This page displays information about the company.");
|
||||
|
||||
private String title;
|
||||
|
||||
private Content(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return title;
|
||||
}
|
||||
PRODUCTS("Products - This page lists the company's products."), COMPANY(
|
||||
"Company - This page displays information about the company.");
|
||||
|
||||
private String title;
|
||||
|
||||
private Content(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return title;
|
||||
}
|
||||
}
|
||||
|
@ -7,14 +7,14 @@ package com.iluwatar.flux.action;
|
||||
*/
|
||||
public class ContentAction extends Action {
|
||||
|
||||
private Content content;
|
||||
private Content content;
|
||||
|
||||
public ContentAction(Content content) {
|
||||
super(ActionType.CONTENT_CHANGED);
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
public Content getContent() {
|
||||
return content;
|
||||
}
|
||||
public ContentAction(Content content) {
|
||||
super(ActionType.CONTENT_CHANGED);
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
public Content getContent() {
|
||||
return content;
|
||||
}
|
||||
}
|
||||
|
@ -8,14 +8,14 @@ package com.iluwatar.flux.action;
|
||||
*/
|
||||
public class MenuAction extends Action {
|
||||
|
||||
private MenuItem menuItem;
|
||||
private MenuItem menuItem;
|
||||
|
||||
public MenuAction(MenuItem menuItem) {
|
||||
super(ActionType.MENU_ITEM_SELECTED);
|
||||
this.menuItem = menuItem;
|
||||
}
|
||||
|
||||
public MenuItem getMenuItem() {
|
||||
return menuItem;
|
||||
}
|
||||
public MenuAction(MenuItem menuItem) {
|
||||
super(ActionType.MENU_ITEM_SELECTED);
|
||||
this.menuItem = menuItem;
|
||||
}
|
||||
|
||||
public MenuItem getMenuItem() {
|
||||
return menuItem;
|
||||
}
|
||||
}
|
||||
|
@ -6,17 +6,17 @@ package com.iluwatar.flux.action;
|
||||
*
|
||||
*/
|
||||
public enum MenuItem {
|
||||
|
||||
HOME("Home"), PRODUCTS("Products"), COMPANY("Company");
|
||||
|
||||
private String title;
|
||||
|
||||
MenuItem(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return title;
|
||||
}
|
||||
HOME("Home"), PRODUCTS("Products"), COMPANY("Company");
|
||||
|
||||
private String title;
|
||||
|
||||
MenuItem(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return title;
|
||||
}
|
||||
}
|
||||
|
@ -9,43 +9,45 @@ import com.iluwatar.flux.view.MenuView;
|
||||
|
||||
/**
|
||||
*
|
||||
* Flux is the application architecture that Facebook uses for building client-side web
|
||||
* applications. Flux eschews MVC in favor of a unidirectional data flow. When a user interacts with
|
||||
* a React view, the view propagates an action through a central dispatcher, to the various stores that
|
||||
* hold the application's data and business logic, which updates all of the views that are affected.
|
||||
* Flux is the application architecture that Facebook uses for building client-side web
|
||||
* applications. Flux eschews MVC in favor of a unidirectional data flow. When a user interacts with
|
||||
* a React view, the view propagates an action through a central dispatcher, to the various stores
|
||||
* that hold the application's data and business logic, which updates all of the views that are
|
||||
* affected.
|
||||
* <p>
|
||||
* This example has two views: menu and content. They represent typical main menu and content area of
|
||||
* a web page. When menu item is clicked it triggers events through the dispatcher. The events are
|
||||
* received and handled by the stores updating their data as needed. The stores then notify the views
|
||||
* that they should rerender themselves.
|
||||
* This example has two views: menu and content. They represent typical main menu and content area
|
||||
* of a web page. When menu item is clicked it triggers events through the dispatcher. The events
|
||||
* are received and handled by the stores updating their data as needed. The stores then notify the
|
||||
* views that they should rerender themselves.
|
||||
* <p>
|
||||
* http://facebook.github.io/flux/docs/overview.html
|
||||
*
|
||||
*/
|
||||
public class App {
|
||||
|
||||
/**
|
||||
* Program entry point
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main( String[] args ) {
|
||||
|
||||
// initialize and wire the system
|
||||
MenuStore menuStore = new MenuStore();
|
||||
Dispatcher.getInstance().registerStore(menuStore);
|
||||
ContentStore contentStore = new ContentStore();
|
||||
Dispatcher.getInstance().registerStore(contentStore);
|
||||
MenuView menuView = new MenuView();
|
||||
menuStore.registerView(menuView);
|
||||
ContentView contentView = new ContentView();
|
||||
contentStore.registerView(contentView);
|
||||
|
||||
// render initial view
|
||||
menuView.render();
|
||||
contentView.render();
|
||||
|
||||
// user clicks another menu item
|
||||
// this triggers action dispatching and eventually causes views to render with new content
|
||||
menuView.itemClicked(MenuItem.COMPANY);
|
||||
}
|
||||
|
||||
/**
|
||||
* Program entry point
|
||||
*
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
||||
// initialize and wire the system
|
||||
MenuStore menuStore = new MenuStore();
|
||||
Dispatcher.getInstance().registerStore(menuStore);
|
||||
ContentStore contentStore = new ContentStore();
|
||||
Dispatcher.getInstance().registerStore(contentStore);
|
||||
MenuView menuView = new MenuView();
|
||||
menuStore.registerView(menuView);
|
||||
ContentView contentView = new ContentView();
|
||||
contentStore.registerView(contentView);
|
||||
|
||||
// render initial view
|
||||
menuView.render();
|
||||
contentView.render();
|
||||
|
||||
// user clicks another menu item
|
||||
// this triggers action dispatching and eventually causes views to render with new content
|
||||
menuView.itemClicked(MenuItem.COMPANY);
|
||||
}
|
||||
}
|
||||
|
@ -16,37 +16,36 @@ import com.iluwatar.flux.store.Store;
|
||||
*
|
||||
*/
|
||||
public class Dispatcher {
|
||||
|
||||
private static Dispatcher instance = new Dispatcher();
|
||||
|
||||
private List<Store> stores = new LinkedList<>();
|
||||
|
||||
private Dispatcher() {
|
||||
}
|
||||
|
||||
public static Dispatcher getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
public void registerStore(Store store) {
|
||||
stores.add(store);
|
||||
}
|
||||
|
||||
public void menuItemSelected(MenuItem menuItem) {
|
||||
dispatchAction(new MenuAction(menuItem));
|
||||
switch (menuItem) {
|
||||
case HOME:
|
||||
case PRODUCTS:
|
||||
default:
|
||||
dispatchAction(new ContentAction(Content.PRODUCTS));
|
||||
break;
|
||||
case COMPANY:
|
||||
dispatchAction(new ContentAction(Content.COMPANY));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void dispatchAction(Action action) {
|
||||
stores.stream().forEach((store) -> store.onAction(action));
|
||||
}
|
||||
private static Dispatcher instance = new Dispatcher();
|
||||
|
||||
private List<Store> stores = new LinkedList<>();
|
||||
|
||||
private Dispatcher() {}
|
||||
|
||||
public static Dispatcher getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
public void registerStore(Store store) {
|
||||
stores.add(store);
|
||||
}
|
||||
|
||||
public void menuItemSelected(MenuItem menuItem) {
|
||||
dispatchAction(new MenuAction(menuItem));
|
||||
switch (menuItem) {
|
||||
case HOME:
|
||||
case PRODUCTS:
|
||||
default:
|
||||
dispatchAction(new ContentAction(Content.PRODUCTS));
|
||||
break;
|
||||
case COMPANY:
|
||||
dispatchAction(new ContentAction(Content.COMPANY));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void dispatchAction(Action action) {
|
||||
stores.stream().forEach((store) -> store.onAction(action));
|
||||
}
|
||||
}
|
||||
|
@ -12,18 +12,18 @@ import com.iluwatar.flux.action.ContentAction;
|
||||
*/
|
||||
public class ContentStore extends Store {
|
||||
|
||||
private Content content = Content.PRODUCTS;
|
||||
private Content content = Content.PRODUCTS;
|
||||
|
||||
@Override
|
||||
public void onAction(Action action) {
|
||||
if (action.getType().equals(ActionType.CONTENT_CHANGED)) {
|
||||
ContentAction contentAction = (ContentAction) action;
|
||||
content = contentAction.getContent();
|
||||
notifyChange();
|
||||
}
|
||||
}
|
||||
|
||||
public Content getContent() {
|
||||
return content;
|
||||
}
|
||||
@Override
|
||||
public void onAction(Action action) {
|
||||
if (action.getType().equals(ActionType.CONTENT_CHANGED)) {
|
||||
ContentAction contentAction = (ContentAction) action;
|
||||
content = contentAction.getContent();
|
||||
notifyChange();
|
||||
}
|
||||
}
|
||||
|
||||
public Content getContent() {
|
||||
return content;
|
||||
}
|
||||
}
|
||||
|
@ -12,18 +12,18 @@ import com.iluwatar.flux.action.MenuItem;
|
||||
*/
|
||||
public class MenuStore extends Store {
|
||||
|
||||
private MenuItem selected = MenuItem.HOME;
|
||||
|
||||
@Override
|
||||
public void onAction(Action action) {
|
||||
if (action.getType().equals(ActionType.MENU_ITEM_SELECTED)) {
|
||||
MenuAction menuAction = (MenuAction) action;
|
||||
selected = menuAction.getMenuItem();
|
||||
notifyChange();
|
||||
}
|
||||
}
|
||||
|
||||
public MenuItem getSelected() {
|
||||
return selected;
|
||||
}
|
||||
private MenuItem selected = MenuItem.HOME;
|
||||
|
||||
@Override
|
||||
public void onAction(Action action) {
|
||||
if (action.getType().equals(ActionType.MENU_ITEM_SELECTED)) {
|
||||
MenuAction menuAction = (MenuAction) action;
|
||||
selected = menuAction.getMenuItem();
|
||||
notifyChange();
|
||||
}
|
||||
}
|
||||
|
||||
public MenuItem getSelected() {
|
||||
return selected;
|
||||
}
|
||||
}
|
||||
|
@ -12,16 +12,16 @@ import com.iluwatar.flux.view.View;
|
||||
*
|
||||
*/
|
||||
public abstract class Store {
|
||||
|
||||
private List<View> views = new LinkedList<>();
|
||||
|
||||
public abstract void onAction(Action action);
|
||||
|
||||
public void registerView(View view) {
|
||||
views.add(view);
|
||||
}
|
||||
|
||||
protected void notifyChange() {
|
||||
views.stream().forEach((view) -> view.storeChanged(this));
|
||||
}
|
||||
private List<View> views = new LinkedList<>();
|
||||
|
||||
public abstract void onAction(Action action);
|
||||
|
||||
public void registerView(View view) {
|
||||
views.add(view);
|
||||
}
|
||||
|
||||
protected void notifyChange() {
|
||||
views.stream().forEach((view) -> view.storeChanged(this));
|
||||
}
|
||||
}
|
||||
|
@ -11,17 +11,17 @@ import com.iluwatar.flux.store.Store;
|
||||
*/
|
||||
public class ContentView implements View {
|
||||
|
||||
private Content content = Content.PRODUCTS;
|
||||
private Content content = Content.PRODUCTS;
|
||||
|
||||
@Override
|
||||
public void storeChanged(Store store) {
|
||||
ContentStore contentStore = (ContentStore) store;
|
||||
content = contentStore.getContent();
|
||||
render();
|
||||
}
|
||||
@Override
|
||||
public void storeChanged(Store store) {
|
||||
ContentStore contentStore = (ContentStore) store;
|
||||
content = contentStore.getContent();
|
||||
render();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render() {
|
||||
System.out.println(content.toString());
|
||||
}
|
||||
@Override
|
||||
public void render() {
|
||||
System.out.println(content.toString());
|
||||
}
|
||||
}
|
||||
|
@ -12,27 +12,27 @@ import com.iluwatar.flux.store.Store;
|
||||
*/
|
||||
public class MenuView implements View {
|
||||
|
||||
private MenuItem selected = MenuItem.HOME;
|
||||
|
||||
@Override
|
||||
public void storeChanged(Store store) {
|
||||
MenuStore menuStore = (MenuStore) store;
|
||||
selected = menuStore.getSelected();
|
||||
render();
|
||||
}
|
||||
private MenuItem selected = MenuItem.HOME;
|
||||
|
||||
@Override
|
||||
public void render() {
|
||||
for (MenuItem item: MenuItem.values()) {
|
||||
if (selected.equals(item)) {
|
||||
System.out.println(String.format("* %s", item.toString()));
|
||||
} else {
|
||||
System.out.println(item.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void itemClicked(MenuItem item) {
|
||||
Dispatcher.getInstance().menuItemSelected(item);
|
||||
}
|
||||
@Override
|
||||
public void storeChanged(Store store) {
|
||||
MenuStore menuStore = (MenuStore) store;
|
||||
selected = menuStore.getSelected();
|
||||
render();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render() {
|
||||
for (MenuItem item : MenuItem.values()) {
|
||||
if (selected.equals(item)) {
|
||||
System.out.println(String.format("* %s", item.toString()));
|
||||
} else {
|
||||
System.out.println(item.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void itemClicked(MenuItem item) {
|
||||
Dispatcher.getInstance().menuItemSelected(item);
|
||||
}
|
||||
}
|
||||
|
@ -9,7 +9,7 @@ import com.iluwatar.flux.store.Store;
|
||||
*/
|
||||
public interface View {
|
||||
|
||||
public void storeChanged(Store store);
|
||||
public void storeChanged(Store store);
|
||||
|
||||
public void render();
|
||||
public void render();
|
||||
}
|
||||
|
@ -10,10 +10,10 @@ import com.iluwatar.flux.app.App;
|
||||
*
|
||||
*/
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user