设计模式-Study(尚硅谷)
课程地址:https://www.bilibili.com/video/BV1G4411c7N4
课件资料地址:https://pan.baidu.com/s/1a-cOqagjI9-5rBwb3pFNOA?pwd=pn1e
一、设计模式七大原则
编写软件过程中,程序员面临着来自耦合性,内聚性以及可维护性,可扩展性,重用性,灵活性等多方面的挑战,设计模式就是对软件设计中普遍存在(反复出现)的问题提出的解决方案。
设计模式是为了让程序具有更好:
- 代码重用性(即:相同功能的代码,不用多次编写)
- 可读性(即:编程规范性,便于其他程序员的阅读和理解)
- 可扩展性(即:当需要增加新的功能时,非常的方便,称为可维护)
- 可靠性 (即:当我们增加新的功能后,对原来的功能没有影响)
- 使程序呈现高内聚,低耦合的特性
设计模式七大原则:
- 1)单一职责原则
- 2)接口隔离原则
- 3)依赖倒转原则
- 4)里氏替换原则
- 5)开闭原则
- 6)迪米特法则
- 7)合成复用原则
二十三种设计模式
- 1)创建型模式:单例模式、抽象工厂模式、原型模式、建造者模式、工厂模式。
- 2)结构型模式:适配器模式、桥接模式、装饰模式、组合模式、外观模式、享元模式、代理模式。
- 3)行为型模式:模版方法模式、命令模式、访问者模式、迭代器模式、观察者模式、中介者模式、备忘录模式、 解释器模式(Interpreter 模式)、状态模式、策略模式、职责链模式(责任链模式)。
1.1、单一职责原则
对类来说,即一个类只能负责一项职责;
如类 A 负责两个不同职责:职责 1,职责 2;
当职责 1 需求变更而改变 A 时,可能造成职责 2 执行错误,所以需要将类 A 的粒度分解为 A1,A2。
方式一
public class SingleResponsibility1 {
public static void main(String[] args) {
Vehicle vehicle = new Vehicle();
vehicle.run("摩托车");
vehicle.run("汽车");
vehicle.run("飞机");
}
}
class Vehicle{
public void run(String vehicle){
System.out.println(vehicle+"在公路上跑");
}
}
问题分析
1)运行结果中不管是摩托车,飞机还是汽车,都是在公路上运行,违反了单一职责原则
2)解决办法,我们应该将Vehicle 分解成不同的类
方式二
public class SingleResponsibility2 {
public static void main(String[] args) {
RoadVehicle roadVehicle = new RoadVehicle();
roadVehicle.run("汽车");
AirVehicle airVehicle = new AirVehicle();
airVehicle.run("飞机");
WaterVehicle waterVehicle = new WaterVehicle();
waterVehicle.run("轮船");
}
}
class RoadVehicle {
public void run(String verhicle) {
System.out.println(verhicle + "公路上");
}
}
class AirVehicle{
public void run(String verhicle) {
System.out.println(verhicle + "在天空");
}
}
class WaterVehicle{
public void run(String verhicle){
System.out.println(verhicle + "在水里");
}
}
问题分析
1) 遵守单一职责原则,但是这样做的改动很大,即将类分解,同时修改客户端
2)改进:直接修改 Vehicle 类,改动的代码会比较少=>方案 3
方式三
public class SingleResponsibility3 {
public static void main(String[] args) {
Vehicle2 vehicle2 = new Vehicle2();
vehicle2.run("摩托");
vehicle2.runAir("船");
vehicle2.runWater("飞机");
}
}
class Vehicle2{
public void run(String vehicle){
System.out.println(vehicle + "在公路上运行");
}
public void runAir(String vehicle){
System.out.println(vehicle + "在天空运行");
}
public void runWater(String verhicle){
System.out.println(verhicle + "在水里");
}
}
分析
1)这种修改方法没有对原来的类做大的修改,只是增加方法
2)这里虽然没有在类这个级别上遵守单一职责原则,但是在方法级别上,仍然是遵守单一职责
注意事项和细节
- 1)降低类的复杂度,一个类只负责一个职责
- 2)提高类的可读性,可维护性
- 3)降低变更引起的风险
- 4)通常情况下,我们应当遵守单一职责原则,只有逻辑足够简单,才可以在代码级违反单一职责原则;只有类中方法数量足够少,可以在方法级别保持单一职责原则
1.2、接口隔离原则
客户端不应该依赖它不需要的接口,即一个类对另一个的依赖应该建立在最小接口上。

