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 ...
随机推荐
- EditText动态转换只读/编辑状态
public class MyActivity extends Activity { private KeyListener listener; private EditText editText; ...
- An invalid form control with name='timeone[]' is not focusable.
在项目开发的时候 遇到了这样的报错 An invalid form control with name='timeone[]' is not focusable. 学习源头:https://segme ...
- FPGA应用及ARM-FPGA架构举例
FPGA的应用非常广泛,通信领域,视频图像处理领域,汽车电子领域,消费电子领域,工业领域,数据处理领域等,都能看到FPGA的身影. 在设计中,FPGA通常和其他处理IC架构,完成整个设计.FPGA-A ...
- 【转】在Linux下使用Jmeter执行测试任务
想在Linux下运行jmeter必须先安装jdk,安装步骤如下: (1).下载一个linux可用的jdk包(比如:jdk-6u45-linux-i586.bin), (2).然后将jdk-6u45-l ...
- appium 中swipe()方法向左滑动时
应该在UI Automator Viewer中读取到的例如ImageView [180,600][900,1320],如果要左滑,代码中应该是写为driver.swipe(900,1320,180,6 ...
- oracle查询题目2道
1.列出与“SCOTT”从事相同工作的所有员工. ①先查询SCOTT从事的是什么工作 select job from emp where name like='SCOTT'; ②select enam ...
- win10下默认使用福昕打开PDF
win10为了推他的edge浏览器, 将默认的pdf打开设置为了edge浏览器, 非常令人反感, 做浏览器就好好做浏览器, 为什么要默认打开pdf? 而且修改默认为福昕后, 下次打开pdf文件, 他又 ...
- JavaScript笔记——基础知识(一)
<Script>标签属性 <script>xxx</script>这组标签,是用于在 html 页面中插入 js 的主要方法.它主要有以下 几个属性: charse ...
- 判定元素是否刚插入到DOM树
上接<这篇博文>,其应用于avalon的if绑定.如果一个节点还没有插入DOM树,那么avalon将延时对它进行扫描渲染,直到它再次插入到DOM树为止.由于CSS3 keyframe动画的 ...
- springMVC的多文件的异步上传实现
springMVC的MultipartFile与传统的ajax文件上传兼容性不好,采用如下的ajax方法,后台无法获取文件. $.ajax({ url: '/upload', type: 'POST' ...