• 创建者模式


    package com.sise.llm.test;


    //建造者模式:将一个复杂对象的建造与他的表示分离,使得同样的构造过程可以创建不同的表示
    //四部分 Director(步骤控制) ConcreteBuilder(具体构建方式) Builder(抽象产品) Product(产品A)
    // ProductBuilder(抽象接口:产品属性与创建方法)
    //
    // ProductBuilder DefaultConcreteProductBuilder Director Product
    // 想法-》工具-》实现过程-》产品
    //
    //
    // 1
    public class BuilderTest {
    public static void main(String[] args) {
    //Product a = new Product("产品A","公司1","功能1");
    DefaultConcreteProductBuilder defaultConcreteProductBuilder = new DefaultConcreteProductBuilder();
    Director director = new Director(defaultConcreteProductBuilder);
    director.makeProduct("产品名称","公司名称","功能1");



    }
    }

    interface ProductBuilder{
    void builderProductName(String productName);
    void builderCompanyName(String companyName);
    void builderPart1(String part1);

    Product builder();
    }

    class DefaultConcreteProductBuilder implements ProductBuilder{
    private String productName;
    private String companyName;
    private String part1;

    @Override
    public void builderProductName(String productName) {
    this.productName = productName;

    }

    @Override
    public void builderCompanyName(String companyName) {
    this.companyName = companyName;

    }

    @Override
    public void builderPart1(String part1) {
    this.part1 = part1;

    }

    @Override
    public Product builder() {
    return new Product(this.productName,this.companyName,this.part1);
    }
    }

    class Director{
    private ProductBuilder builder;

    public Director(ProductBuilder builder){
    this.builder = builder;
    }

    public Product makeProduct(String productName, String companyName, String part1){
    builder.builderProductName(productName);
    builder.builderCompanyName(companyName);
    builder.builderPart1(part1);
    Product product = builder.builder();
    System.out.print(product);
    return product;
    }
    }

    class Product{

    private String productName;
    private String companyName;
    private String part1;



    public Product(String productName, String companyName, String part1) {
    this.productName = productName;
    this.companyName = companyName;
    this.part1 = part1;
    }

    public String getProductName() {
    return productName;
    }

    public void setProductName(String productName) {
    this.productName = productName;
    }

    public String getCompanyName() {
    return companyName;
    }

    public void setCompanyName(String companyName) {
    this.companyName = companyName;
    }

    public String getPart1() {
    return part1;
    }

    public void setPart1(String part1) {
    this.part1 = part1;
    }




    }
  • 相关阅读:
    bash while until 循环用法
    微信小程序入门介绍
    jquery遍历
    jquery获取元素和DOM获取元素
    ul在div中水平居中效果
    一个文字在一个图片上水平居中,并且悬浮变大特效
    一个div在另一个div中垂直居中的方法
    layer插件的使用
    百度分享插件使用
    图标字体
  • 原文地址:https://www.cnblogs.com/oo-llm/p/12045015.html
Copyright © 2020-2023  润新知