提交 3fcf9f8b 编写于 作者: C chenjianqiang

增加组合模式

上级 5c0b331c
......@@ -8,6 +8,27 @@
----FactoryPattern 工厂模式
----InterpreterPattern 解释器模式
----StrategyPattern 策略模式
----AdapterPattern 适配器模式
----BridgePattern 桥接模式
----BuilderPattern 构建者模式
----PrototypePattern 原型模式
----ProxyPattern 代理模式
----SingletonPattern 单例模式
----Pattern 模式
----Pattern 模式
----Pattern 模式
----Pattern 模式
----Pattern 模式
----Pattern 模式
----Pattern 模式
----Pattern 模式
----Pattern 模式
----Pattern 模式
----Pattern 模式
----Pattern 模式
----Pattern 模式
----Pattern 模式
----Pattern 模式
```
```
-----examples 贡献用户的真实业务代码案例
......
package com.pattern.CompositePattern.materials;
import com.pattern.CompositePattern.materials.po.ClassRoom;
/**
* 组合模式
* @author lx
* @date 2021/11/25 13:44
**/
public class CompositeMain {
public void main() {
ClassRoom classRoom = new ClassRoom("教室001",1);
classRoom.addStudent(new ClassRoom.Student("张开花",1));
classRoom.addStudent(new ClassRoom.Student("王大瓜",2));
classRoom.addStudent(new ClassRoom.Student("刘小艺",2));
System.out.println("教室有" +classRoom.countStudentSize() +"人");
}
}
package com.pattern.CompositePattern.materials.po;
import java.util.ArrayList;
import java.util.List;
/**
* 教室
*
* @author lx
* @date 2021/11/25 13:41
**/
public class ClassRoom {
private List<Student> students;
private String className;
private Integer studentSize;
public ClassRoom(String className, Integer studentSize) {
this.className = className;
this.studentSize = studentSize;
this.students = new ArrayList<>();
}
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public Integer getStudentSize() {
return studentSize;
}
public Integer countStudentSize() {
return students.size();
}
public void setStudentSize(Integer studentSize) {
this.studentSize = studentSize;
}
public Boolean addStudent(Student student) {
if (studentSize >= students.size()) {
return students.add(student);
} else {
System.err.println("教室已经满员,塞不进去了");
return Boolean.FALSE;
}
}
/**
* 学生
*/
public static class Student {
private String name;
/**
* 1 男 2女
*/
private Integer sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getSex() {
return sex;
}
public void setSex(Integer sex) {
this.sex = sex;
}
public Student(String name, Integer sex) {
this.name = name;
this.sex = sex;
}
}
}
package com.pattern.FilterPattern.materials;
/**
* 过滤器模式
*
* @author lx
* @date 2021/11/25 13:56
**/
public class FilterMain {
public static void main(String[] args) {
}
}
......@@ -100,10 +100,16 @@ public class PatternApplication {
/*
11.过滤模式
new BuilderMain().main();
new FilterMain().main();
2021-11-24:实现同一个抽象类为不同的方法,使用不同的实例处理相同的数据出来不同的结果
*/
/*
12.组合模式
new CompositeMain().main();
2021-11-25:一个实体包含了小规模业务
*/
}
}
......@@ -4,7 +4,7 @@ import com.pattern.PrototypePattern.materials.po.Shape;
import com.pattern.PrototypePattern.materials.po.ShapeCache;
/**
* 主进程
* 原型模式
* @author lx
* @date 2021/11/24 14:54
**/
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册