IOC AOP 设计模式
IOC AOP 不是什么技术而是一种设计模式 学习 IOC AOP 其实是在学习一种思想。
1.IOC
IOC其实是 将对象的创建和获取提取到外部。由外部IOC容器提供需要的组件。
看下面代码:
public class Girl
{ //外部包办直接传入boy类
public void kiss3(Boy boy)
{
Console.WriteLine("girl kiss boy");
boy.kiss();
} } public class Boy
{
public void kiss()
{
Console.WriteLine("boy kiss girl");
}
}
当我在Girl类中要使用Boy类的时候一般做法是在 Girl中实例化Boy类在进行调用如下代码:
public class Girl
{
//1. 内部实例化boy类
public void kiss1()
{
Boy boy = new Boy();
boy.kiss();
}
}
然而在IOC 设计模式中 则将实例化操作放到了 IOC容器中,在这里我们将 main 函数作为它的容器。
static void Main(string[] args)
{
Boy boy = new Boy();
Girl girl = new Girl();
girl.kiss3(boy);
}
2.AOP
AOP其实是一种-Proxy模式 (代理模式) 关注主要的东西,也可以说让你只关注业务,其他的东西就让AOP帮你完成。
看下代码 我定义了一个计算的接口
/// <summary>
/// 抽象主题角色(Subject)
/// </summary>
public interface IMath
{
// 方法
double Add(double x, double y);
double Sub(double x, double y);
double Mul(double x, double y);
double Div(double x, double y); }
/// <summary>
/// 真实主题角色(RealSubject)角色
/// </summary>
public class Math : MarshalByRefObject, IMath
{
// 方法
public double Add(double x, double y) { return x + y; }
public double Sub(double x, double y) { return x - y; }
public double Mul(double x, double y) { return x * y; }
public double Div(double x, double y) { return x / y; } }
我通过代理来实现,但是这时候我 Math 类只会做 加法Add 方法,那把其余的交给其人做。
/// <summary>
/// 远程代理对象 Remote "Proxy Object"
/// 代理主题(Proxy)角色
/// </summary>
public class MathProxy
{
IMath math; // 构造函数
public MathProxy()
{
if (math == null)
math = new Math();
} // 方法
public double Add(double x, double y)
{
return math.Add(x, y);
}
public double Sub(double x, double y)
{
return 0;
}
public double Mul(double x, double y)
{
return 0;
}
public double Div(double x, double y)
{
return 0;
} }
static void Main(string[] args)
{
// 创建代理和请求一个服务
Proxy p = new Proxy();
p.Request(); //创建函数
MathProxy mp = new MathProxy(); // 执行函数
Console.WriteLine("4 + 2 = {0}", mp.Add(, ));
Console.WriteLine("4 - 2 = {0}", mp.Sub(, ));
Console.WriteLine("4 * 2 = {0}", mp.Mul(, ));
Console.WriteLine("4 / 2 = {0}", mp.Div(, ));
Console.ReadLine();
}
这个过程就叫AOP。 我只关注一个加法运算 其余的交给其他人做。
3.IOC AOP 联合使用
只要改下代理就好。 通过外部的IOC容器来注入依赖对象
/// <summary>
/// 远程代理对象 Remote "Proxy Object"
/// 代理主题(Proxy)角色
/// </summary>
public class MathProxy
{
IMath math; // 构造函数
public MathProxy(IMath _math)
{
math = _math;
} // 方法
public double Add(double x, double y)
{
return math.Add(x, y);
}
public double Sub(double x, double y)
{
return math.Sub(x, y);
}
public double Mul(double x, double y)
{
return math.Mul(x, y);
}
public double Div(double x, double y)
{
return math.Div(x, y);
} }
IOC AOP 设计模式的更多相关文章
- Spring中三个重要概念 IOC AOP Bean
Spring中三个重要概念 IOC AOP Bean 首先讲解一下Spring框架,以及为什么要使用Spring 框架? spring 是一个很好的容器框架, 是轻量级的IoC和AOP的容器框架,主要 ...
- spring ioc aop 原理
spring ioc aop 原理 spring ioc aop 的原理 spring的IoC容器是spring的核心,spring AOP是spring框架的重要组成部分. 在传统的程序设计中,当调 ...
- 依赖注入[2]: 基于IoC的设计模式
正如我们在<控制反转>提到过的,很多人将IoC理解为一种"面向对象的设计模式",实际上IoC自身不仅与面向对象没有必然的联系,它也算不上是一种设计模式.一般来讲,设计模 ...
- 仿写一个简陋的 IOC/AOP 框架 mini-spring
讲道理,感觉自己有点菜.Spring 源码看不懂,不想强行解释,等多积累些项目经验之后再看吧,但是 Spring 中的控制反转(IOC)和面向切面编程(AOP)思想很重要,为了更好的使用 Spring ...
- Spring Bean的生命周期、Spring MVC的工作流程、IOC,AOP
1.Spring Bean的生命周期? (1)构造方法实例化bean. (2)构造方法设置对象属性. (3)是否实现aware接口,三种接口(BeanNameAware,BeanFactoryAwar ...
- spring(一)IOC & AOP
参考文档: spring详解:http://www.cnblogs.com/ysocean/p/7466191.html(可以说非常详细了) aop源码详解:https://www.cnblogs.c ...
- Spring(二)--IoC&AOP
IOC 一.IOC概述: 一般指控制反转(inversion of Control),把创建对象的权利交给框架,Ioc容器控制对象,是框架的重要特征,并非是面向对象编程的专用术语.它包括依赖注入(DI ...
- Unity 处理IOC AOP
用Unity 可以做IOC(控制反转) AOP(切面)可以做统一的异常和日志处理,非常方便,项目中是用微软企业库中的Microsoft.Practices.Unity实现 1 定义接口与实现 //定义 ...
- TinyFrame系列:基于EFCodeFirst,IOC,AOP的轻型框架
TinyFrame开篇:基于CodeFirst的ORM TinyFrame续篇:整合Spring IOC实现依赖注入 TinyFrame再续篇:整合Spring AOP实现日志拦截 TinyFrame ...
随机推荐
- 处理mysql主从中断
主从同步中断跳过处理步骤: slave stop;set GLOBAL SQL_SLAVE_SKIP_COUNTER=1;slave start; 在使用set global sql_slave_s ...
- Python--urllib3库
Urllib3是一个功能强大,条理清晰,用于HTTP客户端的Python库,许多Python的原生系统已经开始使用urllib3.Urllib3提供了很多python标准库里所没有的重要特性: 1 ...
- spring data mongodb 操作
xml配置(mongo集群方式): <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=& ...
- oracle 分析函数加order by的影响
create table test (id number(2), name varchar2(10), salary number(6,2));insert into test values (1,' ...
- 用python40行代码编写的计算器
效果图 代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 ...
- django 基于正则表达式的url
方式一: urls.py from mytest import views urlpatterns = [ url(r'^index-(\d+)-(\d+).html', views.Index.as ...
- zabbix监控MySQL部署实战
1.部署zabbix监控. 1.1 建用户组和用户 groupadd zabbix useradd -d /home/zabbix -g zabbix -m zabbix passwd zabbix ...
- Python基础学习四 文件操作(二)
####读取文件#### with open('goods_info.txt', 'r', encoding='utf-8') as f: f.seek(0) # 注意指针位置 goods_info ...
- 微信小程序中的倒计时
这是我项目中的例子,如果有更好的建议欢迎留言 ,一起学习 //获取时间 var sekillStartTime = resultLis[0].planGroup0.sekillStartTime;// ...
- cuteFTP软件往linux中上传文件时报…
我是在win7和VM中的ubuntu传输文件: 使用一个客户端,可以正常的连接,但是当上传文件时,总是报553 Could not create file错误信息. 主要原因是新建的文件夹没有更改权限 ...