标签:类的构造函数 this 电脑 usb lazy java color 而且 不同的
以前对这种模式学完就忘,学完就忘,今天好好整理了下,以后应该不会忘了,这一整理就是好几个小时。
使用场景:当一个类的构造函数参数个数超过4个,而且这些参数有些是可选的参数,考虑使用构造者模式。
针对该场景:
1.我们可以提供带各种参数的构造器来实现,即折叠构造函数模式
2.java bean模式,就是new一个对象,然后设置各种参数
-------------
第一种主要是使用及阅读不方便。你可以想象一下,当你要调用一个类的构造函数时,你首先要决定使用哪一个,然后里面又是一堆参数,如果这些参数的类型很多又都一样,你还要搞清楚这些参数的含义,很容易就传混了。。。那酸爽谁用谁知道。
第二种方式在构建过程中对象的状态容易发生变化,造成错误。因为那个类中的属性是分步设置的,所以就容易出错。
实现方式一:在客户端使用链式调用,一步一步的把对象构建出来。
public class Computer { private String cpu;//必须 private String ram;//必须 private int usbCount;//可选 private String keyBoard;//可选 private String display;//可选 @Override public String toString() { return "Computer{" + "cpu=‘" + cpu + ‘\‘‘ + ", ram=‘" + ram + ‘\‘‘ + ", usbCount=" + usbCount + ", keyBoard=‘" + keyBoard + ‘\‘‘ + ", display=‘" + display + ‘\‘‘ + ‘}‘; } private Computer(Builder builder){ this.cpu = builder.cpu; this.ram = builder.ram; this.usbCount = builder.usbCount; this.keyBoard = builder.keyBoard; this.display = builder.display; } static class Builder{ private String cpu;//必须 private String ram;//必须 private int usbCount;//可选 private String keyBoard;//可选 private String display;//可选 public Builder(String cpu,String ram){ this.cpu = cpu; this.ram = ram; } public Builder setUsbCount(int usbCount) { this.usbCount = usbCount; return this; } public Builder setKeyBoard(String keyBoard) { this.keyBoard = keyBoard; return this; } public Builder setDisplay(String display) { this.display = display; return this; } public Computer builder(){ return new Computer(this); } } public static void main(String[] args) { //假设苹果电脑需要cpu:inter ram:三星 keyboard:键盘1 Computer computer = new Computer.Builder("inter","三星") .setKeyBoard("键盘1").builder(); System.out.println(computer); //假设联想电脑需要cpu:inter ram:三星 显示器:攀升 Computer computer2 = new Computer.Builder("inter","三星") .setDisplay("攀升").builder(); System.out.println(computer2); } }
分析:这种模式就两个类三个角色,一个是产品,一个是产品的构建者,还有一个是main函数里面的调度者Director。
这个构建者等于封装了产品的所有属性,实际情况中,可能有不同的构建者来封装各自的属性,就会有多个构建者,且这些构建者我们可以提取一个共同的抽象类或者接口。
实现方式二:传统模式
public class TraditionBuilder { public static void main(String[] args) { //小学生只需要地址 //下面两行代码可以提取另外一个类,作为Director AbsBuilder builder = new Pupil("小明",8); builder.setAddress(); Student student = builder.getStudent(); System.out.println(builder); //大学生需要地址和工号 builder = new UniversityStudent("宇轩",22); new StudentDirector().makeStudent(builder); student = builder.getStudent(); System.out.println(student); } } class StudentDirector{ public void makeStudent(AbsBuilder absBuilder){ absBuilder.setNo(); absBuilder.setAddress(); } } abstract class AbsBuilder{ abstract void setNo(); abstract void setAddress(); abstract Student getStudent(); } //大学生 class UniversityStudent extends AbsBuilder{ private Student student; public UniversityStudent(String name,int age){ student = new Student(name,age); } @Override void setNo() { student.setNo("456"); } @Override void setAddress() { student.setAddress("大明路"); } @Override Student getStudent() { return student; } @Override public String toString() { return "UniversityStudent{" + "student=" + student + ‘}‘; } } //小学生 class Pupil extends AbsBuilder{ private Student student; public Pupil(String name,int age){ student = new Student(name,age); } @Override void setNo() { student.setNo("123"); } @Override void setAddress() { student.setAddress("长安路"); } @Override Student getStudent() { return student; } @Override public String toString() { return "Pupil{" + "student=" + student + ‘}‘; } } class Student{ private String name; private int age; private String no; private String address; public Student(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getNo() { return no; } public void setNo(String no) { this.no = no; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "Student{" + "name=‘" + name + ‘\‘‘ + ", age=" + age + ", no=‘" + no + ‘\‘‘ + ", address=‘" + address + ‘\‘‘ + ‘}‘; } }
标签:类的构造函数 this 电脑 usb lazy java color 而且 不同的
原文地址:https://www.cnblogs.com/johnzhao/p/14672626.html