带有继承的工厂方法模式的好方法 [英] Nice way of factory method pattern with inheritance

查看:156
本文介绍了带有继承的工厂方法模式的好方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我具有以下类层次结构:

Suppose I have following class hierarchy:

class abstract Parent{}

class FirstChild extends Parent {}

class SecondChild extends Parent {}

我想从每个孩子创建DTO对象:

And I'd like to create DTO objects from each child:

class abstract ParentDTO {}

class FirstChildDTO extends ParentDTO{}

class SecondChildDTO extends ParentDTO{}

我认为我需要一个类似这样的工厂方法:

I think I need a factory method something like this:

ParentDTO createFrom(Parent source); 

在Java中,有没有 nice 方式,而无需instanceof检查和if/else语句?

Is there any nice way to do this in Java without instanceof checks and if/else statements?

编辑: 此工厂方法不起作用:

EDIT: This factory method does not work:

public ParentDTO create(Parent source)
{
    return _create(source);
}

private FirstChildDTO _create(FirstChild source)
{
    return new FirstDTO();
}

private SecondChildDTO _create(SecondChild source)
{
    return new SecondDTO();
}

private ParentDTO _create(Parent source)
{
    return new ParentDTO();
}

它仅生成ParentDTO s,这就是原因:

It only generates ParentDTOs and here is why:

Parent p = new FirstChild();
System.out.println(factory.create(p));  //ParentDTO

FirstChild f = new FirstChild();
System.out.println(factory.create(f));  //FirstChildDTO

推荐答案

如果您坚持使用工厂进行DTO创建,则可以使用简单的方法重载.示例如下:

If you insist on using a factory for DTO creation you can use simple method overloading. Example follows:

public class Factory {

    public ParentDTO createDTO(Parent parent) {
        return new ParentDTO();
    }
    public FirstChildDTO createDTO(FirstChild firstChild) {
        return new FirstChildDTO();
    }
    public SecondChildDTO createDTO(SecondChild SecondChild) {
        return new SecondChildDTO();
    }
}

public class Parent {
}

public class FirstChild extends Parent {
}

public class SecondChild extends Parent {
}

public class ParentDTO {

    @Override
    public String toString() {
        return this.getClass().getSimpleName();
    }
}

public class FirstChildDTO extends ParentDTO {

    @Override
    public String toString() {
        return this.getClass().getSimpleName();
    }
}

public class SecondChildDTO extends ParentDTO {

    @Override
    public String toString() {
        return this.getClass().getSimpleName();
    }
}

public class App {

    public static void main(String[] args) {
        Factory factory = new Factory();
        ParentDTO parentDTO = factory.createDTO(new Parent());
        System.out.println(parentDTO);
        FirstChildDTO firstChildDTO = factory.createDTO(new FirstChild());
        System.out.println(firstChildDTO);
        SecondChildDTO secondChildDTO = factory.createDTO(new SecondChild());
        System.out.println(secondChildDTO);
    }
}

在我的IDE输出中将应用程序作为Java应用程序运行:

Running the App as Java application in my IDE outputs:

ParentDTO
FirstChildDTO
SecondChildDTO

ParentDTO
FirstChildDTO
SecondChildDTO

这篇关于带有继承的工厂方法模式的好方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