1.单例模式
public class SingletionStarving {
private static final SingletionStarving mInstance = new SingletionStarving();
private SingletionStarving() { }
public static SingletionStarving getInstance() {
return mInstance;
}
}
//SingletionStarving.getInstance();
2.builder模式
public class Student {
private final int stuId;//必须
private final String name;//必须
private final int age;//可选
private final int gender;//可选
private final String address;//可选
public int getStuId() {
return stuId;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public int getGender() {
return gender;
}
public String getAddress() {
return address;
}
private Student(StudentBuilder builder){
this.stuId = builder.stuId;
this.name = builder.name;
this.age = builder.age;
this.gender = builder.gender;
this.address = builder.address;
}
public static class StudentBuilder{
private final int stuId;
private final String name;
private int age;
private int gender;
private String address;
public StudentBuilder(int stuId,String name){
this.stuId = stuId;
this.name = name;
}
public StudentBuilder setAge(int age){
this.age = age;
return this;
}
public StudentBuilder setGender(int gender){
this.gender = gender;
return this;
}
public StudentBuilder setAddress(String address){
this.address = address;
return this;
}
public Student build(){
return new Student(this);
}
}
}
// Student studen= new Student.StudentBuilder(1,"小明")//必填属性在构造方法中赋值
// .setAge(1)//设置可选属性 年龄
// .setGender(1)//设置可选属性 性别 默认1为男
// .build();//对象构建完毕的标识,返回Student对象
// Log.e("","studen=="+studen.getName().toString());
3.责任链模式
public interface Handler {
/**
* 处理请求
* @param dayNums
*/
void handleRequest(int dayNums);
/**
* 设置下一个执行者
*/
void setSuccesor(Handler handler);
/**
* 获取下一个执行者
* @return
*/
Handler getNextSucccesor();
}
public class Leader implements Handler {
public String mName;
public Handler mNextHandler;
public int mCouldHandlerNum;
public Leader(String name, int couldHandlerNum) {
mName = name;
mCouldHandlerNum = couldHandlerNum;
}
@Override
public void setSuccesor(Handler handler) {
mNextHandler = handler;
}
@Override
public Handler getNextSucccesor() {
return mNextHandler;
}
//当请求天数少于 mCouldHandlerNum 即能够处理的最大天数,直接交给当前处理者处理
//否则,交给下一个处理者处理。
@Override
public void handleRequest(int dayNums) {
if (dayNums <= mCouldHandlerNum) {
System.out.println(mName + " 同意了你的申请, dayNums = " + dayNums);
} else {
Handler nextSucccesor = getNextSucccesor();
if (nextSucccesor != null) {
nextSucccesor.handleRequest(dayNums);
} else {
System.out.println(mName + " 拒绝了你的申请 dayNums = " + dayNums);
}
}
}
}
public class TeamLeader extends Leader {
public TeamLeader(String name, int couldHandlerNum) {
super(name, couldHandlerNum);
}
}
public class GeneralManager extends Leader{
public GeneralManager(String name, int couldHandlerNum) {
super(name, couldHandlerNum);
}
}
public class Departmentdirector extends Leader{
public Departmentdirector(String name, int couldHandlerNum) {
super(name, couldHandlerNum);
}
}
// Leader directLeader = new TeamLeader("TeamLeader", 3);
// Leader departmentdirector = new Departmentdirector("Departmentdirector", 7);
// Leader generalManager = new GeneralManager("GeneralManager", Integer.MAX_VALUE);
//
// directLeader.setSuccesor(departmentdirector);//设置下一个执行者,从而形成链表
// departmentdirector.setSuccesor(generalManager);
//
// // 请假三天
// directLeader.handleRequest(3);//TeamLeader 同意了你的申请, dayNums = 3
// // 请假六天
// directLeader.handleRequest(6);//Departmentdirector 同意了你的申请, dayNums = 6
// // 请假100天
// directLeader.handleRequest(100);//GeneralManager 同意了你的申请, dayNums = 100
|