java中用builder模式实现实体类的创建

package com.panther.builder;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 在设计模式中对Builder模式的定义是用于构建复杂对象的一种模式,
 * 所构建的对象往往需要多步初始化或赋值才能完成。
 * 那么,在实际的开发过程中,我们哪些地方适合用到Builder模式呢?
 * 其中使用Builder模式来替代多参数构造函数是一个比较好的实践法则。
 * Created by panther.dongdong on 2015/11/16.
 */
public class Student {
    private static final Logger LOGGER = LoggerFactory.getLogger(Student.class.getName());

    private int id;
    private int age;
    private String name;
    private String sex;
    private String love;
    private String phone;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int 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 getLove() {
        return love;
    }

    public void setLove(String love) {
        this.love = love;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }


    public static Builder create() {
        return new Builder();
    }

    public Student(Builder builder) {
        this.id = builder.id;
        this.age = builder.age;
        this.name = builder.name;
        this.sex = builder.sex;
        this.love = builder.love;
        this.phone = builder.phone;
    }

    public static class Builder {

        private int id = 0;
        private int age = 0;
        private String name = null;
        private String sex = null;
        private String love = null;
        private String phone = null;

        //构建的步骤
        public Builder addId(int id) {
            this.id = id;
            return this;
        }

        public Builder addAge(int age) {
            this.age = age;
            return this;
        }

        public Builder addName(String name) {
            this.name = name;
            return this;
        }

        public Builder addSex(String sex) {
            this.sex = sex;
            return this;
        }

        public Builder addLove(String love) {
            this.love = love;
            return this;
        }

        public Builder addPhone(String phone) {
            this.phone = phone;
            return this;
        }

        public Student build() {
            return new Student(this);
        }
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", age=" + age +
                ", name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", love='" + love + '\'' +
                ", phone='" + phone + '\'' +
                '}';
    }

    /**
     * 测试生成Student
     *
     * @param args
     */
    public static void main(String[] args) {
        Student student = Student.create().addAge(18).addName("panther").addLove("play")
                .addPhone("110").addSex("男").build();
        LOGGER.info("{}", student.toString());
    }
}


  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在备忘录模式中,我们需要定义三个角色:Originator(发起人)、Memento(备忘录)和Caretaker(管理者)。在这个例子中,ChessBoard类是发起人,ChessBoardMemento类是备忘录,ChessBoardCaretaker类是管理者。 ChessBoard类定义: ```java public class ChessBoard { private String[][] board; private int size; public ChessBoard(int size) { this.size = size; board = new String[size][size]; // 初始化棋盘 for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { board[i][j] = "-"; } } } public void setPiece(int x, int y, String piece) { board[x][y] = piece; } public void printBoard() { for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { System.out.print(board[i][j] + " "); } System.out.println(); } } public ChessBoardMemento save() { return new ChessBoardMemento(board); } public void restore(ChessBoardMemento memento) { board = memento.getBoard(); } } ``` ChessBoardMemento类定义: ```java public class ChessBoardMemento { private String[][] board; public ChessBoardMemento(String[][] board) { this.board = new String[board.length][]; for (int i = 0; i < board.length; i++) { this.board[i] = Arrays.copyOf(board[i], board[i].length); } } public String[][] getBoard() { return board; } } ``` ChessBoardCaretaker类定义: ```java import java.util.Stack; public class ChessBoardCaretaker { private Stack<ChessBoardMemento> mementos = new Stack<>(); public void saveMemento(ChessBoardMemento memento) { mementos.push(memento); } public ChessBoardMemento getMemento() { if (mementos.empty()) { return null; } return mementos.pop(); } } ``` 现在我们可以使用这些类来实现象棋悔棋代码了: ```java public class Main { public static void main(String[] args) { ChessBoard board = new ChessBoard(8); board.setPiece(2, 3, "車"); board.setPiece(4, 5, "馬"); board.printBoard(); ChessBoardCaretaker caretaker = new ChessBoardCaretaker(); caretaker.saveMemento(board.save()); board.setPiece(3, 4, "炮"); board.printBoard(); caretaker.saveMemento(board.save()); board.setPiece(2, 3, "-"); board.setPiece(3, 4, "-"); board.printBoard(); board.restore(caretaker.getMemento()); board.printBoard(); board.restore(caretaker.getMemento()); board.printBoard(); } } ``` 输出结果: ``` - - - - - - - - - - - - - - - - - - - - - 車 - - - - - - - - - - - - - - - - 馬 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 炮 - - - - - - - - - - - - - - - - - 馬 - - - - - - - - - - - - - - 車 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 炮 - - - - - - - - - - - - - - - - - 馬 - - - - - - - - - - - - - - 車 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 車 - - - - - - - - - - - - - - - - 馬 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 炮 - - - - - - - - - - - - - - - - - 馬 - - - - - - - - - - - - - - 車 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 車 - - - - - - - - - - - - - - - - 馬 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 炮 - - - - - - - - - - - - - - - - - 馬 - - - - - - - - - - - - - - 車 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 車 - - - - - - - - - - - - - - - - 馬 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 炮 - - - - - - - - - - - - - - - - - 馬 - - - - - - - - - - - - - - - 車 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值