七大设计原则

单一职责原则

介绍:一个类只负责一项职责。如果一个类有两个功能,如果只改变一个功能时,另一个功能有可能会出现错误,这时我们需要将这个类分成两个类,每个类都具有一个功能。

方案一:在Vehicle(交通工具类)中的run方法中,违反了单一职责原则,因为飞机不能再公路上跑,我们需要根据交通工作的运行方式不同,分解成不同类。

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+"在公路上跑");
    }
}

方案二:遵守了单一职责原则,但是改动非常大,将类分解的同时,还需要修改客户端,我们可以直接修改原先的Vehicle类,改动的代码会减少

public class SingleResponsibility2 {
    public static void main(String[] args) {
        RoadVehicle roadVehicle = new RoadVehicle();
        roadVehicle.run("摩托车");
        roadVehicle.run("汽车");
        AirVehicle airVehicle = new AirVehicle();
        airVehicle.run("飞机");
    }
}

class RoadVehicle{
    public void run(String vehicle){
        System.out.println(vehicle+"在公路上跑");
    }
}

class AirVehicle{
    public void run(String vehicle){
        System.out.println(vehicle+"在天上飞");
    }
}

class WaterVehicle{
    public void run(String vehicle){
        System.out.println(vehicle+"在水中游");
    }
}

方案三:这种修改方法,没有对原来的类作大的修改,只是增加了方法,虽然没有在类级别上遵守单一职责原则,但是在方法级别上遵守了单一职责原则。

public class SingleResponsibility3 {
    public static void main(String[] args) {
        Vehicle2 vehicle2 = new Vehicle2();
        vehicle2.runLoad("汽车");
        vehicle2.runAir("飞机");
        vehicle2.runWater("游轮");
    }
}

class Vehicle2{
    public void runLoad(String vehicle){
        System.out.println(vehicle+"在公路上跑");
    }
    public void runAir(String vehicle){
        System.out.println(vehicle+"在天上飞");
    }
    public void runWater(String vehicle){
        System.out.println(vehicle+"在水中游");
    }
}

单一职责原则的注意事项:
(1)降低类的复杂度,一个类只负责一项职责
(2)提高类的可读性和可维护性
(3)降低变更引起的风险
(4)通常我们应当遵守单一职责原则,只有逻辑足够简单,才可以在代码级别违反单一职责原则;只有类中的方法数量足够少,可以在方法级别保持单一职责原则。

接口隔离原则

介绍:客户端不应该依赖它不需要的接口,即一个类对另一个类的依赖应该建立在最小的接口上。
方案一:类B和类D都实现了Interface1,类A通过Interface1会依赖使用类B,但是A中只会用到接口的1,2,3三个方法;类C通过Interface1会依赖使用类D,但是C中只会用到接口的1,4,5三个方法

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{
    public void depend1(Interface1 i){
        i.operation1();
    }
    public void depend2(Interface1 i){
        i.operation2();
    }
    public void depend3(Interface1 i){
        i.operation3();
    }
}

class C{
    public void depend1(Interface1 i){
        i.operation1();
    }
    public void depend4(Interface1 i){
        i.operation4();
    }
    public void depend5(Interface1 i){
        i.operation5();
    }
}

方案二:将Interface1拆分成几个接口,类A和类C分别与他们需要的接口建立依赖关系,也就是接口隔离原则,Interface1将被拆分成三个接口。

interface Interface1{
    void operation1();
}

interface Interface2{
    void operation2();
    void operation3();
}

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{
    public void depend1(Interface1 i){
        i.operation1();
    }
    public void depend2(Interface2 i){
        i.operation2();
    }
    public void depend3(Interface2 i){
        i.operation3();
    }
}

class C{
    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.依赖倒转原则的设计理念:相对于细节的多变性,抽象的东西要稳定的多。以抽象为基础搭建的架构比以细节为基础的架构要稳定的多,抽象指的是接口或抽象类,细节就是具体的实现类。
5.使用接口或抽象类的目的是制定好规范,而不涉及任何具体的操作,把展现细节的任务交给他们的实现类去完成。

方式一:完成Person接收消息的功能,此方式简单,比较容易想到,如果我们获取的消息不是邮件,而是微信、短信,则我们需要新增类,Person需要增加接收方法。我们可以引入一个抽象的接口IReceiver表示接收者,这时PersonIReceiver发生依赖。

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());
    }
}
posted @ 2021-07-09 10:20  小布丁超级帅  阅读(70)  评论(2)    收藏  举报