📄 请教另一个模式问题.txt
字号:
请教另一个模式问题
http://www.shecn.com/best/g17/g1001.htm
--------------------------------------------------------------------------------
各位大侠好!
我遇到了另一个问题。
我想在一个图上显示多条线,每条线的数据源不同,有的来自文件,有的来自
数据库或某个服务器。
这时图与数据源 应该用什么模式?
不同的数据源 实现同一个接口好,
还是用一个数据原类,再对这个数据原类配置, 来得到不同的数据原?
如果用后一种方法?那应该是什么模式?
01/10/12 09:00 酷帖! 臭帖! 回复
酷帖评价: 臭帖评价:
返回页首
it365.com 我的理解
--------------------------------------------------------------------------------
图与数据源用Observer比较好!
目标对象Notify
后一个嘛:
看看JAVA的蓝图:
http://java.sun.com/j2ee/blueprints/design_patterns/data_access_object/index.html
01/10/12 15:50 酷帖! 臭帖! 回复
酷帖评价: 臭帖评价:
返回页首
slovenboy 回复: 我的理解
--------------------------------------------------------------------------------
我使用了Observer模式,
但只是一个数据源对应多个图。
后来发现同时存在一个图对应多个数据源的问题!
我看了一下《design patterns》,有关于他的描速,但不太明白!
不知是否有关于design patterns 的java 实例???
后一个我正在看, 谢谢!
01/10/12 23:11 酷帖! 臭帖! 回复
酷帖评价: 臭帖评价:
返回页首
froghe 回复: 请教另一个模式问题!
--------------------------------------------------------------------------------
1. definitely needs IO factory to
create IO objects from different source.
2. Bridge could decouple the IO from main system.
3. The multiple-model - multiple-view is more or less
architectural than a design problem.
From architectural view, a collection of models is the Model
in the system. IO is independent from the system, and after
objects are loaded, no need to think about different sources.
Maybe you can use Composite pattern for the design, but not
necessary.
4. observer pattern a must for MVC.
01/10/13 06:00 酷帖! 臭帖! 回复
酷帖评价: 臭帖评价:
返回页首
slovenboy ^_^
--------------------------------------------------------------------------------
01/10/13 09:26 酷帖! 臭帖! 回复
酷帖评价: 臭帖评价:
返回页首
it365.com 可能有点帮助
--------------------------------------------------------------------------------
SecurityAdapter.java
/**
* Instances of this class receive a notification from an object that is
* can only deliver it to an object the implements the SecurityObserver
* interface and apsses it on to a SecurityMonitor object that does not
* implement SecurityObserver.
*/
class SecurityAdapter implements SecurityObserver {
private SecurityMonitor sm;
/**
* Constructor
*/
SecurityAdapter(SecurityMonitor sm) {
this.sm = sm;
} // Constructor(SecurityMonitor)
/**
* This is method is called to deliver a security notification to
* this object.
* @param device A number that identifies the device that originated
* this notification.
* @param event This should be one of the constants defined in this
* interface.
*/
public void notify(int device, int event) {
switch (event) {
case ALARM:
sm.securityAlert(device);
break;
case LOW_POWER:
case DIAGNOSTIC:
sm.diagnosticAlert(device);
break;
} // switch
} // notify(int, int)
} // class SecurityAdapter
=====================================================================
SecurityMonitor.java
/**
* Skeletal definition for a class that monitors security devices.
*/
public class SecurityMonitor {
//...
public void securityAlert(int device) {
//...
} // securityAlert(int)
public void diagnosticAlert(int device) {
} // diagnosticAlert(int)
} // SecurityMonitor
====================================================================
SecurityNotifier.java
import java.util.ArraySet;
import java.util.Iterator;
/**
* When an instance of this class receives a notification from a
* security device, it passes it on to all of its registered observers.
*/
class SecurityNotifier {
private ArraySet observers = new ArraySet();
//...
/**
* Add a new observer to this object.
*/
public void addObserver(SecurityObserver observer) {
observers.add(observer);
} // addObserver(SecurityObserver)
/**
* Remove an observer from this object
*/
public void removeObserver(SecurityObserver observer) {
observers.remove(observer);
} // removeObserver(SecurityObserver)
/**
* This method is called when this object needs to pass on a
* notification to its registered observers.
* @param device A number that identifies the device that originated
* this notification.
* @param event This should be one of the constants defined in this
* interface.
*/
private void notify(int device, int event) {
Iterator iterator = observers.iterator();
while (iterator.hasNext()) {
((SecurityObserver)iterator.next()).notify(device, event);
} // while
} // notify(int, int)
} // class SecurityNotifier
======================================================================
SecurityObserver.java
/**
* Classes that implement this interface can register to receive
* security notifications from SecurityNotifier objects.
*/
public interface SecurityObserver {
public final int ALARM = 1;
public final int LOW_POWER = 2;
public final int DIAGNOSTIC = 3;
/**
* This is method is called to deliver a security notification to
* this object.
* @param device A number that identifies the device that originated
* this notification.
* @param event This should be one of the constants defined in this
* interface.
*/
public void notify(int device, int event);
} // interface SecurityObserver
01/10/13 12:44 酷帖! 臭帖! 回复
酷帖评价: 臭帖评价:
返回页首
it365.com 后者用State行吗!
--------------------------------------------------------------------------------
01/10/13 12:46 酷帖! 臭帖! 回复
酷帖评价: 臭帖评价:
返回页首
slovenboy Notify 是什么? 公用成员怎么处理?
--------------------------------------------------------------------------------
Notify 也是一种模式吗??
在《design patterns》一书中, Observer 模式 中 Observer 要用接口
即 java 的 interface , 那么 实现接口的多个类中用到的公用
成员变量怎么处理 ?
我需要更改具体的实现中的变量,而java 中的 interface 中声明的
变量只能是final的,实现这个几口的类是不能更改他的!
是不是可以用abstract class 作为 Observer 的实现??
或者让实现 Observer 接口的类 继承一个公共的基类??
01/10/14 16:23 酷帖! 臭帖! 回复
酷帖评价: 臭帖评价:
返回页首
slovenboy 太多新东西,正在study.
--------------------------------------------------------------------------------
01/10/14 17:12 酷帖! 臭帖! 回复
酷帖评价: 臭帖评价:
返回页首
slovenboy 在研究!
--------------------------------------------------------------------------------
01/10/14 17:13 酷帖! 臭帖! 回复
酷帖评价: 臭帖评价:
返回页首
developerlin 我来说两句... ...
--------------------------------------------------------------------------------
这两种方法都行,不过我还有一个主意。定义一个数据源,然后定义这个数据源可以接受的数据对象,不管你的数据要从什么地方来,首先转换成这个对象,然后传给数据源就行了。基本思路如下:
class DataSource
{
public void populate(DataStandard data)
{
//将data元素中的值装入你的画图程序中
}
}
画图程序:可以设计一个method
getDataSource(DataSource source);
在其他提供数据的类中,可以提供一个method,将自身的数据转化为数据源允许接收的数据类型。
public StandardData toStandardData();
//如果这些类比较多,不好改动,或者改动量较大,可以将这些方法集中到一个类中,假设是
class DataStandardParser
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -