2014년 2월 27일 목요일

M o d e l ( PureMVC )

/*
 PureMVC Java port by Frederic Saunier <frederic.saunier@puremvc.org>
 Adapted from sources of thoses different authors :
  Donald Stinchfield <donald.stinchfield@puremvc.org>, et all
  Ima OpenSource <opensource@ima.eu>
  Anthony Quinault <anthony.quinault@puremvc.org>
 PureMVC - Copyright(c) 2006-10 Futurescale, Inc., Some rights reserved. 
 Your reuse is governed by the Creative Commons Attribution 3.0 License
*/
package org.puremvc.java.interfaces;

/**
 * The interface definition for a PureMVC Model.
 * 
 * <P>
 * In PureMVC, <code>IModel</code> implementors provide access to
 * <code>IProxy</code> objects by named lookup.
 * </P>
 * 
 * <P>
 * An <code>IModel</code> assumes these responsibilities:
 * </P>
 * 
 * <UL>
 * <LI>Maintain a cache of <code>IProxy</code> instances</LI>
 * <LI>Provide methods for registering, retrieving, and removing
 * <code>IProxy</code> instances</LI>
 * </UL>
 */
public interface IModel
{

/**
 * Register an <code>IProxy</code> instance with the <code>Model</code>.
 * 
 * @param proxy
 *            an object reference to be held by the <code>Model</code>.
 */
public void registerProxy( IProxy proxy );

/**
 * Retrieve an <code>IProxy</code> instance from the Model.
 * 
 * @param proxy
 * @return the <code>IProxy</code> instance previously registered with the
 *         given <code>proxyName</code>.
 */
public IProxy retrieveProxy( String proxy );

/**
 * Remove an <code>IProxy</code> instance from the Model.
 * 
 * @param proxy
 *            name of the <code>IProxy</code> instance to be removed.
 */
public IProxy removeProxy( String proxy );
/**
 * Check if a Proxy is registered
 * 
 * @param proxyName
 * @return whether a Proxy is currently registered with the given <code>proxyName</code>.
 */
public boolean hasProxy( String proxyName );
}








/*
 PureMVC Java port by Frederic Saunier <frederic.saunier@puremvc.org>
 Adapted from sources of thoses different authors :
  Donald Stinchfield <donald.stinchfield@puremvc.org>, et all
  Ima OpenSource <opensource@ima.eu>
  Anthony Quinault <anthony.quinault@puremvc.org>
 PureMVC - Copyright(c) 2006-10 Futurescale, Inc., Some rights reserved. 
 Your reuse is governed by the Creative Commons Attribution 3.0 License
*/
package org.puremvc.java.core;

import java.util.HashMap;
import java.util.Map;

import org.puremvc.java.interfaces.IModel;
import org.puremvc.java.interfaces.IProxy;

/**
 * A Singleton <code>IModel</code> implementation.
 * 
 * <P>
 * In PureMVC, the <code>Model</code> class provides
 * access to model objects (Proxies) by named lookup. 
 * 
 * <P>
 * The <code>Model</code> assumes these responsibilities:</P>
 * 
 * <UL>
 * <LI>Maintain a cache of <code>IProxy</code> instances.</LI>
 * <LI>Provide methods for registering, retrieving, and removing 
 * <code>IProxy</code> instances.</LI>
 * </UL>
 * 
 * <P>
 * Your application must register <code>IProxy</code> instances 
 * with the <code>Model</code>. Typically, you use an 
 * <code>ICommand</code> to create and register <code>IProxy</code> 
 * instances once the <code>Facade</code> has initialized the Core 
 * actors.</p>
 *
 * @see org.puremvc.java.patterns.proxy.Proxy Proxy
 * @see org.puremvc.java.interfaces.IProxy IProxy
 */
