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 ...
随机推荐
- 蓝桥杯 算法训练 ALGO-114 黑白无常
算法训练 黑白无常 时间限制:1.0s 内存限制:256.0MB 问题描述 某寝室的同学们在学术完之后准备玩一个游戏:游戏是这样的,每个人头上都被贴了一张白色或者黑色的纸,现在每个人都会说一句 ...
- ALUA and SRM
A couple important (ALUA and SRM) notes There’s some internal dialog today on our “VMware Champions” ...
- python 类实例化,修改属性值
class User(object): def __init__(self, first_name, last_name, login_attempts): self.first_name = fir ...
- 八 Connect API 连接器
Connect API: 实现一个连接器(connector),不断地从一些数据源系统拉取数据到kafka,或从kafka推送到宿系统(sink system). 大多数Connect使用者不需要直接 ...
- spring+hibernate ---含AOP--事务--laobai
biz包: package com.etc.biz; import java.util.List; import org.springframework.orm.hibernate3.support. ...
- python's thirty-first day for me re模块
正则表达式: re 模块 可以读懂 你写的正则表达式,根据你写的表达式去执行任务. 正则表达式:字符串的操作. 使用一些规则来检测字符串是否符合我的要求 —— 表单验证 从一段字符串中找到符合我要 ...
- Three.js导入gltf模型和动画
核心代码 复杂的3D模型一般都是用第三方建模工具生成,然后加载到three中 three官方推荐使用gltf格式的文件,代表编辑器是blender 本文生成了自定义生成了一个blender模型,并且应 ...
- python签名设计
将一个签名网站http://www.uustv.com/的内容爬下来显示出来 代码:sign.py from tkinter import * from tkinter import messageb ...
- 黑客工具包ShadowBrokers浅析
臭名昭著的方程式组织工具包再次被公开,TheShadowBrokers 在 steemit.com博客上提供了相关消息. 本次被公开的工具包大小为117.9MB,包含23 个黑客工具,其中部分文件显示 ...
- Jenkins+maven+SVN构建java项目中遇到的问题及解决
[ERROR] No goals have been specified for this build. You must specify a valid lifecycle phase or a g ...