抽象类和接口的区别(里面有适配器设计、工厂设计、代理设计模式)

在MyWindow 类肯定要覆写全部方法,但是现在希望可以根据自己的需要选择性的覆盖某些方法。那么该怎么实现呢?

用一个类先将接口实现了,但是所有的实现都属于空实现。然后再继承这个类。应该使用抽象类,因为抽象类也不能直接使用。

适配器设计模式:只要接口和抽象类相互使用,就叫适配器模式。

1.        interfaceWindow{

2.          public void open() ;            // 打开窗口

3.          public void close() ;            // 关闭窗口

4.          public void icon() ;             // 最小化

5.          public void unicon() ;         // 最大化

6.        }

7.        abstractclass WindowAdapter implements Window{

8.          public void open(){}

9.          public void close(){}

10.         publicvoid icon(){}

11.         publicvoid unicon(){}

12.       };

13.       class MyWindow extends WindowAdapter{

14.         publicvoid open(){

15.                       System.out.println("打开窗口!") ;

16.         }

17.       };

18.       public class AdpaterDemo{

19.         publicstatic void main(String args[]){

20.                       Windowwin = new MyWindow() ;

21.                       win.open();

22.         }

23.       }

工厂设计模式:

所有接口的实例化对象都是通过工厂类取得的。那么客户端调用的时候根据传入的名称不同完成的功能也不同。

有如下一段代码

interface Fruit{

              public void eat() ;

 

}

class Apple implementsFruit{

              public void eat(){

                            System.out.println("吃苹果") ;

              }

} ;

class Orange implements Fruit{

              public void eat(){

                            System.out.println("吃橘子") ;

              }

} ;

class Foctory{ //工厂类

              public static FruitgetFruit(String className){

                            Fruit f  = null ;

                            if("apple".equals(className)){

                                          f =new Apple() ;

                            }

                            if("orange".equals(className)){

                                          f =new Orange() ;

                            }

                            return f ;

              }

} ;

public classInterDemo{

              public static void main(Stringargs[]){

                            Fruit f =Foctory.getFruit(args[0]) ;

                            if(f!= null ){

                                          f.eat();

                            }

              }

}

 

if("apple".equals(className)){

                                          f =new Apple() ;

                            }

这段代码是把apple写在前面,在以后的开发中,如果有字符串跟常量(这里是apple)比较,要永远把常量写在字符串的前面去。这样可以避免空指向异常。

代理设计模式:

刘某欠张某的钱,刘某有钱不还,所以张某找到讨债公司。讨债公司找刘某要钱。

                                                          要钱

张某---------------------》找到讨债公司-------------------》刘某

interface Give{

              public void giveMoney() ;

}

class RealGiveimplements Give{

              public void giveMoney(){

                            System.out.println("把钱还给我")  ;

              }

} ;

class ProxyGiveimplements Give{

              private Give give = null ;

              public ProxyGive(Give give){

                            this.give = give ;

              }

              public void before(){

                            System.out.println("刀子、枪支等。。。。。。。") ;

              }

              public void giveMoney(){

                            this.before() ;

                            this.give.giveMoney();

                            this.after() ;

              }

              public void after() {

                            System.out.println("销毁所有证据。。。。。。。") ;

              }

} ;

public classProxyDemo{

              public static void main(Stringargs[]){

                            Give give = newProxyGive(new RealGive()) ;

                            give.giveMoney() ;

              }

}

最核心的就是这个代理类,它是真实的讨债人,做了真正的工作。这就是代理设计模式。

 

抽象类和接口(绝对重点

2

比较点

抽象类

接口

1

组成

抽象方法、、普通方法、构造方法、常量、变量、全局常量

抽象方法、全局常量

2

定义

Abstract

Interface

3

子类

通过extends继承抽象类

通过implements实现接口

4

限制

一个子类只能继承一个抽象类

一个子类可以实现多个接口

5

 

 

关系

一个抽象类可以实现多个接口,

但是一个接口不能实现继承一个抽象类。只能实现多个接口。

一个抽象类中可以包含多个接口

一个接口中可以包含多个抽象类

6

设计

模板设计

工厂设计、代理设计

两个一起操作可以完成适配器设计模式。

7

实例化

都是通过对象的多态性,通过子类进行对象的实例化操作

8

实现限制

存在单继承局限

不存在此类局限

9

特性

 

表示一个标准,一种能力

从以上表格可以看出,如果抽象类和接口同时都可以使用的话优先使用接口,因为接口可以避免单继承的局限。

 

 

 

 


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值