设计模式(接口和抽象类)

 (1) 适配器

Code:
  1. interface Window{  
  2.     public void open() ;    // 打开窗口  
  3.     public void close() ;   // 关闭窗口  
  4.     public void icon() ;    // 最小化  
  5.     public void unicon() ;  // 最大化  
  6. }  
  7. abstract class WindowAdapter implements Window{  
  8.     public void open(){}  
  9.     public void close(){}  
  10.     public void icon(){}  
  11.     public void unicon(){}  
  12. };  
  13. class MyWindow extends WindowAdapter{  
  14.     public void open(){  
  15.         System.out.println("打开窗口!") ;  
  16.     }  
  17. };  
  18. public class AdpaterDemo{  
  19.     public static void main(String args[]){  
  20.         Window win = new MyWindow() ;  
  21.         win.open() ;  
  22.     }  
  23. }  

 

 

(2)  工厂设计模式

Code:
  1. interface Fruit{  
  2.     public void eat() ;  
  3. }  
  4. class Apple implements Fruit{  
  5.     public void eat(){  
  6.         System.out.println("吃苹果。。。") ;  
  7.     }  
  8. };  
  9. class Orange implements Fruit{  
  10.     public void eat(){  
  11.         System.out.println("吃橘子。。。") ;  
  12.     }  
  13. };  
  14. class Factory{  // 工厂类  
  15.     public static Fruit getFruit(String className){  
  16.         Fruit f = null ;  
  17.         if("apple".equals(className)){  
  18.             f = new Apple() ;  
  19.         }  
  20.         if("orange".equals(className)){  
  21.             f = new Orange() ;  
  22.         }  
  23.         return f ;  
  24.     }  
  25. };  
  26. public class InterDemo{  
  27.     public static void main(String args[]){  
  28.         Fruit f = Factory.getFruit(args[0]) ;  
  29.         if(f!=null){  
  30.             f.eat() ;  
  31.         }  
  32.     }  
  33. }  

 

 

 

(3)  代理设计模式

Code:
  1. interface Give{  
  2.     public void giveMoney() ;  
  3. }  
  4. class RealGive implements Give{  
  5.     public void giveMoney(){  
  6.         System.out.println("把钱还给我。。。。。") ;  
  7.     }  
  8. };  
  9. class ProxyGive implements Give{    // 代理公司  
  10.     private Give give = null ;  
  11.     public ProxyGive(Give give){  
  12.         this.give = give ;  
  13.     }  
  14.     public void before(){  
  15.         System.out.println("准备:小刀、绳索、钢筋、钢据、手枪、毒品") ;  
  16.     }  
  17.     public void giveMoney(){  
  18.         this.before() ;  
  19.         this.give.giveMoney() ; // 代表真正的讨债者完成讨债的操作  
  20.         this.after() ;  
  21.     }  
  22.     public void after(){  
  23.         System.out.println("销毁所有罪证") ;  
  24.     }  
  25. };  
  26. public class ProxyDemo{  
  27.     public static void main(String args[]){  
  28.         Give give = new ProxyGive(new RealGive()) ;  
  29.         give.giveMoney() ;  
  30.     }  
  31. };  

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值