建造者模式+原型模式 实现发票开具功能

项目需求:

用户支付完成后,部分企业用户或个人账户需要开电子增值税发票,实现该功能

使用建造者模式:

建造者模式是将一个复杂对象的构建和它的表示分离,使得同样的构建过程可以创建不同的表示

支付成功之后开具增值发票,由于发票所需内容较多,而且随着国家政策可能会有所更改,发票内容的添加和删除以及后台对象的组装尽量做到灵活

发票工具不是高并发访问接口,且无法缓存,使用原型模式尽量保证发票创建的性能

    

package com.example.demo.builder;

public abstract class TicketBuilder {

    public abstract void setParam(String account,String title);

    /**
     * 配置中心或者DB或者缓存
     */
    public abstract void setContent(String content);
    /**
     * DB
     */
    public abstract void setProduct(String product);

    /**
     * 需要校验的, 校验通过才能设置
     */
    public void setBankInfo(String bankInfo){};

    public abstract T buildTicket();

}


public class PersonTicketBuilder extends TicketBuilder {

    private PersonTicket personTicket = TicketConstant.personTicket.clone();

    @Override
    public void setParam(String account, String title) {
        personTicket.setAccount(account);
        personTicket.setTitle(title);
    }

    @Override
    public void setContent(String content) {
        personTicket.setContent(content);
    }

    @Override
    public void setProduct(String product) {
        personTicket.setProduct(product);
    }

    @Override
    public PersonTicket buildTicket() {
        return personTicket;
    }
}


public class CompanyTicketBuilder extends TicketBuilder {

    private CompanyTicket companyTicket = TicketConstant.companyTicket.clone();

    @Override
    public void setBankInfo(String  bankInfo){
        companyTicket.setBankInfo(bankInfo);
    }


    @Override
    public void setParam(String account, String title) {
        companyTicket.setType(account);
        companyTicket.setTitle(title);
    }

    @Override
    public void setContent(String content) {
        companyTicket.setContent(content);
    }

    @Override
    public void setProduct(String product) {
        companyTicket.setProduct(product);
    }

    @Override
    public CompanyTicket buildTicket() {
        return companyTicket;
    }


}


public class TicketConstant {
    public static PersonTicket personTicket = null;
    public static CompanyTicket companyTicket = null;

    static {
        companyTicket.setType("type");
        personTicket.setType("type");
        companyTicket.setFooter("footer");
        personTicket.setFooter("footer");
    }

}

public class TicketParam {
    private String account;
    private String title;
    private String bankInfo;
    private String footer;
    private String productId;
    private String type;

    public String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getBankInfo() {
        return bankInfo;
    }

    public void setBankInfo(String bankInfo) {
        this.bankInfo = bankInfo;
    }

    public String getFooter() {
        return footer;
    }

    public void setFooter(String footer) {
        this.footer = footer;
    }

    public String getProductId() {
        return productId;
    }

    public void setProductId(String productId) {
        this.productId = productId;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }
}


@Data
public class CompanyTicket {
    private String footer;
    private String type;
    private String title;
    private String account;
    private String content;
    private String product;
    private String bankInfo;

    @Override
    public CompanyTicket clone() {
        CompanyTicket companyTicket = null;
        try {
            companyTicket = (CompanyTicket) super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return companyTicket;
    }

}


@Data
public class PersonTicket {

    private String type;
    private String footer;
    private String title;
    private String account;
    private String content;
    private String product;

    @Override
    public PersonTicket clone() {
        PersonTicket personTicket = null;
        try {
            personTicket = (PersonTicket) super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return personTicket;
    }

}

你可能感兴趣的:(设计模式,建造者模式)