Java设计模式

Java实现23种设计模式(二):建造者模式(Builder模式

2020-05-31  本文已影响0人  依然慢节奏

二十三种设计模式分类

设计模式三大分类.jpg

一、概述

建造者模式(Builder Pattern)使用多个简单的对象一步一步构建成一个复杂的对象。将一个复杂对象的构建与其表示相分离,同样的构造过程可以创建不同的表示。主要解决复杂对象的创建,将变与不变部分分隔开来;

优点

  1. 各个具体的建造者相互独立,有利于系统的扩展。
  2. 客户端不必知道产品内部组成的细节,便于控制细节风险。

缺点

  1. 产品的组成部分必须相同,这限制了其使用范围。
  2. 如果产品的内部变化复杂,该模式会增加很多的建造者类。

场景

  1. 需要生成的对象具有复杂的内部结构。
  2. 需要生成的对象内部属性本身相互依赖。

二、实现

1. 建造者模式结构图

建造者(Builder)模式的主要角色如下。

建造者模式结构图

PS:UML结构图可以参考,例子实现并不根据UML图来完成,灵活实现即可;

2. 实现

PersonBuilderPerson的静态内部类,也是Person类的建造器,Person类提供builder()方法获取建造器,通过建造器可以设置Person实例的各种属性,通过链式调用可以根据不同的条件方便快捷的构建不同属性的Person实例,通过不同的表示构建不同属性的实例对象;

package cn.missbe.model.builder;

/**
 * Copyright (c) 2020.
 * Email: love1208tt@foxmail.com
 *
 * @author lyg  2020/4/29 上午11:43
 * description:
 **/

public class Person {
    private String age;
    private String name;
    private String sex;
    private String info;
    private String address;
    private String height;
    private String weight;
    private String score;

    /**
     * 基本的构造方法,访问权限为私有只能通过Builder来创建对象
     */
    private Person(final String age, final String name, final String sex, final String info, final String address, final String height, final String weight, final String score) {
        this.age = age;
        this.name = name;
        this.sex = sex;
        this.info = info;
        this.address = address;
        this.height = height;
        this.weight = weight;
        this.score = score;
    }

    public static Person.PersonBuilder builder() {
        return new Person.PersonBuilder();
    }

    /**
     * Person的内部静态Builder类,针对多个属性的对象特别适用
     */
    public static class PersonBuilder {
        private String age;
        private String name;
        private String sex;
        private String info;
        private String address;
        private String height;
        private String weight;
        private String score;

        /**
         * 私有,外部不能new对象只能过builder创建
         */
        private PersonBuilder() {
        }

        public Person.PersonBuilder basicInfo(final String name, String sex, String age) {
            this.name = name;
            this.sex = sex;
            this.age = age;
            return this;
        }

        public Person.PersonBuilder age(final String age) {
            this.age = age;
            return this;
        }

        public Person.PersonBuilder name(final String name) {
            this.name = name;
            return this;
        }

        public Person.PersonBuilder sex(final String sex) {
            this.sex = sex;
            return this;
        }

        public Person.PersonBuilder info(final String info) {
            this.info = info;
            return this;
        }

        public Person.PersonBuilder address(final String address) {
            this.address = address;
            return this;
        }

        public Person.PersonBuilder height(final String height) {
            this.height = height;
            return this;
        }

        public Person.PersonBuilder weight(final String weight) {
            this.weight = weight;
            return this;
        }

        public Person.PersonBuilder score(final String score) {
            this.score = score;
            return this;
        }

        public Person build() {
            return new Person(this.age, this.name, this.sex, this.info, this.address, this.height, this.weight, this.score);
        }

        @Override
        public String toString() {
            return "Person.PersonBuilder(age=" + this.age + ", name=" + this.name + ", sex=" + this.sex + ", info=" + this.info + ", address=" + this.address + ", height=" + this.height + ", weight=" + this.weight + ", score=" + this.score + ")";
        }
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getInfo() {
        return info;
    }

    public void setInfo(String info) {
        this.info = info;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getHeight() {
        return height;
    }

    public void setHeight(String height) {
        this.height = height;
    }

    public String getWeight() {
        return weight;
    }

    public void setWeight(String weight) {
        this.weight = weight;
    }

    public String getScore() {
        return score;
    }

    public void setScore(String score) {
        this.score = score;
    }

    @Override
    public String toString() {
        return "Person{" + "age='" + age + '\'' + ", name='" + name + '\'' + ", sex='" + sex + '\'' + ", info='" + info + '\'' + ", address='" + address + '\'' + ", height='" + height + '\'' + ", weight='" + weight + '\'' + ", score='" + score + '\'' + '}';
    }
}
   public static void main(String[] args) {
        Person person = Person.builder()
                .basicInfo("unnet", "man", "20")
                .height("170")
                .address("xxxx-xxx-xxx")
                .score("100")
                .weight("120")
                .info("this is build model")
                .build();
        System.out.println(person);
        ///修改属性
        person.setWeight("130");
        person.setAddress("cq-china");
        person.setInfo("cqupt");
        System.out.println(person);
    }
上一篇 下一篇

猜你喜欢

热点阅读