Commented the example code.
This commit is contained in:
parent
61f0a2acf7
commit
523551a3fa
@ -1,6 +1,10 @@
|
|||||||
package com.iluwatar.action;
|
package com.iluwatar.action;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Action is the data payload dispatched to the stores when something happens.
|
||||||
|
*
|
||||||
|
*/
|
||||||
public abstract class Action {
|
public abstract class Action {
|
||||||
|
|
||||||
private ActionType type;
|
private ActionType type;
|
||||||
|
@ -1,5 +1,10 @@
|
|||||||
package com.iluwatar.action;
|
package com.iluwatar.action;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Types of actions.
|
||||||
|
*
|
||||||
|
*/
|
||||||
public enum ActionType {
|
public enum ActionType {
|
||||||
|
|
||||||
MENU_ITEM_SELECTED, CONTENT_CHANGED;
|
MENU_ITEM_SELECTED, CONTENT_CHANGED;
|
||||||
|
@ -1,5 +1,10 @@
|
|||||||
package com.iluwatar.action;
|
package com.iluwatar.action;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Content items.
|
||||||
|
*
|
||||||
|
*/
|
||||||
public enum Content {
|
public enum Content {
|
||||||
|
|
||||||
PRODUCTS("Products - This page lists the company's products."), COMPANY("Company - This page displays information about the company.");
|
PRODUCTS("Products - This page lists the company's products."), COMPANY("Company - This page displays information about the company.");
|
||||||
|
@ -1,6 +1,10 @@
|
|||||||
package com.iluwatar.action;
|
package com.iluwatar.action;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* ContentAction is a concrete action.
|
||||||
|
*
|
||||||
|
*/
|
||||||
public class ContentAction extends Action {
|
public class ContentAction extends Action {
|
||||||
|
|
||||||
private Content content;
|
private Content content;
|
||||||
|
@ -1,6 +1,11 @@
|
|||||||
package com.iluwatar.action;
|
package com.iluwatar.action;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* MenuAction is a concrete action.
|
||||||
|
*
|
||||||
|
*/
|
||||||
public class MenuAction extends Action {
|
public class MenuAction extends Action {
|
||||||
|
|
||||||
private MenuItem menuItem;
|
private MenuItem menuItem;
|
||||||
|
@ -1,5 +1,10 @@
|
|||||||
package com.iluwatar.action;
|
package com.iluwatar.action;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Menu items.
|
||||||
|
*
|
||||||
|
*/
|
||||||
public enum MenuItem {
|
public enum MenuItem {
|
||||||
|
|
||||||
HOME("Home"), PRODUCTS("Products"), COMPANY("Company");
|
HOME("Home"), PRODUCTS("Products"), COMPANY("Company");
|
||||||
|
@ -7,10 +7,25 @@ import com.iluwatar.store.MenuStore;
|
|||||||
import com.iluwatar.view.ContentView;
|
import com.iluwatar.view.ContentView;
|
||||||
import com.iluwatar.view.MenuView;
|
import com.iluwatar.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.
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*
|
||||||
|
* http://facebook.github.io/flux/docs/overview.html
|
||||||
|
*
|
||||||
|
*/
|
||||||
public class App {
|
public class App {
|
||||||
|
|
||||||
public static void main( String[] args ) {
|
public static void main( String[] args ) {
|
||||||
// initialize
|
// initialize and wire the system
|
||||||
MenuStore menuStore = new MenuStore();
|
MenuStore menuStore = new MenuStore();
|
||||||
Dispatcher.getInstance().registerStore(menuStore);
|
Dispatcher.getInstance().registerStore(menuStore);
|
||||||
ContentStore contentStore = new ContentStore();
|
ContentStore contentStore = new ContentStore();
|
||||||
|
@ -10,6 +10,11 @@ import com.iluwatar.action.MenuAction;
|
|||||||
import com.iluwatar.action.MenuItem;
|
import com.iluwatar.action.MenuItem;
|
||||||
import com.iluwatar.store.Store;
|
import com.iluwatar.store.Store;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Dispatcher sends Actions to registered Stores.
|
||||||
|
*
|
||||||
|
*/
|
||||||
public class Dispatcher {
|
public class Dispatcher {
|
||||||
|
|
||||||
private static Dispatcher instance = new Dispatcher();
|
private static Dispatcher instance = new Dispatcher();
|
||||||
|
@ -5,6 +5,11 @@ import com.iluwatar.action.ActionType;
|
|||||||
import com.iluwatar.action.Content;
|
import com.iluwatar.action.Content;
|
||||||
import com.iluwatar.action.ContentAction;
|
import com.iluwatar.action.ContentAction;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* ContentStore is a concrete store.
|
||||||
|
*
|
||||||
|
*/
|
||||||
public class ContentStore extends Store {
|
public class ContentStore extends Store {
|
||||||
|
|
||||||
private Content content = Content.PRODUCTS;
|
private Content content = Content.PRODUCTS;
|
||||||
|
@ -5,6 +5,11 @@ import com.iluwatar.action.ActionType;
|
|||||||
import com.iluwatar.action.MenuAction;
|
import com.iluwatar.action.MenuAction;
|
||||||
import com.iluwatar.action.MenuItem;
|
import com.iluwatar.action.MenuItem;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* MenuStore is a concrete store.
|
||||||
|
*
|
||||||
|
*/
|
||||||
public class MenuStore extends Store {
|
public class MenuStore extends Store {
|
||||||
|
|
||||||
private MenuItem selected = MenuItem.HOME;
|
private MenuItem selected = MenuItem.HOME;
|
||||||
|
@ -6,6 +6,11 @@ import java.util.List;
|
|||||||
import com.iluwatar.action.Action;
|
import com.iluwatar.action.Action;
|
||||||
import com.iluwatar.view.View;
|
import com.iluwatar.view.View;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Store is a data model.
|
||||||
|
*
|
||||||
|
*/
|
||||||
public abstract class Store {
|
public abstract class Store {
|
||||||
|
|
||||||
private List<View> views = new LinkedList<>();
|
private List<View> views = new LinkedList<>();
|
||||||
|
@ -4,6 +4,11 @@ import com.iluwatar.action.Content;
|
|||||||
import com.iluwatar.store.ContentStore;
|
import com.iluwatar.store.ContentStore;
|
||||||
import com.iluwatar.store.Store;
|
import com.iluwatar.store.Store;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* ContentView is a concrete view.
|
||||||
|
*
|
||||||
|
*/
|
||||||
public class ContentView implements View {
|
public class ContentView implements View {
|
||||||
|
|
||||||
private Content content = Content.PRODUCTS;
|
private Content content = Content.PRODUCTS;
|
||||||
|
@ -5,6 +5,11 @@ import com.iluwatar.dispatcher.Dispatcher;
|
|||||||
import com.iluwatar.store.MenuStore;
|
import com.iluwatar.store.MenuStore;
|
||||||
import com.iluwatar.store.Store;
|
import com.iluwatar.store.Store;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* MenuView is a concrete view.
|
||||||
|
*
|
||||||
|
*/
|
||||||
public class MenuView implements View {
|
public class MenuView implements View {
|
||||||
|
|
||||||
private MenuItem selected = MenuItem.HOME;
|
private MenuItem selected = MenuItem.HOME;
|
||||||
|
@ -2,6 +2,11 @@ package com.iluwatar.view;
|
|||||||
|
|
||||||
import com.iluwatar.store.Store;
|
import com.iluwatar.store.Store;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* Views define the representation of data.
|
||||||
|
*
|
||||||
|
*/
|
||||||
public interface View {
|
public interface View {
|
||||||
|
|
||||||
public void storeChanged(Store store);
|
public void storeChanged(Store store);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user