问题描述
我正在尝试设计网络连接"以便于轻松地从服务器中检索数据.但是,我遇到了设计问题.
I'm try to design "Network Connection" for easily use to retrieve data from server. However, I facing a design problem.
我要使用的 NetworkUtil
类设计为
class NetworkUtil
public NetworkUtil(URL, resultType); // resultType might be XML or RAW
public setListener(listener); // listener will notice when result has arrive
public addPostData(key, value);
public connect(); // connect start new thread, so result will shown in listener
interface NetworkUtilListener1
public onNetworkFail();
public onNetworkSuccess(URL, resultType, xml, raw);
interface NetworkUtilListener2
public onNetworkFail();
public onNetworkSuccessRAW(URL, resultType, raw);
public onNetworkSuccessXML(URL, resultType, xml);
一旦结果到达,我将检查resultType并使用该参数的结果.但是,在上面显示的2个示例( NetworkUtilListener1
和 NetworkUtilListener2
)中,当更多的 resultType
即将出现时,例如JSON,图像,我考虑了将来使用的问题甚至我的自定义类型,以便我的团队可以轻松使用它.
Once result arrive, I will check resultType and use the result from that parameter. However in 2 example shown above (NetworkUtilListener1
and NetworkUtilListener2
), I consider the problem for future use when more resultType
is coming such as JSON, image or even my custom type, so my team can easily use it.
NetworkUtilListener1
将具有较长的未使用参数,例如
NetworkUtilListener1
will have long unused parameter like
onNetworkSuccess(URL, resultType, raw, xml, json, image);
这不是我认为的好设计.
which isn't a good design as I thought.
NetworkUtilListener2
将迫使使用它的具体类使用很多空方法,因为在大多数情况下,每个项目我们只希望使用一种或两种类型的结果.
NetworkUtilListener2
will force concrete class who use it to have a lot of empty method since most of the time we prefer only 1 or 2 type of result on each project.
onNetworkSuccessRAW(URL, resultType, raw);
onNetworkSuccessXML(URL, resultType, xml);
onNetworkSuccessJSON(URL, resultType, json);
onNetworkSuccessImage(URL, resultType, image);
任何人都可以为我提供一些有关重新设计此类结构的帮助,或者向我推荐我需要关注的设计模式.这样我就可以拥有更好的 NetworkListener
.
Anyone could give me some help on redesign this class structure or recommend me the design pattern I need to focus on. so I can have better NetworkListener
.
推荐答案
这听起来像是经典的解决方案访问者模式:
让我们从重载NetworkListener方法开始:
Let's start by overloading the NetworkListener methods:
interface NetworkListener {
void onSuccess(XMLResult xml);
void onSuccess(JSONResult json);
}
然后,如建议的那样,让我们对结果进行几种实现.每个人都可以通过调用适当的重载方法来通知侦听器.
Then, as suggested, let's have several implementation of results. Everyone of them capable of notifying a listener by invoking the appropriate overloaded method.
interface Result {
void notify(NetworkListener listener);
}
class XMLResult implements Result {
@Override
public void notify(NetworkListener listener) {
listener.onSuccess(this);
}
}
class JSONResult implements Result {
@Override
public void notify(NetworkListener listener) {
listener.onSuccess(this);
}
}
现在,让我们看一下示例网络侦听器实现的样子:
Now, let's see how a sample network listener implementation would look like:
class SampleListner implements NetworkListener{
@Override
public void onSuccess(XMLResult xml) {
// handle here
}
@Override
public void onSuccess(JSONResult json) {
// handle here
}
}
您的通知代码应类似于:
And your notification code would look somewhat like:
Result result = null;
for(NetworkListener listener: listeners){
result.notify(listener);
}
由于该模式严重依赖于方法重载,因此您可以添加一个接收Result对象的包罗万象的方法,这将确保如果创建了Result的任何新实现,那么如果您不执行Result,仍然可以得到它们的默认处理添加其他重载方法.
Since the pattern relies heavily on method overloading you can add a catch-all method that receives a Result object, that will guarantee that if any new implementations of Result are created, you still get a default handling for them if you do not add further overloading methods.
这篇关于OOP结构设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!