应用程序设计-更新域对象

发布于 2024-12-11 14:24:20 字数 587 浏览 0 评论 0原文

我想改进我的应用程序某些部分的设计。它是一个 swing 应用程序,其中用户界面通过从服务器 (TCP/UDP) 接收的数据进行更新。目前,我将域对象(PO​​JO)传递给该类的构造函数,它将与服务器连接,接收和发送数据,并直接使用 getter 和 setter 方法。

public static void main(String[] args) {
TcpClient client = new TcpClient(server, port, domainModel);
}

public class TcpClient {
public void connect() {
// Code to create the socket and connect to the server.

new Thread(new TcpProtocol(socket, domainModel)).start();
}
}

我在 TcpProtocol 类中使用某种 Factory 类来创建正确的对象。 我想将我的域对象与网络编程部分分离。有一些通用的模式可以用于此吗?我正在考虑常用于 JavaEE 应用程序的 DAO 和值对象。这些是我唯一看到的,但如果有人有更好的建议,请告诉我。 谢谢。

I would like to improve the design of some part of my application. It is a swing application in which the user interface is updated by data received from a server (TCP/UDP). At the moment i pass my domain object (POJO) to the constructor of the class the will connect with the server, receive and send data, and directly use getter and setter method.

public static void main(String[] args) {
TcpClient client = new TcpClient(server, port, domainModel);
}

public class TcpClient {
public void connect() {
// Code to create the socket and connect to the server.

new Thread(new TcpProtocol(socket, domainModel)).start();
}
}

I 'm using some sort of Factory class inside the TcpProtocol class to create the right object.
I would like to decouple my domain object from the network programming part. Is there some common pattern to use for this ? I was thinking about DAO and Value Object, commonly used for JavaEE applications. Those are the only one i see, but if someone has better proposition please let me know.
Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

爱情眠于流年 2024-12-18 14:24:20

作为一般规则,我认为构造函数中的重要代码是一种设计味道,因为它使继承和其他形式的可扩展性变得复杂。

如果您不需要对象在创建后从网络更新自身,那么您可以使用 Factory 来创建您的 POJO。我倾向于避免 ValueObjects 除非你用很少的数据(如货币/日期)包装一些东西,否则你最终会得到一堆 getter/setter。

如果您需要该对象在使用过程中访问网络,那么您可以将其设为Proxy,并传入一个处理所有网络代码的DAO。为了进行测试,您可以传入一个Mock/Stub

As a general rule I consider non-trivial code in the constructor to be a design smell since it complicates inheritance and other forms of extensibility.

If you do not need the object to update itself from the network after it is created, then you can use a Factory to create your POJO. I tend to avoid ValueObjects unless you are wrapping something with very little data like currency/dates otherwise you will end up with a bunch of getters/setters.

If you need the object to access the network during its use, then you can make it a Proxy and pass in a DAO that handles all the network code. For testing you pass in a Mock/Stub instead.

一直在等你来 2024-12-18 14:24:20

实现目标的第一步是从构造函数中删除网络访问代码(我假设您有一个与网络联系的类,并且将 POJO 传递给其构造函数)。您可以创建一个连接网络的实用程序。

要从数据流实例化正确的对象类型,您可以考虑创建一个知道如何使流适应特定对象类型的Factory

A start to reaching your goal would be to remove the network access code from your constructor (I'm assuming you have one class that contacts the network and you're passing your POJO to its constructor). You would instead create a utility that contacts the network.

To instantiate the correct object type from your data stream, you might consider creating a Factory that knows how to adapt the stream to specific object types.

榆西 2024-12-18 14:24:20

也许使用 java.io ObjectInputStream 和 ObjectOutputStream 可能是执行此操作的简单方法,因为它不依赖于传输的对象的类?

Maybe using the java.io ObjectInputStream and ObjectOutputStream might be a simple way to do this since it does not depend on the class of the object transferred?

爱*していゐ 2024-12-18 14:24:20

我建议做的是应用 Observer 模式。您可能还需要将 Mediator 模式与观察者一起应用 - 但请仔细检查您的设计是否
保证了这种复杂性。

观察者背后的想法是,您的 UI 代码“监听”所呈现模型中的更改。当发生更改时(可能是来自网络绑定代码),会触发一个事件,并通知您的 UI 以便相应地更新自身,

What i would suggest to do is apply the Observer pattern. You might also need to apply the Mediator pattern along with your Observer - but check carefully if your design
warrants this kind of complexity.

The idea behind the observer is that your UI code "listens" for changes in the presented model. When changes take place (presumably from your network-bound code) an event is fired and your UI is thus notified in order to update itself accordingly,

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文