MVCみたいなもの
戻る
javadocはこちら。
::::::::::::::
AbstractView.java
::::::::::::::
package com.ragtime.MVC;
/**
* $Id: SomethingLikeMVC.html,v 1.1 2009/06/22 16:11:57 kishi Exp kishi $
*/
public abstract class AbstractView implements View {
/** サービスオブジェクト */
protected Service service;
public void setService ( Service service ) {
this.service = service;
}
public abstract String getResponse();
}
::::::::::::::
Broker.java
::::::::::::::
package com.ragtime.MVC;
/**
* $Id: SomethingLikeMVC.html,v 1.1 2009/06/22 16:11:57 kishi Exp kishi $
* @author KISHI Yasuhiro
*/
public class Broker {
public String dispatch ( String command, Object param ) throws Exception {
System.out.println ( "in " + this.getClass().getName() + ": コマンド=" + command );
return Controller.getInstance().undertake ( command, param );
}
}
::::::::::::::
Client.java
::::::::::::::
package com.ragtime.MVC;
/**
* $Id: SomethingLikeMVC.html,v 1.1 2009/06/22 16:11:57 kishi Exp kishi $
* @author KISHI Yasuhiro
*/
public class Client {
public static void main ( String[] args ) throws Exception {
Broker broker = new Broker();
String response;
response = broker.dispatch ( "RETRIEVE_CATS", null );
System.out.println ( response );
response = broker.dispatch ( "RETRIEVE_DOGS", null );
System.out.println ( response );
}
}
::::::::::::::
ConfigParser.java
::::::::::::::
package com.ragtime.MVC;
/**
* $Id: SomethingLikeMVC.html,v 1.1 2009/06/22 16:11:57 kishi Exp kishi $
*/
import javax.xml.parsers.*;
import org.w3c.dom.*;
import java.io.*;
import java.util.*;
public class ConfigParser {
private final String CONFIG_FILE = "./etc/service-config.xml";
private Map association = null;
public ConfigParser() throws Exception {
association = new HashMap();
traverse();
}
public Map getAssociation() {
return association;
}
private void traverse( ) throws Exception {
//------------------------------------------------------------------------------------
// ドキュメントビルダーファクトリを生成
//------------------------------------------------------------------------------------
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//------------------------------------------------------------------------------------
// ドキュメントビルダーを生成
//------------------------------------------------------------------------------------
DocumentBuilder builder = factory.newDocumentBuilder();
//----------------------------------------------------------------
// パースを実行してDocumentオブジェクトを取得
//----------------------------------------------------------------
Document doc = builder.parse ( CONFIG_FILE );
System.out.println ( "【XMLのバージョン】" + doc.getXmlVersion() );
System.out.println ( "【文書のエンコーディング】" + doc.getXmlEncoding() );
Node root = doc.getDocumentElement();
doTraverse ( root );
}
private void doTraverse ( Node root ) {
// 子要素のリストを取得
Node childNode = root.getFirstChild();
while ( childNode != null ) {
String nodeName = childNode.getNodeName();
if ( "command".equals ( nodeName ) ) {
// System.out.println( nodeName );
parseCommand ( childNode );
}
childNode = childNode.getNextSibling();
}
}
private void parseCommand ( Node node ) {
String commandName = ( ( Element ) node ).getAttribute ( "name" );
System.out.println ( commandName );
// 子要素のリストを取得
Node childNode = node.getFirstChild();
String serviceName = null;
String viewName = null;
while ( childNode != null ) {
String nodeName = childNode.getNodeName();
// System.out.println( nodeName );
if ( "service".equals ( nodeName ) ) {
serviceName = childNode.getFirstChild().getNodeValue();
}
if ( "view".equals ( nodeName ) ) {
viewName = childNode.getFirstChild().getNodeValue();
}
childNode = childNode.getNextSibling();
}
association.put ( commandName, new String[] {serviceName.trim(), viewName.trim() } );
}
public static void main ( String[] args ) throws Exception {
ConfigParser parser = new ConfigParser();
Map association = parser.getAssociation();
Iterator iterator = association.keySet().iterator();
while ( iterator.hasNext() ) {
String command = ( String ) iterator.next();
String[] values = ( String[] ) association.get ( command );
System.out.println ( "command=" + command );
System.out.println ( "\tservice=" + values[ 0 ] + "\tview=" + values[ 1 ] );
}
}
}
::::::::::::::
Controller.java
::::::::::::::
package com.ragtime.MVC;
import java.util.*;
/**
* $Id: SomethingLikeMVC.html,v 1.1 2009/06/22 16:11:57 kishi Exp kishi $
*/
public class Controller {
/**
* インスタンス
*/
private static Controller uniqueInstance = null;
/**
* commandから導出されるserviceとviewの関係
*/
private static Map association = null;
/** シングルトンです */
private Controller() {
// Do nothing
}
/**
* インスタンスを取得する
* @return コントローラのインスタンス
*/
public static Controller getInstance() throws Exception {
if ( uniqueInstance == null ) {
uniqueInstance = new Controller();
// 設定内容を読み込む
ConfigParser parser = new ConfigParser();
association = parser.getAssociation();
}
return uniqueInstance;
}
/**
* ブローカから依頼された仕事を引き受ける
*/
public String undertake ( String command, Object param ) {
/**
Service service = new RetrieveCatsService();
service.execute( param );
View view = new RetrieveCatsView();
view.setService( service );
**/
/**
Service service;
View view;
String response = null;
try {
service = ( Service ) Class.forName( "RetrieveCatsService" ).newInstance();
service.execute( param );
view = ( View ) Class.forName( "RetrieveCatsView" ).newInstance();
view.setService( service );
response = view.getResponse();
} catch ( Exception e ) {
e.printStackTrace();
}
**/
Service service;
View view;
String response = null;
// コマンドから、serviceとviewの実装クラスを求める
String[] values = ( String[] ) association.get ( command );
String serviceName = values[ 0 ];
String viewName = values[ 1 ];
System.out.println ( "in " + this.getClass().getName() + ": service = " + serviceName
+ " view = " + viewName );
try {
service = ( Service ) Class.forName ( serviceName ).newInstance();
service.execute ( param );
view = ( View ) Class.forName ( viewName ).newInstance();
view.setService ( service );
response = view.getResponse();
} catch ( Exception e ) {
e.printStackTrace();
}
return response;
}
}
::::::::::::::
RetrieveCatsService.java
::::::::::::::
package com.ragtime.MVC;
/**
* $Id: SomethingLikeMVC.html,v 1.1 2009/06/22 16:11:57 kishi Exp kishi $
* @author KISHI Yasuhiro
*/
import java.util.*;
public class RetrieveCatsService implements Service {
private List catList = null;
public void execute ( Object param ) {
catList = new LinkedList();
catList.add ( "縞猫1号" );
catList.add ( "斑猫2号" );
catList.add ( "黒猫3号" );
}
public List getCatList() {
return catList;
}
}
::::::::::::::
RetrieveCatsView.java
::::::::::::::
package com.ragtime.MVC;
/**
* $Id: SomethingLikeMVC.html,v 1.1 2009/06/22 16:11:57 kishi Exp kishi $
*/
import java.util.*;
public class RetrieveCatsView extends AbstractView {
public String getResponse() {
RetrieveCatsService rcService = ( RetrieveCatsService ) service;
List catList = rcService.getCatList();
StringBuilder builder = new StringBuilder();
Iterator iterator = catList.iterator();
int i = 1;
while ( iterator.hasNext() ) {
builder.append ( i + ": " + iterator.next() + "\n" );
i++;
}
return builder.toString();
}
}
::::::::::::::
RetrieveDogsService.java
::::::::::::::
package com.ragtime.MVC;
/**
* $Id: SomethingLikeMVC.html,v 1.1 2009/06/22 16:11:57 kishi Exp kishi $
* @author KISHI Yasuhiro
*/
import java.util.*;
public class RetrieveDogsService implements Service {
private Map<String, Integer> dogMap = null;
public void execute ( Object param ) {
dogMap = new HashMap();
dogMap.put ( "ポチ" , 10 );
dogMap.put ( "白" , 30 );
dogMap.put ( "ハチ公", 25 );
dogMap.put ( "ジョン" , 40 );
}
public Map getDogMap() {
return dogMap;
}
}
::::::::::::::
RetrieveDogsView.java
::::::::::::::
package com.ragtime.MVC;
/**
* $Id: SomethingLikeMVC.html,v 1.1 2009/06/22 16:11:57 kishi Exp kishi $
*/
import java.util.*;
public class RetrieveDogsView extends AbstractView {
public String getResponse() {
RetrieveDogsService rdService = ( RetrieveDogsService ) service;
Map dogMap = rdService.getDogMap();
StringBuilder builder = new StringBuilder();
Iterator iterator = dogMap.keySet().iterator();
while ( iterator.hasNext() ) {
String name = ( String ) iterator.next();
Integer weight = ( Integer ) dogMap.get ( name );
builder.append ( name + "\t" + weight + "\n" );
}
return builder.toString();
}
}
::::::::::::::
Service.java
::::::::::::::
package com.ragtime.MVC;
/**
* $Id: SomethingLikeMVC.html,v 1.1 2009/06/22 16:11:57 kishi Exp kishi $
* @author KISHI Yasuhiro
*/
public interface Service {
/**
* <pre>実行する</pre>
* @param param サービスに引き渡されるパラメータ
*/
public void execute ( Object param );
}
::::::::::::::
View.java
::::::::::::::
package com.ragtime.MVC;
/**
* $Id: SomethingLikeMVC.html,v 1.1 2009/06/22 16:11:57 kishi Exp kishi $
* @author KISHI Yasuhiro
*/
public interface View {
public String getResponse();
public void setService ( Service service );
}
::::::::::::::
build.xml
::::::::::::::
<?xml version="1.0" encoding="Shift_JIS"?>
<!-- $Id -->
<project name="MVC" default="jar" basedir=".">
<property name="src.dir" value="src"/>
<property name="classes.dir" value="classes"/>
<property name="javadoc.dir" value="docs"/>
<property name="jar.name" value="MVC.jar"/>
<target name="jar" depends="compile">
<jar jarfile="${jar.name}" basedir="${classes.dir}"></jar>
</target>
<target name="compile">
<mkdir dir="${classes.dir}" />
<javac srcdir="${src.dir}" destdir="${classes.dir}"/>
</target>
<target name="javadoc">
<javadoc destdir="${javadoc.dir}">
<fileset dir="${src.dir}" includes="**/*.java"/>
</javadoc>
</target>
<target name="clean">
<delete dir="${classes.dir}" />
<delete dir="${javadoc.dir}" />
<delete file="${jar.name}" />
</target>
</project>
::::::::::::::
Client.sh
::::::::::::::
#!/bin/sh
# $Id: SomethingLikeMVC.html,v 1.1 2009/06/22 16:11:57 kishi Exp kishi $
java -cp ./MVC.jar MVC.com.ragtime.Client
::::::::::::::
indent.sh
::::::::::::::
#!/bin/sh
# $Id: SomethingLikeMVC.html,v 1.1 2009/06/22 16:11:57 kishi Exp kishi $
CURRENT_DIR=`pwd`
echo ${CURRENT_DIR}
for JAVA in `find . -name "*.java"` ; do
DIR=`dirname ${JAVA}`
cd ${DIR}
TARGET=`basename ${JAVA}`
astyle -j -a -P ${TARGET} && vu.sh ${TARGET} && rm -f *.orig *.bak
cd ${CURRENT_DIR}
done
$ tree
.
|-- Client.sh
|-- MVC.jar
|-- RCS
| |-- Client.sh,v
| |-- build.xml,v
| `-- indent.sh,v
|-- build.xml
|-- classes
| `-- MVC
| `-- com
| `-- ragtime
| |-- AbstractView.class
| |-- Broker.class
| |-- Client.class
| |-- ConfigParser.class
| |-- Controller.class
| |-- RetrieveCatsService.class
| |-- RetrieveCatsView.class
| |-- RetrieveDogsService.class
| |-- RetrieveDogsView.class
| |-- Service.class
| `-- View.class
|-- docs
| |-- MVC
| | `-- com
| | `-- ragtime
| | |-- AbstractView.html
| | |-- Broker.html
| | |-- Client.html
| | |-- ConfigParser.html
| | |-- Controller.html
| | |-- RetrieveCatsService.html
| | |-- RetrieveCatsView.html
| | |-- RetrieveDogsService.html
| | |-- RetrieveDogsView.html
| | |-- Service.html
| | |-- View.html
| | |-- package-frame.html
| | |-- package-summary.html
| | `-- package-tree.html
| |-- allclasses-frame.html
| |-- allclasses-noframe.html
| |-- constant-values.html
| |-- deprecated-list.html
| |-- help-doc.html
| |-- index-all.html
| |-- index.html
| |-- overview-tree.html
| |-- package-list
| |-- resources
| | `-- inherit.gif
| `-- stylesheet.css
|-- etc
| |-- RCS
| | `-- service-config.xml,v
| `-- service-config.xml
|-- indent.sh
`-- src
|-- AbstractView.java
|-- Broker.java
|-- Client.java
|-- ConfigParser.java
|-- Controller.java
|-- RCS
| |-- AbstractView.java,v
| |-- Borker.java,v
| |-- Broker.java,v
| |-- Client.java,v
| |-- ConfigParser.java,v
| |-- Controller.java,v
| |-- RetrieveCatsService.java,v
| |-- RetrieveCatsView.java,v
| |-- RetrieveDogsService.java,v
| |-- RetrieveDogsView.java,v
| |-- Service.java,v
| `-- View.java,v
|-- RetrieveCatsService.java
|-- RetrieveCatsView.java
|-- RetrieveDogsService.java
|-- RetrieveDogsView.java
|-- Service.java
`-- View.java
14 directories, 69 files
$ cat etc/service-config.xml | EscapeHTML.pl
<?xml version="1.0" encoding="Shift_JIS" ?>
<!-- $Id: SomethingLikeMVC.html,v 1.1 2009/06/22 16:11:57 kishi Exp kishi $ -->
<config>
<command name="RETRIEVE_CATS">
<service>
MVC.com.ragtime.RetrieveCatsService
</service>
<view>
MVC.com.ragtime.RetrieveCatsView
</view>
</command>
<command name="RETRIEVE_DOGS">
<service>
MVC.com.ragtime.RetrieveDogsService
</service>
<view>
MVC.com.ragtime.RetrieveDogsView
</view>
</command>
</config>
$ ./Client.sh
in MVC.com.ragtime.Broker: コマンド=RETRIEVE_CATS
【XMLのバージョン】1.0
【文書のエンコーディング】Shift_JIS
RETRIEVE_CATS
RETRIEVE_DOGS
in MVC.com.ragtime.Controller: service = MVC.com.ragtime.RetrieveCatsService view = MVC.com.ragtime.RetrieveCatsView
1: 縞猫1号
2: 斑猫2号
3: 黒猫3号
in MVC.com.ragtime.Broker: コマンド=RETRIEVE_DOGS
in MVC.com.ragtime.Controller: service = MVC.com.ragtime.RetrieveDogsService view = MVC.com.ragtime.RetrieveDogsView
白 30
ハチ公 25
ポチ 10
ジョン 40
戻る