类 A 通过接口 Interface1 依赖类 B,类 C 通过接口 Interface1 依赖类 D;
如果接口 Interface1 对于类 A 和类 C来说不是最小接口,那么类 B 和类 D 必须去实现他们不需要的方法。
按隔离原则应当这样处理:
将接口 Interface1 拆分为独立的几个接口(这里我们拆分成 3 个接口),类 A 和类 C 分别与他们需要的接口建立依赖关系。也就是采用接口隔离原则
方式一
/**
* 接口隔离原则
*/
public class Segreation1 {
}
interface Interface1 {
void operation1();
void operation2();
void operation3();
void operation4();
void operation5();
}
class B implements Interface1 {
public void operation1() {
System.out.println("B实现了operation1");
}
public void operation2() {
System.out.println("B实现了operation2");
}
public void operation3() {
System.out.println("B实现了operation3");
}
public void operation4() {
System.out.println("B实现了operation4");
}
public void operation5() {
System.out.println("B实现了operation5");
}
}
lass D implements Interface1 {
public void operation1() {
System.out.println("D实现了operation1");
}
public void operation2() {
System.out.println("D实现了operation2");
}
public void operation3() {
System.out.println("D实现了operation3");
}
public void operation4() {
System.out.println("D实现了operation4");
}
public void operation5() {
System.out.println("D实现了operation5");
}
}
class A {
//A类通过依赖接口Interface1依赖使用B类,但只会使用1,2,3方法
public void depend1(Interface1 i) {
i.operation1();
}
public void depend2(Interface1 i) {
i.operation2();
}
public void depend3(Interface1 i) {
i.operation3();
}
}
class C {
//C类通过依赖接口Interface1依赖使用B类,但只会使用1,4,5方法
public void depend1(Interface1 i) {
i.operation1();
}
public void depend4(Interface1 i) {
i.operation4();
}
public void depend5(Interface1 i) {
i.operation5();
}
}
改进

