当前位置: 移动技术网 > IT编程>软件设计>设计模式 > Java面向接口编程,低耦合高内聚的设计哲学

Java面向接口编程,低耦合高内聚的设计哲学

2019年01月04日  | 移动技术网IT编程  | 我要评论

接口体现的是一种规范和实现分离的设计哲学,充分利用接口可以极大的降低程序中各个模块之间的耦合,提高系统的可维护性以及可扩展性。

因此,很多的软件架构设计理念都倡导“面向接口编程”而不是面向实现类编程,以期通过这种方式来降低程序的耦合。

但是在讨论这些之前,我们先要搞清楚一个问题:

接口还是抽象类?

为什么会有这个问题,因为在某些情况下,接口和抽象类很像:

  • 接口和抽象类都不能被实例化,它们都位于继承树的顶端,用于被其它类实现或者继承。
  • 接口和抽象类都可以包含抽象方法,实现接口或者继承抽象类的普通子类都必须实现这些抽象方法。

接口作为系统和外界交互的窗口,体现的是一种规范。规定了实现者必须向外提供哪些服务,调用者可以调用哪些服务。当在一个程序中使用接口时,接口是多个模块之间的耦合标准;当在多个应用程序之间使用接口时,接口是多个程序之间的通信标准。

而抽象类则不同,它作为多个子类的共同父类,体现的是一种模板式设计。它可以作为系统实现的中间产品,但是必须要进行完善才能成为最终产品。

除此之外,接口和抽象类在用法上也存在差别:

  • 接口只能包含抽象方法和默认方法,不能为普通方法提供方法实现;抽象类则可以包含普通方法。
  • 接口中不能定义静态方法;抽象类可以。
  • 接口不能定义普通成员变量;抽象类可以。
  • 接口不包含构造器;抽象类可以包含构造器,但并不是用于创建对象,而是用于让其他子类调用这些构造器来完成属于抽象类的初始化操作。
  • 一个类只能有一个直接父类,包括抽象类;但一个类可以直接实现多个接口,通过实现多个接口可以弥补单继承的不足。

下面介绍两种常见的应用场景来示范面向接口编程的优势。

简单的工厂模式

有这样一个场景:程序中的每一个computer类都需要集成一个printer类,以完成设备的输入输出,示例如下:

public class computer{
     public static void main(string []args){
        printer p = new printer();
        p.getmsg("amos");
        p.getmsg("h's blog");
        p.printmsg();
     }
}

class printer{
    private string msg = "";
    public void getmsg(string message){
        msg += message;
    }
    public void printmsg(){
        system.out.println(msg);
    }
}

但是,当我们在重构代码的时候,需要使用betterprinter类以替换printer类时,如果只有一个类集成了printer那么还好,但是如果有成百上千个类集成了这个类呢?那么工作量将会是巨大的!

为了解决这个问题,我们可以使用接口实现工厂模式,让computer类集成一个printerfactory类,将computer类和printer类的实现完全分隔开来。当从printer类切换到betterprinter类时对系统完全没有影响。

示例如下:

class computer{
    private out p;
    public computer(out printer){
        this.p = printer;
    }
    public void keyin(string msg){
        p.getmsg(msg);
    }
    public void printmsg(){
        p.printmsg();
    }
}

class printer implements out{
    private string msg = "";
    public void getmsg(string message){
        msg += message;
    }
    public void printmsg(){
        system.out.println(msg);
    }
}

interface out{
    string mes = "";
    void getmsg(string message);
    void printmsg();
}

public class printerfactory{
    public out out(){
        return new printer();
    }
    public static void main(string[] args){
        printerfactory pf = new printerfactory();
        computer c = new computer(pf.out());
        c.keyin("amos");
        c.keyin("h's blog");
        c.printmsg();
        //output amosh's blog
    }
}

该实现使用工厂类用来生产printer对象实例,而computer类则只接收工厂类返回的对象用以初始化,分隔了和printer对象实现之间的耦合。当系统需要使用betterprinter类替换printer类时,只要betterprinter类实现了out接口,然后在printerfactory工厂类中修改产生的对象即可。

下面是替换示例:

class computer{
    private out p;
    public computer(out printer){
        this.p = printer;
    }
    public void keyin(string msg){
        p.getmsg(msg);
    }
    public void printmsg(){
        p.printmsg();
    }
}

class betterprinter implements out{
    //全新的打印类,限制只能存储5次输入
    private string msg = "";
    private int count = 5;
    public void getmsg(string message){
        msg += message;
        count--;
        if(count==0){
            printmsg();
            count = 5;
        }
    }
    public void printmsg(){
        system.out.println(msg);
        msg="";
        count = 5;
    }
}

interface out{
    string mes = "";
    void getmsg(string message);
    void printmsg();
}

public class printerfactory{
    public out out(){
        return new betterprinter();
    }
    public static void main(string[] args){
        printerfactory pf = new printerfactory();
        computer c = new computer(pf.out());
        c.keyin("amos");
        c.keyin("h's ");
        c.keyin("blog ");
        c.keyin("wel");
        c.keyin("come");
        //output amosh's blog welcome
        c.keyin("amos");
        c.keyin("h's ");
        c.keyin("blog ");
        c.keyin("wel");
        c.keyin("come");
        //output amosh's blog welcome
        c.keyin("end this");
        c.printmsg();
        //output end this
    }
}

命令模式

考虑这样一个问题:在某些情形下,某个方法需要完成某一个特殊的行为但这个实现无法确定,必须在调用该方法时指定具体的处理行为。

这就意味着,我们必须把“处理方法”作为参数传入该方法,这个“处理行为”用编程实现来说就是一段代码,那么如何把代码传入该方法呢?

我们可以使用lambda表达式作为处理方法传入,但是当涉及一些比较复杂的处理行为时,这往往是不够的。

我们可以考虑使用command接口定义一个方法,用这个方法来封装“处理行为”。以下以遍历一个数组作为示范:

public class test{
    public static void main(string[] args){
        int[] target = {1,-1,10,-20};
        process pc = new process();
        pc.process(target,new printcmd());
        //output 当前元素为正数1
        //output 当前元素为负数-1
        //output 当前元素为正数10
        //output 当前元素为负数-20
        system.out.println("=======分割线========");
        pc.process(target,new addcmd());
        //output 数组的和为-10
    }
}

interface command{
    void process(int[] target);
}

class process{
    public void process(int[] target,command cmd){
        cmd.process(target);
    }
}

class printcmd implements command{
    public void process(int[] target){
        for(int i:target){
            if(i<0){
                system.out.println("当前元素为负数"+i);
            }else{
                system.out.println("当前元素为正数"+i);
            }
        }
    }
}

class addcmd implements command{
    private int count;
    public void process(int[] target){
        for(int i:target){
            count += i;
        }
        system.out.println("数组的和为"+count);
    }
}

对于printcmd和addcmd而言,真正有用的就是process方法,该方法体传入process的实例中用以处理数组。实现了process()方法和“处理方法”的相分离。

java 填坑手册,欢迎fork我的github仓库

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网