一、facade(门面模式)
1、思想
大量对象集中在一个门面对象里面,统一进行管理,进行方法调用。
2、步骤
1. 声明多个普通类。
2. 创建门面类,门面对象里面通过static块初始化要管理的对象,赋值给成员变量。
3. 门面对象的方法里面按需求调用不同的成员变量方法
4. 客户端创建门面对象,通过门面对象调用方法去执行被管理对象的方法。
3、步骤
//心脏科
public class heart {
public void consult(){
System.out.println("咨询心脏手术");
}
public void exec(){
System.out.println("心脏手术");
}
}
//骨科
public class orthopaedics {
public void consult(){
System.out.println("咨询骨科手术");
}
public void exec(){
System.out.println("骨科手术");
}
}
//接待员
public class reception {
private static orthopaedics orthopaedics;
private static heart heart;
static {
orthopaedics=new orthopaedics();
heart=new heart();
}
public void exec(){
heart.exec();
orthopaedics.exec();
}
public void consult(){
heart.consult();
orthopaedics.consult();
}
}
//门面模式,将多个对象复杂的操作,全部通过持有对象的方式,集中在一个门面对象中,
//通过门面对象去调用方法。
public class facadeTest {
public static void main(String[] args) {
reception reception=new reception();
reception.exec();
}
}
二、flyweight(享元模式)
1、思想
池化思想,已经存在的直接返回,没有的就创建然后加入到池里面去再返回。
2、步骤
1. 创建对象池类,创建获取对象的方法
2. 根据传入的key判断,如果池里面存在就返回
3. 如果不存在就创建然后加入到池里面去
(数据库、map、redis、文件等凡是可以用key-val值的存储池)
3、代码
//图像接口
public interface IShape {
public void draw();
}
//圆形
public class Circle implements IShape{
//颜色
private String color;
//x轴
private int x;
//y轴
private int y;
//半径
private int radius;
public Circle(String color) {
this.color = color;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getRadius() {
return radius;
}
public void setRadius(int radius) {
this.radius = radius;
}
@Override
public String toString() {
return "Circle{" +
"color='" + color + '\'' +
", x=" + x +
", y=" + y +
", radius=" + radius +
'}';
}
@Override
public void draw() {
System.out.println(this.toString());
}
}
//图形工厂
public class ShopFactory {
//图形池
private static final HashMap<String, IShape> circleMap=new HashMap<>();
//获取图形,没有就新增到池子里
public static IShape getCircle(String color){
IShape shape=circleMap.get(color);
if (shape==null){
shape=new Circle(color);
circleMap.put(color,shape);
}
return shape;
}
}
//享元模式,就是池化思想,用key为标识,池子里面没有的就生成了加到池子里面去
public class flyweightTest {
private static final String colors[] =
{ "Red", "Green", "Blue", "White", "Black" };
public static void main(String[] args) {
for(int i=0; i < 20; ++i) {
Circle circle =
(Circle)ShopFactory.getCircle(getRandomColor());
circle.setX(getRandomX());
circle.setY(getRandomY());
circle.setRadius(100);
circle.draw();
}
}
private static String getRandomColor() {
return colors[(int)(Math.random()*colors.length)];
}
private static int getRandomX() {
return (int)(Math.random()*100 );
}
private static int getRandomY() {
return (int)(Math.random()*100);
}
}
三、composite(组合模式)
1、思想
就是树状结构,重点在于类里面包含一个list<类>的属性,也就是包含子节点。
2、步骤
- 创建类对象,创建成树状结构
- 外部可以用类对象来添加、删除、遍历树状结构。
3、代码
//员工实体,树状结构
public class Employee {
private String name;
private String dept;
private int salary;
private List<Employee> subordinates;
public Employee(String name,String dept, int sal) {
this.name = name;
this.dept = dept;
this.salary = sal;
subordinates = new ArrayList<Employee>();
}
public void add(Employee e) {
subordinates.add(e);
}
public void remove(Employee e) {
subordinates.remove(e);
}
public List<Employee> getSubordinates(){
return subordinates;
}
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", dept='" + dept + '\'' +
", salary=" + salary +
", subordinates=" + subordinates +
'}';
}
}
//组合模式,也就是树状结构,重点在于实体类里面加一个list<实体类>属性,
//声明一个add方法,为对象添加叶子节点
public class CompositeTest {
public static void main(String[] args) {
Employee CEO = new Employee("John","CEO", 30000);
Employee headSales = new Employee("Robert","Head Sales", 20000);
Employee headMarketing = new Employee("Michel","Head Marketing", 20000);
Employee clerk1 = new Employee("Laura","Marketing", 10000);
Employee clerk2 = new Employee("Bob","Marketing", 10000);
Employee salesExecutive1 = new Employee("Richard","Sales", 10000);
Employee salesExecutive2 = new Employee("Rob","Sales", 10000);
CEO.add(headSales);
CEO.add(headMarketing);
headSales.add(salesExecutive1);
headSales.add(salesExecutive2);
headMarketing.add(clerk1);
headMarketing.add(clerk2);
//打印该组织的所有员工
System.out.println(CEO);
for (Employee headEmployee : CEO.getSubordinates()) {
System.out.println(headEmployee);
for (Employee employee : headEmployee.getSubordinates()) {
System.out.println(employee);
}
}
}
}
四、template(模板模式)
1、思想
抽象类里面定义好骨架,具体的实现由不同的抽象子类去实现。
抽象类里面的一个方法,里面包含多个其它抽象方法的调用,
其它的抽象方法具体逻辑由不同的抽象子类去实现。
2、步骤
1. 创建抽象类,抽象类里面声明一个骨架方法,里面包含多个其它的抽象方法。
2. 创建抽象子类,重写抽象方法(除了骨架方法)。
3. 在客户端调用抽象类的骨架方法,就可以按照骨架方法里面的逻辑执行其它由
子类实现的方法。
3、代码
//抽象模板类,定义骨架, 普通方法里面实现整体逻辑,具体的逻辑又子类继承模板对象后,重写抽象方法来实现
public abstract class AbstractSetting {
public final String getSetting(String key){
String val=getLocal(key);
if (val==null){
val=getRetome(key);
}
return val;
}
protected abstract String getLocal(String key);
protected abstract String getRetome(String key);
}
//具体实现类,实现抽象类里面的抽象方法逻辑
public class RedisSetting extends AbstractSetting{
@Override
protected String getLocal(String key) {
return null;
}
@Override
protected String getRetome(String key) {
Map<String,String> map=new HashMap<>();
map.put("1","1");
return map.get(key);
}
}
//模板方法,抽象类里面实现骨架,普通方法里面包含抽象方法,抽象方法里面的具体逻辑又子类实现
public class templateTest {
public static void main(String[] args) {
AbstractSetting setting=new RedisSetting();
System.out.println(setting.getSetting("1"));
}
}