Как клонировать параметризованный прототип? - PullRequest
0 голосов
/ 26 мая 2020

Я хочу клонировать прототип, но с некоторыми изменениями. Я хочу передать аргументы, которые нужно изменить. У меня есть несколько вариантов решения, но я не уверен, какой из них лучший. Это может быть ни один из них.

Здесь у меня 3 класса, Prototype1, Prototype2 и Prototype3. Каждый из них использует свой способ клонирования. Класс PrototypeBase предназначен только для определения общего поведения трех классов.

Каждый класс клонируется в функции main.

public class Main {

    public static void main(String[] args) {

        var prototype1 = new Prototype1("a", "b", "c");
        var prototype1Clone = prototype1.clone(null, "bbb", null);

        var prototype2 = new Prototype2("a", "b", "c");
        var prototype2Clone = prototype2.clone();
        prototype2Clone.setB("bbb");

        var prototype3 = new Prototype3("a", "b", "c");
        var prototype3Clone = new Prototype3(null, "bbb", null);
        prototype3Clone.clone(prototype3);
    }
}

class PrototypeBase
{
    protected String a, b, c;

    public void setA(String a) {
        this.a = a;
    }

    public void setB(String b) {
        this.b = b;
    }

    public void setC(String c) {
        this.c = c;
    }

    @Override
    public String toString() {
        return "Prototype{" +
                "a='" + a + '\'' +
                ", b='" + b + '\'' +
                ", c='" + c + '\'' +
                '}';
    }
}

class Prototype1 extends PrototypeBase
{

    public Prototype1(String a, String b, String c) {
        this.a = a;
        this.b = b;
        this.c = c;
    }

    // Won't have a uniform interface across all prototypes
    // since each prototype may have a different number of fields.
    public Prototype1 clone(String a, String b, String c)
    {
        if (a == null)
            a = this.a;

        if (b == null)
            b = this.b;

        if (c == null)
            c = this.c;

        return new Prototype1(a, b, c);
    }
}

class Prototype2 extends PrototypeBase
{
    public Prototype2(String a, String b, String c) {
        this.a = a;
        this.b = b;
        this.c = c;
    }

    // a, b or c may be expensive to copy. It would
    // be better if we can pass the parameters directly
    // without copying all members first.
    public Prototype2 clone()
    {
        return new Prototype2(a, b, c);
    }
}

class Prototype3 extends PrototypeBase
{
    public Prototype3() {}

    public Prototype3(String a, String b, String c) {
        this.a = a;
        this.b = b;
        this.c = c;
    }

    // Not so intuitive. Usually, people just
    // ask for a clone and get the result back.
    public void clone(Prototype3 prototype)
    {
        if (this.a == null)
            this.a = prototype.a;

        if (this.b == null)
            this.b = prototype.b;

        if (this.c == null)
            this.c = prototype.c;
    }
}

У каждого есть свои недостатки. Как лучше всего клонировать прототип, если нужно, чтобы некоторые поля имели другое значение?

...