public class Model implements IModel
{

/**
 * Singleton instance
 */ 
protected static Model instance;

/**
 * Mapping of proxyNames to IProxy instances
 */
protected Map<String, IProxy> proxyMap;

/**
 * Constructor. 
 * 
 * <P>
 * This <code>IModel</code> implementation is a Multiton, 
 * so you should not call the constructor 
 * directly, but instead call the static Multiton 
 * Factory method <code>Model.getInstance( multitonKey )</code>
 * 
 * @throws Error Error if instance for this Multiton key instance has already been constructed
 * 
 */
protected Model()
{
instance = this;
proxyMap = new HashMap<String, IProxy>();
initializeModel();
}

/**
 * Initialize the Singleton <code>Model</code> instance.
 * 
 * <P>
 * Called automatically by the constructor, this is your opportunity to
 * initialize the Singleton instance in your subclass without overriding the
 * constructor.
 * </P>
 * 
 */
protected void initializeModel( )
{
}

/**
 * <code>Model</code> Multiton Factory method.
 * 
 * @return the instance for this Multiton key 
 */
public synchronized static Model getInstance()
{
if( instance == null)
instance = new Model();

return instance;
}

/**
 * Register an <code>Proxy</code> with the <code>Model</code>.
 * 
 * @param proxy
 *            an <code>Proxy</code> to be held by the <code>Model</code>.
 */
public void registerProxy( IProxy proxy )
{
this.proxyMap.put( proxy.getProxyName(), proxy );
proxy.onRegister();
}

/**
 * Remove an <code>Proxy</code> from the <code>Model</code>.
 * 
 * @param proxy
 *            name of the <code>Proxy</code> instance to be removed.
 */
public IProxy removeProxy( String proxy )
{
return (IProxy) this.proxyMap.remove( proxy );
}

/**
 * Retrieve an <code>Proxy</code> from the <code>Model</code>.
 * 
 * @param proxy
 * @return the <code>Proxy</code> instance previously registered with the
 *         given <code>proxyName</code>.
 */
public IProxy retrieveProxy( String proxy )
{
return (IProxy) this.proxyMap.get( proxy );
}
/**
 * Check if a Proxy is registered
 * 
 * @param proxyName
 * Name of the <code>Proxy</code> object to check for existance.
 *
 * @return
 * Whether a Proxy is currently registered with the given <code>proxyName</code>.
 */
public boolean hasProxy( String proxyName ) 
{
return proxyMap.containsKey( proxyName );
}
}

2014년 2월 26일 수요일

P r o x y ( PureMVC )



/*
 PureMVC Java port by Frederic Saunier <frederic.saunier@puremvc.org>

 Adapted from sources of thoses different authors :
  Donald Stinchfield <donald.stinchfield@puremvc.org>, et all
  Ima OpenSource <opensource@ima.eu>
  Anthony Quinault <anthony.quinault@puremvc.org>

 PureMVC - Copyright(c) 2006-10 Futurescale, Inc., Some rights reserved. 
 Your reuse is governed by the Creative Commons Attribution 3.0 License
*/
package org.puremvc.java.interfaces;

/**
 * The interface definition for a PureMVC Proxy.
 * 
 * <P>
 * In PureMVC, <code>IProxy</code> implementors assume these responsibilities:
 * </P>
 * <UL>
 * <LI>Implement a common method which returns the name of the Proxy.</LI>
 * </UL>
 * <P>
 * Additionally, <code>IProxy</code>s typically:
 * </P>
 * <UL>
 * <LI>Maintain references to one or more pieces of model data.</LI>
 * <LI>Provide methods for manipulating that data.</LI>
 * <LI>Generate <code>INotifications</code> when their model data changes.</LI>
 * <LI>Expose their name as a <code>public static const</code> called <code>NAME</code>, if they are not instantiated multiple times.</LI>
 * <LI>Encapsulate interaction with local or remote services used to fetch and
 * persist model data.</LI>
 * </UL>
 */
public interface IProxy extends INotifier
{

/**
 * Get the Proxy name
 * 
 * @return the Proxy instance name
 */
public String getProxyName( );
/**
 * Set the data object
 * 
 * @param data the data object
 */
public void setData( Object data );
/**
 * Get the data object
 * 
 * @return the data as type Object
 */
public Object getData(); 
/**
 * Called by the Model when the Proxy is registered
 */ 
public void onRegister();
/**
 * Called by the Model when the Proxy is removed
 */ 
public void onRemove( );

}







/*
 PureMVC Java port by Frederic Saunier <frederic.saunier@puremvc.org>

 Adapted from sources of thoses different authors :
  Donald Stinchfield <donald.stinchfield@puremvc.org>, et all
  Ima OpenSource <opensource@ima.eu>
  Anthony Quinault <anthony.quinault@puremvc.org>

 PureMVC - Copyright(c) 2006-10 Futurescale, Inc., Some rights reserved. 
 Your reuse is governed by the Creative Commons Attribution 3.0 License
*/
package org.puremvc.java.patterns.proxy;

import org.puremvc.java.interfaces.IProxy;
import org.puremvc.java.patterns.observer.Notifier;

