java 将一种模型转换为另一种模型的设计模式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13070488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 11:23:06  来源:igfitidea点击:

Design pattern for converting one model to another model

javadesign-patterns

提问by Ashay Batwal

Basically, I have a number of objects in my application and I have to convert them to another third party objects before sending the request. On receiving the response I have to convert these objects back to objects supported by my application.

基本上,我的应用程序中有许多对象,我必须在发送请求之前将它们转换为另一个第三方对象。收到响应后,我必须将这些对象转换回我的应用程序支持的对象。

What pattern can I use for converting one model object to another in Java?

我可以使用什么模式在 Java 中将一个模型对象转换为另一个?

采纳答案by user1310761

Either Adapter or facade pattern should solve your problem:

适配器或外观模式应该可以解决您的问题:

Adapter: http://www.youtube.com/watch?v=TriX8OiEhOU

适配器:http: //www.youtube.com/watch?v=TriX8OiEhOU

Facade: http://www.youtube.com/watch?v=WLjvNpP6yeQ

外观:http: //www.youtube.com/watch?v=WLjvNpP6yeQ

回答by casablanca

I don't think there is a specific pattern for this, but you simply need a "converter" or "translator" class that takes in one object and returns another:

我认为这没有特定的模式,但您只需要一个“转换器”或“转换器”类,它接受一个对象并返回另一个对象:

MyObject convert(ThirdPartyObject obj);
ThirdPartyObject convert(MyObject obj);

回答by Ravindra babu

Adapterand Facadeare structural patterns. You don't have any patterns to cater to Object transformation.

AdapterFacade是结构模式。您没有任何模式可以满足Object 转换

On creational pattern front, Builderis one pattern you can think of.

在创建模式方面,Builder是您可以想到的一种模式。

Generally Builderpattern is used to build object with mandatory and optional parameter. But you can fine tune it by building necessary object.

通常Builder模式用于构建具有强制和可选参数的对象。但是您可以通过构建必要的对象来微调它。

You can solve the problem without pattern too. Either you can use Object compositionor Write your own method to transform the object.

你也可以不用模式解决问题。您可以使用对象组合编写自己的方法来转换对象

Have a look at related SE question with code example:

使用代码示例查看相关的 SE 问题:

How to prune an object of some of its fields in Java?

如何在 Java 中修剪其某些字段的对象?

回答by tjg184

It's a bit hard to determine the context of the objects, but take a look at the Assembler pattern, although not technically a design pattern. An Assembler class is used to map from one object to another, specifically when one object is a DTO (kind of like your response object) to a Domain object. The Dozer framework can help you with some of these tedious transformations.

确定对象的上下文有点困难,但看看汇编模式,虽然在技术上不是设计模式。Assembler 类用于从一个对象映射到另一个对象,特别是当一个对象是 DTO(有点像您的响应对象)到域对象时。Dozer 框架可以帮助您完成其中一些乏味的转换。

回答by SomeWittyUsername

You probably look for the adapter pattern: http://en.wikipedia.org/wiki/Adapter_pattern

您可能会寻找适配器模式:http: //en.wikipedia.org/wiki/Adapter_pattern