ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

图解java设计模式

2021-05-17 22:59:38  阅读:177  来源: 互联网

标签:设计模式 java void System tiger println 图解 public out


前言设计模式的目的

编写程序过程中,软件工程师们面临着众多挑战,其中,低耦合性(功能和功能之间低耦合)、高内聚性(模块内部是十分紧密的)、可扩展性以及可维护性、灵活性(增加新功能十分方便)、重用性(相同的功能,代码无需重复编写),可靠性(增加新的功能后,原来的功能不受影响)都是必须要考虑的因素,所以设计模式是为了让程序更可靠,更灵活,更具有面向对象的精髓

设计模式七大原则

单一职责原则
对类来说,即一个类只负责一个职责,比如我们之前写过的studentMapper他就知识负责学生的增删改查,这就是一个单一职责,如果说studentMapper还要去负责老师的信息管理,这就违背的单一职责原则,这个时候我们需要把studentMapper分成A类和B类,A去管理学生,B去管理老师。

package com.rlw.principle.singleresponsibility;

public class SingleResponsibility {
    public static void main(String[] args) {
        Tiger tiger = new Tiger();
        tiger.eat("母老虎");
        tiger.eat("菜包子");
        tiger.eat("菜团子");
        tiger.eat("菜卷子");
    }
}
class Tiger {
    public void eat(String tiger) {
        System.out.println(tiger + "在吃人!!!");
    }
}

此时已经违反了单一职责原则,母老虎可以吃人没问题,但是菜包子什么的就违反了单一职责原则。
在这里插入图片描述
改良2::单一职责原则模式:但是软件花销比较大,即不仅需要修改很多个类,还要修改主方法(客户端)

package com.rlw.principle.singleresponsibility;
public class SingleResponsibility {
    public static void main(String[] args) {

        Tiger tiger = new Tiger();
        tiger.eat("母老虎");
        CaiBaoZi caiBaoZi = new CaiBaoZi();
        caiBaoZi.eat("菜包子");
        CaiTuanZi caiTuanZi = new CaiTuanZi();
        caiTuanZi.eat("菜团子");
        CaiJuanZi caiJuanZi = new CaiJuanZi();
        caiJuanZi.eat("菜卷子");
    }
}
class Tiger {
    public void eat(String tiger) {
        System.out.println(tiger + "在吃人!!!");
    }
}
class CaiBaoZi{
    public void eat(String cai){
        System.out.println(cai + "在吃火锅!!!");
    }
}
class CaiTuanZi{
    public void eat(String cai){
        System.out.println(cai + "菜团子在喝可乐!!!");
    }
}
class CaiJuanZi{
    public void eat(String cai){
        System.out.println(cai + "菜卷子在想吃鸡架!!");
    }
}

在这里插入图片描述
改良3::这类虽然没有在类的级别上实现单一职责原则,但是在方法级别上仍然遵守单一职责原则

package com.rlw.principle.singleresponsibility;

public class SingleResponsibility {
    public static void main(String[] args) {

        Tiger tiger = new Tiger();
        tiger.tigerEat("母老虎");
        tiger.caiBaoZiEat("菜包子");
        tiger.caiTuanZiEat("菜团子");
        tiger.caiJuanZiEat("菜卷子");

    }
}
class Tiger {
    public void tigerEat(String tiger) {
        System.out.println(tiger + "在吃人!!!");
    }
    public void caiBaoZiEat(String tiger) {
        System.out.println(tiger + "在吃火锅!!!");
    }
    public void caiTuanZiEat(String tiger) {
        System.out.println(tiger + "在喝可乐!!!");
    }
    public void caiJuanZiEat(String tiger) {
        System.out.println(tiger + "在想吃鸡架!!!");
    }
}
//补充一点,改良3版本如果方法及其多,那我们还是要使用改良2的办法分多个类进行拆分

总结:单一职责原则各行其职!!!!
接口隔离原则
一个类不应该依赖它不需要的接口,即一个类对另一个类的依赖应该建立在最小接口上
在这里插入图片描述

package com.rlw.principle.segregation;

