zoukankan      html  css  js  c++  java
  • 设计模式(建造者模式)

    建造者模式用于创建相对复杂,组成较多的对象。它包括几类角色:

    • Product: 待被创建的对象,本例中是Table
    • builder:创建对象方法的抽象,本例中是TableBuilder
    • ConcreteBuilder:具体某个创建的实现,本例中是TableBuilderImpl
    • Director:封装builder接口

    类图关系如下:

    代码如下:

    • Table
    package com.example;
    
    public class Table {
        private String head;
        private String content;
        private String footer;
        
        public String getHead() {
            return head;
        }
        public void setHead(String head) {
            this.head = head;
        }
        public String getContent() {
            return content;
        }
        public void setContent(String content) {
            this.content = content;
        }
        public String getFooter() {
            return footer;
        }
        public void setFooter(String footer) {
            this.footer = footer;
        }
        
        public String getTable(){
            return this.head + "_" + this.content + "_" + this.footer;
        }
    }
    • TableBuilder
    public interface TableBuilder {
        public TableBuilder setTableHead(String head);
        public TableBuilder setTableContent(String content);
        public TableBuilder setTableFooter(String footer);
        public Table builder();
    }
    • TableBuilderImpl
    package com.example;
    
    public class TableBuilderImpl implements TableBuilder {
        
        private Table table;
        private static final String TYPE = "TYPE1";
        
        public TableBuilderImpl(){
            table = new Table();
        }
    
        @Override
        public TableBuilder setTableHead(String head) {
            table.setHead(TYPE + head);
            return this;    
        }
    
        @Override
        public TableBuilder setTableContent(String content) {
            table.setContent(TYPE + content);
            return this;
        }
    
        @Override
        public TableBuilder setTableFooter(String footer) {
            table.setFooter(TYPE + footer);
            return this;
        }
    
        @Override
        public Table builder() {
            return table;
        }
    }
    • Director
    public class Director {
        private TableBuilder builder;
        
        public Director(TableBuilder builder){
            this.builder = builder;
        }
        
        public Table Builder(){
            return builder.setTableHead("head")
                    .setTableContent("content")
                    .setTableFooter("footer")
                    .builder();
        }
    }
    • APP
    public class App {
    
        public static void main(String[] args) {
            Director direct = new Director(new TableBuilderImpl());
            Table table = direct.Builder();
            System.out.println(table.getTable());
        }
    }
  • 相关阅读:
    fatal: 'origin' does not appear to be a git repository
    Mac cpu过高问题分析及解决
    Java8新特性之日期和时间
    Allure自动化测试报告之修改allure测试报告logo
    Allure自动化测试报告之修改allure测试报告名称
    jmeter压测过程中报java.lang.NoClassDefFoundError: org/bouncycastle/jce/provider/BouncyCastleProvider
    java.security.InvalidKeyException: Illegal key size or default parameters
    scanf中的%[^ ]%*c格式
    fork,vfork和clone底层实现
    僵尸进程的产生原因和避免方法
  • 原文地址:https://www.cnblogs.com/Fredric-2013/p/4571573.html
Copyright © 2011-2022 走看看