资讯详情

Java设计模式(一)——七大原则

  1. 代码重用性 (即同一功能的代码不需要多次编写)

  2. 可读性 (即编程规范, 方便其他程序员的阅读和理解)

  3. 可扩展性 (即需要添加新功能时,非常方便,称为可维护)

  4. 可靠性 (即增加新功能后,对原功能无影响)

  5. 使程序具有高内聚性功能相似的内容尽可能在同一模块中)和低耦合(类与模块之间的依赖)的特性

如下:

  1. 单一职责原则

  2. 接口隔离原则

  3. 依靠倒转(倒置)原则

  4. 替氏替换原则

  5. 开闭原则

  6. 迪米特法则

  7. 合成复用原则

单一职责原则

对类来说,即一类应该只负责一项责任。 A 负责两个不同职责:职责 1,职责 2。当职责 1 改变需求 A 可能会造成责任 2 因此,有必要执行类别 A 粒度分解为 A1,A2。

单一职责原则注意事项及细节:

  1. 降低类的复杂性,一类只负责一项责任。

  2. 提高可读性和可维护性

  3. 降低变更造成的风险

  4. 一般来说,我们应该遵守单一责任原则。只有逻辑足够简单,我们才能在代码级违反单一责任原则;只有当类中的方法数量足够少时,才能在方法级别保持单一责任原则

接口隔离原则

客户端不应该依赖其不需要的接口,即一个类对另一个类的依赖应该基于

例子:

在这里插入图片描述

类 A 通过接口 Interface1 依赖类 B,类 C 通过接口 Interface1 依赖类 D,如果接口 Interface1 对于类 A 和类 C不是最小接口,所以类别 B 和类 D 必须实现他们不需要的方法。

代码实现:

public class Segregation1 { 
              public static void main(String[] args) { 
                 // TODO Auto-generated method stub     }  }  //接口 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");
    }
}

class 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 依赖(使用) D 类,但是只会用到 1,4,5 方法
    public void depend1(Interface1 i) { 
        
        i.operation1();
    }
    public void depend4(Interface1 i) { 
        
        i.operation4();
    }
    public void depend5(Interface1 i) { 
        
        i.operation5();
    }
}

按隔离原则应当这样处理:

  1. 类 A 通过接口 Interface1 依赖类 B,类 C 通过接口 Interface1 依赖类 D,如果接口 Interface1 对于类 A 和类 C来说不是最小接口,那么类 B 和类 D 必须去实现他们不需要的方法

  2. 将接口 拆分为独立的几个接口,类 A 和类 C 分别与他们需要的接口建立依赖关系。也就是采用接口隔离原则

  3. 接口 Interface1 中出现的方法,根据实际情况拆分为三个接口

代码修改如下:

public class Segregation1 { 
        

    public static void main(String[] args) { 
        
        // TODO Auto-generated method stub
        // 使用一把
        A a = new A();
        a.depend1(new B()); // A 类通过接口去依赖 B 类
        a.depend2(new B());
        a.depend3(new B());

        C c = new C();

        c.depend1(new D()); // C 类通过接口去依赖(使用)D 类
        c.depend4(new D());
        c.depend5(new D());
    }
    
}

// 接 口 1
interface Interface1 { 
         
    void operation1();
}

// 接 口 2
interface Interface2 { 
        
    void operation2();
    void operation3();
}

// 接 口 3
interface Interface3 { 
         
    void operation4();
    void operation5();
}