/**
 * A base <code>IProxy</code> implementation.
 * 
 * <P>
 * In PureMVC, <code>Proxy</code> classes are used to manage parts of the
 * application's data model.
 * </P>
 * 
 * <P>
 * A <code>Proxy</code> might simply manage a reference to a local data
 * object, in which case interacting with it might involve setting and getting
 * of its data in synchronous fashion.
 * </P>
 * 
 * <P>
 * <code>Proxy</code> classes are also used to encapsulate the application's
 * interaction with remote services to save or retrieve data, in which case, we
 * adopt an asyncronous idiom; setting data (or calling a method) on the
 * <code>Proxy</code> and listening for a <code>Notification</code> to be
 * sent when the <code>Proxy</code> has retrieved the data from the service.
 * </P>
 * 
 * @see org.puremvc.java.core.Model Model
 */
public class Proxy extends Notifier implements IProxy
{

// the proxy name
private String proxyName = "Proxy";

// the data object
private Object data = null;

/**
 * Constructor
 * @param proxyName 
 * @param data 
 */
public Proxy( String proxyName, Object data )
{
if (proxyName != null) {
this.proxyName = proxyName;
}
if (data != null) {
this.data = data;
}
}
/**
 * Constructor
 * @param proxyName
 * Name of the <code>Proxy</code>
 */
public Proxy( String proxyName )
{
if( proxyName != null)
this.proxyName = proxyName;
}

/**
 * Get the proxy name
 * @return the proxy name
 */
public String getProxyName()
{
return this.proxyName;
}

/**
 * Set the data object
 * @param data 
 */
public void setData( Object data )
{
this.data = data;
}

/**
 * Get the data object
 * @return the data object
 */
public Object getData( )
{
return this.data;
}
/**
 * Called by the Model when the Proxy is registered
 */ 
public void onRegister(){}
/**
 * Called by the Model when the Proxy is removed
 */ 
public void onRemove(){}
}



2014년 2월 14일 금요일

Observer Pattern


package my.observer;

public class WeatherVO {

private int temperature;
private int humidity;
private int pressure;
public WeatherVO(){}
public WeatherVO(int temperature, int humidity, int pressure){
this.temperature = temperature;
this.humidity = humidity;
this.pressure = pressure;
}
public int getTemperature() {
return temperature;
}
public void setTemperature(int temperature) {
this.temperature = temperature;
}
public int getHumidity() {
return humidity;
}
public void setHumidity(int humidity) {
this.humidity = humidity;
}
public int getPressure() {
return pressure;
}
public void setPressure(int pressure) {
this.pressure = pressure;
}

}




package my.observer;

public interface DataSource {
public void print();

public WeatherVO getWeather();
}




package my.observer;

public class WeatherMeasure implements DataSource{
private WeatherVO weather;
public WeatherMeasure(WeatherVO weather){
this.weather = weather;
}
@Override
public WeatherVO getWeather(){
return weather;
}
@Override
public void print(){
String query = weather.getTemperature() + " / " + weather.getHumidity() + " / " + weather.getPressure();
System.out.println(query);
}
}








package my.observer;


public interface Notification {
public void addObserver(Observer observer);
public Observer removeObserver(Observer observer);
public void notifyObserver();
}





package my.observer;

import java.util.ArrayList;

public class WeatherData implements Notification{
protected ArrayList<Observer> observers = new ArrayList<>();
private DataSource dataSource;

public WeatherData(DataSource dataSource){
this.dataSource = dataSource;
}
@Override
public void addObserver(Observer observer) {
for(Observer o : observers){
if(o.equals(observer)) return;
}
observers.add(observer);
}

@Override
public Observer removeObserver(Observer observer) {
for(int i=0; i<observers.size(); ++i){
if(observers.get(i).equals(observer)){
observers.remove(i); break;
}
}
return null;
}

@Override
public void notifyObserver() {
for(int i=0; i<observers.size(); ++i) observers.get(i).update(dataSource);
}
}




package my.observer;

public interface Observer {

public void update(DataSource dataSource);
}




package my.observer;

public class ConditionView implements Observer{

@Override
public void update(DataSource dataSource) {
dataSource.print();
}
}



package my.observer;

public class ExplainView implements Observer{

@Override
public void update(DataSource dataSource) {
WeatherVO vo = dataSource.getWeather();
System.out.println("온도 : " + vo.getTemperature());
System.out.println("습도 : " + vo.getHumidity());
System.out.println("기압 : " + vo.getPressure());
}
}




package my.observer;

import org.junit.Test;

public class WeatherTest {

@Test
public void test(){
WeatherVO vo = new WeatherVO(5, 6, 7);
WeatherData weather = new WeatherData(new WeatherMeasure(vo));
Observer conditionView = new ConditionView();
Observer explainView = new ExplainView();
weather.addObserver(conditionView);
weather.addObserver(explainView);
weather.removeObserver(conditionView);
weather.notifyObserver();
}
}