public class Segregation1 {
    public static void main(String[] args) {

    }
}
interface Interface1{
    void operation1();
    void operation2();
    void operation3();
    void operation4();
    void operation5();
}
class B implements Interface1{
    @Override
    public void operation1() {
        System.out.println("B 实现了 operation1");
    }

    @Override
    public void operation2() {
        System.out.println("B 实现了 operation2");
    }

    @Override
    public void operation3() {
        System.out.println("B 实现了 operation3");
    }

    @Override
    public void operation4() {
        System.out.println("B 实现了 operation4");
    }

    @Override
    public void operation5() {
        System.out.println("B 实现了 operation5");
    }
}
class D implements Interface1{
    @Override
    public void operation1() {
        System.out.println("D 实现了 operation1");
    }

    @Override
    public void operation2() {
        System.out.println("D 实现了 operation2");
    }

    @Override
    public void operation3() {
        System.out.println("D 实现了 operation3");
    }

    @Override
    public void operation4() {
        System.out.println("D 实现了 operation4");
    }

    @Override
    public void operation5() {
        System.out.println("D 实现了 operation5");
    }
}
class A{//A类通过接口Interface1依赖B类,但是只会用到1,2,3方法
    public void depand1(Interface1 i){
        i.operation1();
    }
    public void depand2(Interface1 i){
        i.operation2();
    }
    public void depand3(Interface1 i){
        i.operation3();
    }
}
class C{//C类通过接口Interface1依赖B类,但是只会用到1,2,3方法
    public void depand1(Interface1 i){
        i.operation1();
    }
    public void depand4(Interface1 i){
        i.operation4();
    }
    public void depand5(Interface1 i){
        i.operation5();
    }
}

一个类对另一个类的依赖应该建立在最小接口
以上代码类A通过接口Interface1依赖B,类C通过接口Interface1依赖D,如果接口Interface1对于类A和类C来说不是最小接口,那么类B和类D必须去实现他们不需要的方法。
按照隔离原则应当这样处理:将接口Interface1拆分为独立的几个接口,类A和类C分别与他们需要的接口建立依赖关系。也就是采用接口隔离原则
接口Interface1中出现的方法,根据实际情况拆分为三个接口
在这里插入图片描述

package com.rlw.principle.segregation;

public class Segregation1 {
    public static void main(String[] args) {
        A a = new A();
        a.depand1(new B()); //A类通过接口去依赖B
        a.depand2(new B());
        a.depand3(new B());
        C c = new C();
        c.depand1(new D());//C类通过接口去依赖D
        c.depand4(new D());
        c.depand5(new D());

    }
}
interface Interface1{
    void operation1();
}
interface Interface2{
    void operation2();
    void operation3();
}
interface Interface3{
    void operation4();
    void operation5();
}
class B implements Interface1,Interface2{
    @Override
    public void operation1() {
        System.out.println("B 实现了 operation1");
    }

    @Override
    public void operation2() {
        System.out.println("B 实现了 operation2");
    }

    @Override
    public void operation3() {
        System.out.println("B 实现了 operation3");
    }

}
class D implements Interface1,Interface3{
    @Override
    public void operation1() {
        System.out.println("D 实现了 operation1");
    }
    @Override
    public void operation4() {
        System.out.println("D 实现了 operation4");
    }
    @Override
    public void operation5() {
        System.out.println("D 实现了 operation5");
    }
}
class A{//A类通过接口Interface1依赖B类,但是只会用到1,2,3方法
    public void depand1(Interface1 i){
        i.operation1();
    }
    public void depand2(Interface2 i){
        i.operation2();
    }
    public void depand3(Interface2 i){
        i.operation3();
    }
}
class C{//C类通过接口Interface1依赖B类,但是只会用到1,2,3方法
    public void depand1(Interface1 i){
        i.operation1();
    }
    public void depand4(Interface3 i){
        i.operation4();
    }
    public void depand5(Interface3 i){
        i.operation5();
    }
}

在这里插入图片描述

标签:设计模式,java,void,System,tiger,println,图解,public,out
来源: https://blog.csdn.net/qq_44805043/article/details/116948997

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有