class B implements Interface1, Interface2 { 
        
    
    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 D implements Interface1, Interface3 { 
         
    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 A { 
         // A 类通过接口 Interface1,Interface2 依赖(使用) B 类,但是只会用到 1,2,3 方法
    public void depend1(Interface1 i) { 
        
        i.operation1();
    }
    public void depend2(Interface2 i) { 
         
        i.operation2();
    }
    public void depend3(Interface2 i) { 
         
        i.operation3();
    }
}

class C { 
         // C 类通过接口 Interface1,Interface3 依赖(使用) D 类,但是只会用到 1,4,5 方法
    public void depend1(Interface1 i) { 
         
        i.operation1();
    }
    public void depend4(Interface3 i) { 
         
        i.operation4();
    }
    public void depend5(Interface3 i) { 
        
        i.operation5();
    }
}

依赖倒转(倒置)原则

  1. 高层模块不应该依赖低层模块,二者都应该依赖其抽象

  2. 抽象不应该依赖细节,细节应该依赖抽象

  3. 依赖倒转(倒置)的中心思想是

  4. 依赖倒转原则是基于这样的设计理念:相对于细节的多变性,抽象的东西要稳定的多。以抽象为基础搭建的架构比以细节为基础的架构要稳定的多。在 java 中,抽象指的是接口或抽象类,细节就是具体的实现类

  5. ,参数为接口,实际传入对应的实现类。

例子:

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";
    }
}

//完成 Person 接收消息的功能
//方式 1 分析
//1. 简单,比较容易想到
//2. 如果我们获取的对象是 微信,短信等等,则新增类,同时 Perons 也要增加相应的接收方法
//3. 解决思路:引入一个抽象的接口 IReceiver, 表示接收者, 这样 Person 类与接口 IReceiver 发生依赖
// 因为 Email, WeiXin 等等属于接收的范围,他们各自实现 IReceiver 接口就 ok, 这样我们就符号依赖倒转原则
class Person { 
        
    public void receive(Email email) { 
        
        System.out.println(email.getInfo());
    }
}

依赖倒转修改,通过子类或实现类实现具体的方法,面向接口编程:

public class DependecyInversion { 
        

    public static void main(String[] args) { 
        
        //客户端无需改变
        Person person = new Person();
        person.receive(new Email());
        person.receive(new WeiXin());
    }

}

//定义接口
interface IReceiver { 
        
    public String getInfo();
}

class Email implements IReceiver { 
        
    public String getInfo() { 
        
        return "电子邮件信息: hello,world";
    }
}


//增加微信
class WeiXin implements IReceiver { 
        
    public String getInfo() { 
        
        return "微信信息: hello,ok";
    }
}

//方式 2
class Person { 
        
    //这里我们是对接口的依赖
    public void receive(IReceiver receiver) { 
        
        System.out.println(receiver.getInfo());
    }
}

依赖关系传递的三种方式和应用案例:

  1. 接口传递

  2. 构造方法传递应用案例代码

  3. setter 方式传递

public class DependencyPass { 
        

    public static void main(String[] args) { 
        
        // TODO Auto-generated method stub 
        ChangHong changHong = new ChangHong();
        // OpenAndClose openAndClose = new OpenAndClose();
        // openAndClose.open(changHong);

        //通过构造器进行依赖传递
        // OpenAndClose openAndClose = new OpenAndClose(changHong);
        // openAndClose.open();
        
        //通过 setter 方法进行依赖传递
        OpenAndClose openAndClose = new OpenAndClose();
        openAndClose.setTv(changHong);
        openAndClose.open();

    }

}

// 方式 1: 通过接口传递实现依赖
// 开关的接口


// interface IOpenAndClose { 
        
// public void open(ITV tv); //抽象方法,接收接口
// }
//
// interface ITV { //ITV 接口
// public void play();
// }
//
// class ChangHong implements ITV { 
        
//
// @Override
// public void play() { 
        
// // TODO Auto-generated method stub
// System.out.println("长虹电视机,打开");
// }
//
// }
 实现接口
// class OpenAndClose implements IOpenAndClose{ 
        
// public void open(ITV tv){ 
        
// tv.play();
// }
// }

// 方式 2: 通过构造方法依赖传递
// interface IOpenAndClose { 
        


// public void open(); //抽象方法
// }
// interface ITV { //ITV 接口
// public void play();
// }
// class OpenAndClose implements IOpenAndClose{ 
        
// public ITV tv; //成员
// public OpenAndClose(ITV tv){ //构造器
// this.tv = tv;
// }
// public void open(){ 
        
// this.tv.play();
// }
// }


// 方式 3 , 通过 setter 方法传递
interface IOpenAndClose { 
        
