初学spring 关于IOC与AOP 使用的设计模式

IOC(Inverse of Control) 控制反转


以前程序中需要使用对象时,需要自己new一个,造成程序与对象的强耦合。
IOC 对象的生成交给spring容器完成


DI(Dependency Injection),依赖注入


IOC与DI一个意思,等价

 

AOP(Aspect-oriented Programming)面向切面编程

----------------------------------------------------------------------------------------------------------------------------------------------------------

Spring   IOC使用了工厂模式

 

抽象产品:

Product.Java

  1. <span style="font-size:18px">package com.test.simplefactory;  
  2.   
  3. public abstract class Product  
  4. {  
  5.       
  6. }  
  7. </span>  


具体产品:

ConcreteProductA.java

  1. <span style="font-size:18px">package com.test.simplefactory;  
  2.   
  3. public class ConcreteProductA extends Product  
  4. {  
  5.   
  6. }  
  7. </span>  

 

ConcreteProductB.java

  1. <span style="font-size:18px">package com.test.simplefactory;  
  2.   
  3. public class ConcreteProductB extends Product  
  4. {  
  5.   
  6. }  
  7. </span>  


工厂类

Creator.java

  1. <span style="font-size:18px">package com.test.simplefactory;  
  2.   
  3. public class Creator  
  4. {  
  5.     public static Product createProduct(String str)  
  6.     {  
  7.         if("A".equals(str))  
  8.         {  
  9.             return new ConcreteProductA();  
  10.         }  
  11.         else if("B".equals(str))  
  12.         {  
  13.             return new ConcreteProductB();  
  14.         }  
  15.         else  
  16.         {  
  17.             return null;  
  18.         }  
  19.     }  
  20. }  
  21. </span>  


具体应用

Client.java

  1. <span style="font-size:18px">package com.test.simplefactory;  
  2.   
  3. public class Client  
  4. {  
  5.     public static void main(String[] args)  
  6.     {  
  7.         Product productA = Creator.createProduct("A");  
  8.           
  9.         System.out.println(productA.getClass().getName());  
  10.           
  11.         Product productB = Creator.createProduct("B");  
  12.           
  13.         System.out.println(productB.getClass().getName());  
  14.     }  
  15. }  
  16. </span>  


 

 

----------------------------------------------------------------------

Spring   AOP使用了代理模式

 

 静态代理

 抽象角色

Subject.java

  1. <span style="font-size:18px">package com.test.proxy;  
  2.   
  3. // 抽象角色  
  4.   
  5. public abstract class Subject  
  6. {  
  7.     abstract public void request();  
  8. }  
  9. </span>  


 

 

代理角色    中介

ProxySubject.java

  1. <span style="font-size:18px">package com.test.proxy;  
  2.   
  3. //代理角色  
  4.   
  5. public class ProxySubject extends Subject  
  6. {  
  7.     private RealSubject realSubject; // 以真实角色作为代理角色的属性  
  8.   
  9.     public ProxySubject()  
  10.     {  
  11.     }  
  12.   
  13.     public void request() // 该方法封装了真实对象的request方法  
  14.     {  
  15.         preRequest();  
  16.   
  17.         if (realSubject == null)  
  18.         {  
  19.             realSubject = new RealSubject();  
  20.         }  
  21.   
  22.         realSubject.request(); // 此处执行真实对象的request方法  
  23.   
  24.         postRequest();  
  25.     }  
  26.   
  27.     private void preRequest()  
  28.     {  
  29.         // something you want to do before requesting  
  30.     }  
  31.   
  32.     private void postRequest()  
  33.     {  
  34.         // something you want to do after requesting  
  35.     }  
  36.   
  37. }  
  38. </span>  


 

真实角色   房东

RealSubject.java

  1. <span style="font-size:18px">package com.test.proxy;  
  2.   
  3. //真实角色:实现了Subject的request()方法  
  4.   
  5. public class RealSubject extends Subject  
  6. {  
  7.     public RealSubject()  
  8.     {  
  9.     }  
  10.   
  11.     public void request()  
  12.     {  
  13.         System.out.println("From real subject.");  
  14.     }  
  15.   
  16. }  
  17. </span>  


 

客户端调用    租客

Client.java

  1. <span style="font-size:18px">package com.test.proxy;  
  2.   
  3. //客户端调用  
  4.   
  5. public class Client  
  6. {  
  7.     public static void main(String[] args)  
  8.     {  
  9.         Subject sub = new ProxySubject();  
  10.   
  11.         sub.request();  
  12.     }  
  13. }  
  14. </span>  


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值