public class Segreation2 {
}
interface Interface2{
void operation1();
}
interface Interface3{
void operation2();
void operation3();
}
interface Interface4{
void operation4();
void operation5();
}
class E implements Interface2, Interface3 {
public void operation1() {
System.out.println("B实现了operation1");
}
public void operation2() {
System.out.println("B实现了operation2");
}
public void operation3() {
System.out.println("B实现了operation3");
}
}
class F implements Interface2, Interface4 {
public void operation1() {
System.out.println("D实现了operation1");
}
public void operation4() {
System.out.println("D实现了operation4");
}
public void operation5() {
System.out.println("D实现了operation5");
}
}
class G{
//A类通过依赖接口Interface1依赖使用B类,但只会使用1,2,3方法
public void depend1(Interface2 i){
i.operation1();
}
public void depend2(Interface3 i){
i.operation2();
}
public void depend3(Interface3 i){
i.operation3();
}
}
class H {
//C类通过依赖接口Interface1依赖使用B类,但只会使用1,4,5方法
public void depend1(Interface2 i){
i.operation1();
}
public void depend4(Interface4 i){
i.operation4();
}
public void depend5(Interface4 i){
i.operation5();
}
}
1.3、依赖倒转(倒置)原则
依赖倒转原则是指:
- 1)高层模块不应依赖低层模块,二者都应该依赖其抽象(比如抽象类,接口)
- 2)抽象不应该依赖细节,细节应该依赖抽象
- 3)依赖倒转(倒置)中心思想就是面向接口编程
- 4)依赖倒转原则是基于这样的设计理念:相对于细节的多变性,抽象的东西要稳定的多。以抽象为基础搭建的架构比以细节为基础的架构要稳定的多。在 java 中,抽象指的是接口或抽象类,细节就是具体的实现类
- 5) 使用接口或抽象类的目的是制定好规范,而不涉及任何具体的操作,把展现细节的任务交给他们的实现类去完成。
应用实例
编程完成 Person 接收消息的功能
public class DependecyInversion {
public static void main(String[] args) {
person person = new person();
person.receive(new Email());
}
}
class Email{
public String getInfo(){
return "邮箱:hello,world";
}
}
class person{
public void receive(Email email){
System.out.println("收到:"+email.getInfo());
}
}
问题分析
- 1)如果我们获取的对象是微信,短信等等,则新增类,同时Perons也要增加相应的接收方法
- 2)解决思路:引入一个抽象的接口IReceiver,表示接收者,这样Person类与IReceiver发生依赖,因为Email, WeiXin等等属于接收的范围,他们各自实现IReceiver接口就ok,这样我们就符号依赖倒转原则
改进
public class DependecyInversion {
public static void main(String[] args) {
person person = new person();
person.receive(new Email());
person.receive(new weixin());
}
}
interface IReceive {
String getInfo();
}
class Email implements IReceive {
public String getInfo() {
return "邮箱:hello,world";
}
}
class weixin implements IReceive {
public String getInfo() {
return "微信:hello,world";
}
}
class person {
//对接口的依赖
public void receive(IReceive iReceive) {
System.out.println("收到:" + iReceive.getInfo());
}
}
依赖传递的三种方式
接口传递
public class DependecyPass {
public static void main(String[] args) {
OpenAndClose openAndClose = new OpenAndClose();
openAndClose.open(new changhong());
openAndClose.open(new xiaomi());
}
}
interface Itv{
public void play();
}
class changhong implements Itv{
public void play() {
System.out.println("长虹电视机打开");
}
}
class xiaomi implements Itv{
public void play() {
System.out.println("小米电视机打开");
}
}
class OpenAndClose{
public void open(Itv itv) {
itv.play();
}
}
构造方法传递
public class DependecyPass {
public static void main(String[] args) {
OpenAndClose openAndClose1 = new OpenAndClose(new changhong());
openAndClose1.open();
OpenAndClose openAndClose2 = new OpenAndClose(new xiaomi());
openAndClose2.open();
}
}
interface Itv{
public void play();
}
class changhong implements Itv{
public void play() {
System.out.println("长虹电视机打开");
}
}
class xiaomi implements Itv{
public void play() {
System.out.println("小米电视机打开");
}
}
class OpenAndClose{
private Itv itv;
public OpenAndClose(Itv itv){
this.itv=itv;
}
public void open() {
this.itv.play();
}
}
setter方法传递
public class DependecyPass {
public static void main(String[] args) {
OpenAndClose openAndClose = new OpenAndClose();
openAndClose.settle(new changhong());
openAndClose.open();
openAndClose.settle(new xiaomi());
openAndClose.open();
}
}
interface Itv{
public void play();
}
class changhong implements Itv{
public void play() {
System.out.println("长虹电视机打开");
}
}
class xiaomi implements Itv{
public void play() {
System.out.println("小米电视机打开");
}
}
class OpenAndClose{
private Itv itv;
public void settle(Itv itv){
this.itv=itv;
}
public void open() {
this.itv.play();
}
}
依赖倒转原则的注意事项和细节
- 1)低层模块尽量都要有抽象类或接口,或者两者都有,程序稳定性更好.
- 2)变量的声明类型尽量是抽象类或接口, 这样我们的变量引用和实际对象间,就存在一个缓冲层,利于程序扩展和优化
- 3)继承时遵循里氏替换原则