设计模式之单一职责模式
常用 在只有一个变量发生改变造成整个数据的变动等
实现方法 写一个公共接口 在用不同的变量 去实现该接口方法就行
接口例子
/**
* 单一职责模式
*
* @Date:2022/4/9
* @author: ZLF
*/
public interface ServiceSingleResponsibilityModel01 {
/**
* 画质
*/
void imageQuality();
/**
* 广告
*/
void advertising();
}
实现方法
/**
* @Date:2022/4/9
* @author: ZLF
*/
public class Tourists implements ServiceSingleResponsibilityModel01 {
@Override
public void imageQuality() {
System.out.println("360P");
}
@Override
public void advertising() {
System.out.println("广告90秒");
}
}
/**
* @Date:2022/4/9
* @author: ZLF
*/
public class TheUser implements ServiceSingleResponsibilityModel01 {
@Override
public void imageQuality() {
System.out.println("720P");
}
@Override
public void advertising() {
System.out.println("广告30秒");
}
}
/**
* @Date:2022/4/9
* @author: ZLF
*/
public class Vip implements ServiceSingleResponsibilityModel01 {
@Override
public void imageQuality() {
System.out.println("1080P");
}
@Override
public void advertising() {
System.out.println("VIP无广告");
}
}
测试接口
import org.junit.Test;
/**
* @Date:2022/4/9
* @author: ZLF
*/
public class TestSingleResponsibilityModel {
@Test
public void test1() {
TheUser theUser = new TheUser();
theUser.advertising();
theUser.imageQuality();
}
@Test
public void test2() {
Tourists tourists = new Tourists();
tourists.advertising();
tourists.imageQuality();
}
@Test
public void test3() {
Vip vip = new Vip();
vip.advertising();
vip.imageQuality();
}
}
总结
总体来讲就是把唱用的if else if 等只有一个条件改变时抽取出来放入接口 通过不同的类来实现对应的方法 从而解决if else的屎山问题 。
开闭原则
扩展原有的代码 但不能修改原有的方法 例如原方法提供的公式不能变动 但是值的精确度变了这个时候可以用开闭原则进行继承后修改精度(new 新的类)来实现
接口
package com.example.设计模式.开闭原则;
/**
* 计算面机公式
*
* @Date:2022/4/10
* @author: ZLF
*/
public interface ICalculationArea {
/**
* 圆周率
* @param r 半径
* @return
*/
double round(double r);
}
实现接口
package com.example.设计模式.开闭原则;
/**
* @Date:2022/4/10
* @author: ZLF
*/
public class OneCalculationArea implements ICalculationArea {
private final double p = 3.14;
@Override
public double round(double r) {
return 2 * p * r;
}
}
继承类对熟悉进行修改等
package com.example.设计模式.开闭原则;
/**
* @Date:2022/4/10
* @author: ZLF
*/
public class TwoCalculationArea extends OneCalculationArea {
private final double p = 3.1415926;
@Override
public double round(double r) {
return 2 * p * r;
}
}
测试
package com.example.设计模式.开闭原则;
import org.junit.Test;
/**
* @Date:2022/4/10
* @author: ZLF
*/
public class TestCalculationArea {
@Test
public void roundOne() {
ICalculationArea oneCalculationArea = new OneCalculationArea();
System.out.println(oneCalculationArea.round(6));
}
@Test
public void roundTwo() {
ICalculationArea tneCalculationArea = new TwoCalculationArea();
System.out.println(tneCalculationArea.round(6));
}
}
里氏替换原则
继承后对原有的接口方法进行改造(银行卡 [储蓄卡 借记卡 信用卡] 银行卡为标准其他卡进行不同的方法改造)
迪米特原则
减少依赖,高内聚,低耦合(规模较大的公司 经理管队伍 总经理 直接找经理 减少底层的方法耦合)
实体类
package com.example.设计模式.迪米特原则;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @Date:2022/4/10
* @author: ZLF
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Department {
// 学生姓名
private String name;
// 总绩效
private int rank;
// 总工资
private double grade;
}
经理 计算 方法等
package com.example.设计模式.迪米特原则;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.ArrayList;
import java.util.List;
/**
* 经理
*
* @Date:2022/4/10
* @author: ZLF
*/
@Data
@NoArgsConstructor
public class TheManager {
private String name;
private String departmentOne;
private static List<Department> departmentList;
public TheManager(String name, String departmentOne) {
this.name = name;
this.departmentOne = departmentOne;
}
static {
departmentList = new ArrayList<>();
departmentList.add(new Department("JAVA", 10, 15000));
departmentList.add(new Department("Python", 8, 10000));
departmentList.add(new Department("C", 7, 10000));
departmentList.add(new Department("C++", 9, 11000));
departmentList.add(new Department("C", 10, 14000));
}
// 绩效
public double clazzRank() {
double totalScore = 0;
for (Department department : departmentList) {
totalScore += department.getRank();
}
return totalScore;
}
// 平均工资
public double clazzGrade() {
double totalScore = 0;
for (Department department : departmentList) {
totalScore += department.getGrade();
}
return totalScore / TheManager.departmentList.size();
}
// 部门人数
public int clazzDepartmentCount() {
return TheManager.departmentList.size();
}
}
总经理 只负责调用不需要管下面个人
package com.example.设计模式.迪米特原则;
/**
* @Date:2022/4/10
* @author: ZLF
*/
public class TheGeneralManager {
public void theManagerOne() {
TheManager theManager = new TheManager();
double v = theManager.clazzGrade();
double v1 = theManager.clazzRank();
int i = theManager.clazzDepartmentCount();
System.out.println("平均工资"+v);
System.out.println("总绩效"+v1);
System.out.println("总人数"+i);
}
}
测试
package com.example.设计模式.迪米特原则;
import org.junit.Test;
/**
* @Date:2022/4/10
* @author: ZLF
*/
public class TestTheGeneralManager {
@Test
public void test01() {
TheGeneralManager theGeneralManager = new TheGeneralManager();
theGeneralManager.theManagerOne();
}
}
接口隔离原则
接口过大进行拆分化整为零
依赖倒置原则
写一个接口定义标准 写自己需要的类去详细实现该接口最后在其他类中进行调用