参考内容:https://www.cnblogs.com/liebrother/p/10941660.html
一、 单一职责原则
package day01.designPattern.srp;
/**
* @description 单一职责原则适用的范围有接口、方法、类。
* 按大家的说法,接口和方法必须保证单一职责,类就不必保证,只要符合业务就行。
* @author ZerlindaLi create at 2019/6/11 16:13
* @version 1.0.0
* @description Shopping
*/
public class SrpTest{
public static void main(String[] args) {
}
}
interface Shopping {
void doShopping();
}
interface PourGarbage {
void doPourGarbage();
}
interface WashingUp{
void doWashingUp();
}
class XiaoMing implements PourGarbage {
@Override
public void doPourGarbage() {
System.out.println(("pourGarbage..."));
}
}
class XiaoHong implements Shopping, WashingUp {
@Override
public void doShopping() {
System.out.println(("Shopping..."));
}
@Override
public void doWashingUp() {
System.out.println(("doWashingUp..."));
}
}
二、开闭原则
package day01.designPattern.ocp;
import java.math.BigDecimal;
/**
* 开闭原则:当应用的需求改变时,在不修改软件实体的源代码或者二进制代码的前提下,可以扩展模块的功能,使其满足新的需求。
* 对扩展开放,对修改封闭
* 抽象约束,封装变化,派生类
* @author ZerlindaLi create at 2019/5/28 17:09
* @version 1.0.0
* @description OcpTest
*/
public class OcpTest {
public static void main(String[] args) {
Hospital h = new Hospital();
Medicine m = new Medicine("阿司匹林",new BigDecimal(12.34));
OneLevelSocialSecurityPatient chf = new OneLevelSocialSecurityPatient("程海锋");
h.sellMedicine(chf, m);
TwoLevelSocialSecurityPatient zl = new TwoLevelSocialSecurityPatient("郑龙");
h.sellMedicine(zl, m);
ThreeLevelSocialSecurityPatient zc = new ThreeLevelSocialSecurityPatient("张超");
h.sellMedicine(zc, m);
}
}
class Medicine {
private String name;
private BigDecimal price;
public Medicine(String name, BigDecimal price){
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
}
class Hospital {
public void sellMedicine(IPatient patient, Medicine medicine){
BigDecimal money = patient.pay(medicine);
System.out.println(patient.getName() + "花了" + money.setScale(2, BigDecimal.ROUND_UP) + "块钱买了药:" + medicine.getName());
}
}
interface IPatient {
String getName();
BigDecimal pay(Medicine medicine);
}
/**
* 一档社保
*/
class OneLevelSocialSecurityPatient implements IPatient {
private String name;
public OneLevelSocialSecurityPatient(String name) {
this.name = name;
}
@Override
public String getName() {
return this.name;
}
@Override
public BigDecimal pay(Medicine medicine) {
return medicine.getPrice().multiply(new BigDecimal(0.7));
}
}
/**
* 二挡社保
*/
class TwoLevelSocialSecurityPatient implements IPatient {
private String name;
public TwoLevelSocialSecurityPatient(String name) {
this.name = name;
}
@Override
public String getName() {
return this.name;
}
@Override
public BigDecimal pay(Medicine medicine) {
return medicine.getPrice().multiply(new BigDecimal(0.8));
}
}
/**
* 三挡社保
*/
class ThreeLevelSocialSecurityPatient implements IPatient {
private String name;
public ThreeLevelSocialSecurityPatient(String name) {
this.name = name;
}
@Override
public String getName() {
return this.name;
}
@Override
public BigDecimal pay(Medicine medicine) {
return medicine.getPrice().multiply(new BigDecimal(0.9));
}
}
三、里氏替换原则
package day01.designPattern.lsp;
/**
* 里氏替换原则:
* 如果对每一个类型为S的对象o1,都有类型为T的对象o2,使得以T定义的所有程序P在所有的对象o1都代换成o2时,程序P的行为没有发生变化,
* 那么类型S是类型T的子类型。
* 所有引用基类的地方必须能透明地使用其子类的对象。
*
* 子类继承父类,且不重写父类的方法。
* @author ZerlindaLi create at 2019/5/28 11:41
* @version 1.0.0
* @description LSPTest
*/
public class LspTest {
public static void main(String[] args) {
Father f = new Father();
Son1 s1 = new Son1();
Son2 s2 = new Son2();
f.makeMechine();
s1.makeMechine();
s2.makeMechine();
s2.makeWeapon();
}
}
class Father{
public void makeMechine(){
System.out.println("制造冷兵器");
}
}
class Son1 extends Father {
@Override
public void makeMechine(){
System.out.println("制造核武器");
}
}
class Son2 extends Father {
public void makeWeapon(){
System.out.println("制造核武器");
}
}
四、迪米特法则
package day01.designPattern.lod;
/**
* @author ZerlindaLi create at 2019/7/2 17:12
* @version 1.0.0
* @description 迪米特法则
* Each unit should have only limited knowledge about other units: only units "closely" related to the current unit. (每个单元对于其他的单元只能拥有有限的知识:只是与当前单元紧密联系的单元)
* Each unit should only talk to its friends; don't talk to strangers. (每个单元只能和它的朋友交谈:不能和陌生单元交谈)
* Only talk to your immediate friends. (只和自己直接的朋友交谈)
* (来自维基百科)
*
* 平常在零碎的时间里,喜欢看一些书籍,一般都是电子书,现在我们看书的操作是这样的:唤醒手机,打开阅读软件,选择书籍,然后阅读。总共 3 个步骤,涉及了 3 样东西:手机、软件、书籍。同学们用代码实现这个过程。
*/
public class LODRightTest {
public static void main(String[] args) {
Phone phone = new Phone();
phone.readBook();
}
}
class Book {
private String title;
public Book(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
class App {
private Book book = new Book("设计模式");
public void read(){
System.out.println(book.getTitle());
}
}
class Phone {
private App app = new App();
public void readBook() {
app.read();
}
}
五、接口隔离原则
package day01.designPattern.isp;
/**
* @description 接口隔离原则
* 1. Clients should not be forced to depend upon interfaces that they don't use.(客户端不应该依赖它不需要的接口。)
* 2. The dependency of one class to another one should depend on the smallest possible interface.(类间的依赖关系应该建立在最小的接口上。)
* @author ZerlindaLi create at 2019/6/11 16:44
* @version 1.0.0
* @description IspTest
*/
public class IspTest {
public static void main(String[] args) {
// 测试代码
LiAunt liAunt = new LiAunt();
WangMather wangMather = new WangMather();
ChenUncle chenUncle = new ChenUncle();
HuangSister huangSister = new HuangSister();
liAunt.hairBraiding();
wangMather.getDressed();
chenUncle.packingIntoTheBox();
huangSister.makeTag();
}
}
interface Hair {
void hairBraiding();
}
interface Dress {
void getDressed();
}
interface Box {
void packingIntoTheBox();
}
interface Tag {
void makeTag();
}
class LiAunt implements Hair {
@Override
public void hairBraiding() {
System.out.println("李大姨给布娃娃扎辫子");
}
}
class WangMather implements Dress {
@Override
public void getDressed() {
System.out.println("王大妈给布娃娃穿衣服");
}
}
class ChenUncle implements Box {
@Override
public void packingIntoTheBox() {
System.out.println("陈大叔给布娃娃装箱");
}
}
class HuangSister implements Tag {
@Override
public void makeTag() {
System.out.println("黄大姐给箱子打标签");
}
}
六、依赖倒置原则
package day01.designPattern.dip;
/**
* @author ZerlindaLi create at 2019/5/29 15:42
* @version 1.0.0
* @description 依赖倒置原则
*
* High level modules should not depend upon low level modules.Both should depend upon abstractions. 高层模块不应该依赖低层模块,两者都应该依赖其抽象(模块间的依赖通过抽象发生,实现类之间不发生直接的依赖关系,其依赖关系是通过接口或抽象类产生的)
* Abstractions should not depend upon details. 抽象不应该依赖细节(接口或抽象类不依赖于实现类)
* Details should depend upon abstractions. 细节应该依赖抽象(实现类依赖接口或抽象类)
*/
abstract class Boss {
private Staff staff;
public Boss(Staff staff) {
this.staff = staff;
}
/**
* 提供支持
*/
abstract void support();
/**
* 需求帮助
* @param boss
*/
abstract void askHelp(Boss boss);
public void setStaff(Staff staff) {
this.staff = staff;
}
/**
* 招募员工
* @return
*/
public Staff getStaff() {
return staff;
}
}