    public void open(); // 抽象方法
    public void setTv(ITV tv);
}

interface ITV { 
         // ITV 接口
    public void play();
}


class OpenAndClose implements IOpenAndClose { 
        
    private ITV tv;

    public void setTv(ITV tv) { 
        
        this.tv = tv;
    }

    public void open() { 
        
        this.tv.play();
    }
}


class ChangHong implements ITV { 
        
    
    @Override
    public void play() { 
        
        // TODO Auto-generated method stub
        System.out.println("长虹电视机,打开");
    }


}
  1. 低层模块尽量都要有抽象类或接口,或者两者都有,程序稳定性更好.

  2. 变量的声明类型尽量是抽象类或接口, 这样我们的变量引用和实际对象间,就存在一个缓冲层,利于程序扩展和优化

  3. 继承时遵循里氏替换原则

里氏替换原则

  1. 继承包含这样一层含义:,实际上是在设定规范和契约,虽然它不强制要求所有的子类必须遵循这些契约,但是如果子类对这些已经实现的方法任意修改,就会对整个继承体系造成破坏。

  2. 继承在给程序设计带来便利的同时,也带来了弊端。比如使用继承会给程序带来侵入性,程序的可移植性降低, 增加对象间的耦合性,

  3. 问题提出:在编程中,如何正确的使用继承? => 里氏替换原则

通用的做法是:原来的父类和子类都继承一个更通俗的基类,原有的继承关系去掉,采用依赖,聚合,组合等关系代替重写。

public class Liskov { 
        

    public static void main(String[] args) { 
        
        // TODO Auto-generated method stub A a = new A();
        System.out.println("11-3=" + a.func1(11, 3));
        System.out.println("1-8=" + a.func1(1, 8));

        System.out.println("-----------------------");
        B b = new B();
        //因为 B 类不再继承 A 类,因此调用者,不会再 func1 是求减法
        //调用完成的功能就会很明确
        System.out.println("11+3=" + b.func1(11, 3));//这里本意是求出 11+3

        System.out.println("1+8=" + b.func1(1, 8));// 1+8 System.out.println("11+3+9=" + b.func2(11, 3));
        
//使用组合仍然可以使用到 A 类相关方法
        System.out.println("11-3=" + b.func3(11, 3));// 这里本意是求出 11-3
    }


}

//创建一个更加基础的基类 
class Base { 
        
//把更加基础的方法和成员写到 Base 类
}

// A 类
class A extends Base { 
        
    // 返回两个数的差
    public int func1(int num1, int num2) { 
        
        return num1 - num2;
    }
}

// B 类继承了 A
// 增加了一个新功能:完成两个数相加,然后和 9 求和
class B extends Base { 
        
    //如果 B 需要使用 A 类的方法,使用组合关系
    private A a = new A();

    //这里,重写了 A 类的方法, 可能是无意识
    public int func1(int a, int b) { 
        
        return a + b;
    }

    public int func2(int a, int b) { 
        
        return func1(a, b) + 9;
    }

    //我们仍然想使用 A 的方法
    public int func3(int a, int b) { 
        
        return this.a.func1(a, b);
    }
}

开闭原则

  1. 开闭原则(Open Closed Principle)是编程中最基础、最重要的设计原则

  2. 一个软件实体如类,模块和函数应该对扩展开放**()()**。用抽象构建框架,用实现扩展细节。

  3. 编程中遵循其它原则,以及使用设计模式的目的就是遵循开闭原则。

简单例子是使用抽象类,子类继承重写对应的方法,然后父类引用指向子类实例,类似依赖倒置的接口多实现类。

例子:

public class Ocp { 
        

    public static void main(String[] args) { 
        
        //使用看看存在的问题
        GraphicEditor graphicEditor = new GraphicEditor();
        graphicEditor.drawShape(new Rectangle());
        graphicEditor 

标签: itv液压螺旋连接器

锐单商城拥有海量元器件数据手册IC替代型号,打造 电子元器件IC百科大全!

 锐单商城 - 一站式电子元器件采购平台  

 深圳锐单电子有限公司