简体   繁体   English

面向对象的对象转换器设计模式

[英]Object-oriented design pattern for object converter

I was wondering what the best OOP design pattern should be put in place for my scenario.我想知道应该为我的场景设置什么最好的OOP 设计模式

The scenario:场景:

I have two classes, ClassA & ClassB.我有两个班级,ClassA 和 ClassB。 I want to convert ClassA into a new ClassB, however, ClassA may change its member variables & methods.我想将 ClassA 转换为新的 ClassB,但是,ClassA 可能会更改其成员变量和方法。 This means that one may have to update the way ClassA is translated into ClassB.这意味着人们可能必须更新 ClassA 转换为 ClassB 的方式。

What would be the best OOP design to go about facilitating this translation from ClassA to a new ClassB?促进从 ClassA 到新 ClassB 的转换的最佳 OOP 设计是什么?

The goal:目标:

Create a design pattern that will allow to easily change the way ClassA will be translated to ClassB.创建一个设计模式,允许轻松更改 ClassA 转换为 ClassB 的方式。

Example:例子:

ClassA A类

public class ClassA {
    private String actionName;

    // With some getters & setters
}

ClassB B级

public class ClassB {
    private String action; // Note the subtle difference

    // With some getters & setters
}

Handler处理程序

public class Handler {
    ClassB classB = new ClassB();

    // Convert ClassB to ClassA

    This.publish(ClassA);
}

You need a type that has a knowledge of all the members of both ClassA and ClassB.您需要一个了解 ClassA 和 ClassB 的所有成员的类型。 It is a bad idea to couple either ClassA or ClassB to each other, so you would normally see a third type that implements an interface like this:将 ClassA 或 ClassB 相互耦合是一个坏主意,因此您通常会看到实现如下接口的第三种类型:

interface Converter<T, R> {
  R convert(T obj);
}

You could implement this interface for your classes:你可以为你的类实现这个接口:

class AtoBConverter implements Converter<ClassA, ClassB> {
  public ClassB convert(ClassA obj) {
    ...
  }
}

This assumes that ClassA has public methods that allow you to examine its state and ClassB has a constructor/setters that allow you to modify its state.这假设 ClassA 具有允许您检查其状态的公共方法,而 ClassB 具有允许您修改其状态的构造函数/设置器。 Strictly speaking if all three classes are in the same java package you could have those methods/constructors declared package private.严格来说,如果所有三个类都在同一个 java 包中,您可以将这些方法/构造函数声明为包私有。

use constructor with params (ClassA) for ClassB and vise versa对 ClassB 使用带有 params (ClassA) 的构造函数,反之亦然

ClassA(ClassB object){
...
}

You should definitely opt for a separate handler class to deal with this conversion.您绝对应该选择一个单独的处理程序类来处理这种转换。

As a little optimization, you could have static api's on this handler class so that whenever you wish to perform a conversion between two objects you don't have to instantiate an instance of the handler class as well.作为一个小优化,您可以在此处理程序类上使用静态 api,这样无论何时您希望在两个对象之间执行转换,您也不必实例化处理程序类的实例。

For example:例如:

Say you need to convert an object of Type A to an object of Type B. Your converter could look like假设您需要将 A 类对象转换为 B 类对象。您的转换器可能看起来像

class AToBConverter {
    
   public static B convert(A objA){
       //business logic to convert objA to objB
       return objB;
   } }

The advantage here is that whenever you need to convert object of type A to B you can simply do :这里的优点是,每当您需要将 A 类型的对象转换为 B 时,您只需执行以下操作:

B objB = AToBConverter.convert(objA); //Note how we don't need to create an instance of the converter

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM