抽象生成器

Abstract builder

所以我有一个抽象交易class,它有多个实现(支付、文件)。

我想要交易(抽象)+ 实施者的构建器。

我这样做了:

public abstract class TransactionBuilder
{
    protected final Transaction transaction;
    
    public TransactionBuilder(Transaction transaction)
    {
        this.transaction = transaction;
    }
    
    public TransactionBuilder setSignature(byte[] signature)
    {
        this.transaction.setSignature(signature);
        return this;
    }
    
    public TransactionBuilder setPreviousHash(String previousHash)
    {
        this.transaction.setPreviousHash(previousHash);
        return this;
    }
    
    public abstract Transaction build();
}

PaymentBuilder 示例 class:

public class PaymentBuilder extends TransactionBuilder
{
    public PaymentBuilder(String from, String to, double amount)
    {
        super(new Payment(from, to, amount));
    }
    
    public PaymentBuilder addAmount(double amount)
    {
        ((Payment) this.transaction).amount += amount;
    }
    
    @Override
    public Payment build()
    {
        return (Payment) this.transaction;
    }
}

每个字段都有一个getter/setter,交易:

public abstract class Transaction
{
    //Used for serialization
    private String type;
    
    private String previousTransactionHash;
    private String hash;
    private String signature;
    
    private String fromAddress;
    private String toAddress;
    
    private Instant timeStamp;
    
    public Transaction(String type, String from, String to)
    {
        this.type = type;
        this.fromAddress = from;
        this.toAddress = to;
        this.timeStamp = Instant.now();

        setHash();
    }

我的使用方式:

Payment payment = new PaymentBuilder(from, to, amount)
                .setPreviousHash(previousHash)
                .build();

但是当我调用 setSignature() 时,我得到“类型不匹配:无法从交易转换为支付”,所以我需要将其转换为支付,我该如何避免这种情况?可以吗?

您可以使您的抽象构建器通用,通用类型是它生成的交易类型。

public abstract class TransactionBuilder<T extends Transaction> {
    ...
    public abstract T build();
}

public class PaymentBuilder extends TransactionBuilder<Payment> {
    @Override
    public Payment build() {
        ...
    }
}

感谢@khelwood,我最终使我的抽象生成器变得通用:

事务生成器:

public abstract class TransactionBuilder<T extends Transaction>
{
    protected final T transaction;
    
    public TransactionBuilder(T transaction)
    {
        this.transaction = transaction;
    }
    
    public TransactionBuilder<T> setSignature(byte[] signature)
    {
        this.transaction.setSignature(signature);
        return this;
    }
    
    public TransactionBuilder<T> setPreviousHash(String previousHash)
    {
        this.transaction.setPreviousHash(previousHash);
        return this;
    }
    
    public abstract T build();
}

支付生成器:

public class PaymentBuilder extends TransactionBuilder<Payment>
{
    public PaymentBuilder(String from, String to, double amount)
    {
        super(new Payment(from, to, amount));
    }

    public PaymentBuilder addAmount(double amount)
    {
        this.transaction.amount += amount;
    }
    
    @Override
    public Payment build()
    {
        return this.transaction;
    }
}