建站之星网站建设下载版,宁波东方论坛,网站建设实施过程,抖音带运营3种合作方式一 前言
经常听说各种设计模式#xff0c;知道理论#xff0c;也知道应该使用#xff0c;但具体怎么用#xff0c;什么时候用#xff0c;使用的优点一直比较模糊#xff0c;今天写一个项目中经常用到的模式#xff0c;来具体理解。项目中经常用到工厂模式或者策略模式知道理论也知道应该使用但具体怎么用什么时候用使用的优点一直比较模糊今天写一个项目中经常用到的模式来具体理解。项目中经常用到工厂模式或者策略模式偶尔也会一起使用来增强代码管理今天就一起使用来理解。
二 实现
不废话直接上代码最后解释 先写一个接口 Person 类
package com.fcy.demo.entity;public interface Person {void eat();void run();
}
然后使用策略模式根据不同的人的类型创建不同的人实现该接口这里有三种类型 Teacher, Doctor, Worker, 首先定义一个常量类包含这三种类型的人。
package com.fcy.demo.constant;public class PersonType {public static final String TEACHER teacher;public static final String DOCTOR doctor;public static final String WORKER worker;
}
接着写三种人的实现类
package com.fcy.demo.entity;import com.fcy.demo.constant.PersonType;
import org.springframework.stereotype.Component;Component(PersonType.TEACHER)
public class Teacher implements Person{Overridepublic void eat() {System.out.println(老师吃);}Overridepublic void run() {System.out.println(老师跑);}
}
package com.fcy.demo.entity;import com.fcy.demo.constant.PersonType;
import org.springframework.stereotype.Component;Component(PersonType.DOCTOR)
public class Doctor implements Person{Overridepublic void eat() {System.out.println(医生吃);}Overridepublic void run() {System.out.println(医生跑);}
}
package com.fcy.demo.entity;import com.fcy.demo.constant.PersonType;
import org.springframework.stereotype.Component;Component(PersonType.WORKER)
public class Worker implements Person{Overridepublic void eat() {System.out.println(工人吃);}Overridepublic void run() {System.out.println(工人跑);}
}
到此为什么使用策略模式呢因为如果不使用策略模式所有代码都写在一个实现类里当用户想让一个老师吃的时候代码会变得比较繁琐类似
public void eat(PersonType type){if (PersonType.TEACHER.equals(type)){System.out.println(老师吃);} else if (PersonType.DOCTOR.equals(type)){System.out.println(医生吃);} else (PersonType.WORKER.equals(type)){System.out.println(工人吃);}
}这里只有三种人如果有十种甚至更多会有更多的 if…else…, 同理对 run 操作也会有这么多的判断实在难以维护后续对某一类型的 eat 行为做更改时也会影响其他代码违背了开闭原则即对扩展开放对修改关闭。因此使用策略模式使不同类型的人实现同一接口当对某一类型的行为做更改时不会影响其他类型的代码。 接下来说工厂模式 上面代码中重点是 Component(PersonType.WORKER) 定义了 Component 里的参数使得之后使用工厂模式时可以知道工厂该生产那种人。 到此为什么使用工厂模式呢如果不使用工厂模式仅使用策略模式同理当我想要一个老师吃的时候我需要自己实例一个老师类类似下面代码
public void eat(PersonType type){if (PersonType.TEACHER.equals(type)){Teacher teacher new Teacher();teacher.eat();} else if (PersonType.DOCTOR.equals(type)){Doctor doctor new Doctor();doctor.eat();} else (PersonType.WORKER.equals(type)){Worker worker new Worker();worker.eat();}
}虽然我们使用了策略模式使代码满足开闭原则易于维护但并没有解决这么多 if…else… 的问题所以我们使用工厂模式减少判断上代码
package com.fcy.demo.factory;import com.fcy.demo.entity.Person;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;Component
public class PersonFactor {Autowiredprivate MapString, Person personTypeMap;public Person getPerson(String personType) {return personTypeMap.get(personType);}
}
至此我们就不需要这么多的判断了当需要某种类型的人时直接告诉工厂就可以了controller 代码如下
package com.fcy.demo.controller;import com.fcy.demo.entity.Person;
import com.fcy.demo.factory.PersonFactor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;RestController
RequestMapping(/person)
public class PersonController {Autowiredpublic PersonFactor personFactor;GetMapping(/teacher)public void getTeacher(){Person teacher personFactor.getPerson(teacher);teacher.eat();}
}
启动代码访问 http://localhost:8080/person/teacher 发现控制台输出 “老师吃” 测试成功至此代码完成也可以在 controller 中写其他的方法。
三 总结
在上面中解释了为什么使用 策略模式以及工厂模式以及使用的优点还有一个问题没有说就是什么时候使用该模式, 在一类事物有共同的行为但行为模式不同时就可以使用。 就到这吧至此关于 策略模式与工厂模式有了一个基本理解也可以实现了。