Spring温故知新(六)AOP面向切面编程 <1>
这一章我们将开始剖析Spring框架最为重要的AOP(Aspect Oriented Programming)面向切面编程。可以说Spring的精华就在于AOP了。
所谓AOP,就是相对于OOP(Object Oriented Programming)面向对象编程的说法,有些人喜欢叫面向切面编程,有些人喜欢叫做面向方面,事实上这两个都是指同一个东西,只是叫法不同。
我们传统的编程都是面向对象,就是说每个类都有它实际的意义。而面向切面略有不同,它在面向对象的基础上扩展了一下,它编程的时候不是先考虑的一个具体对象(比如用户类),而是先考虑的对象的行为或者功能。这个不是编程方法的不同,而是编程思维的转变。
理论性的东西还是放一边,我们用实际的机器人案例来慢慢理解这个概念。
为了突出重点我们这里重写了ISpeak:
package com.iteye.bolide74.impl;public interface ISpeaker {public void say(String msg);}
package com.iteye.bolide74.action;import com.iteye.bolide74.impl.ISpeaker;public class Robot implements ISpeaker {public String name;public String getName() {return name;}public void setName(String name) {this.name = name;}public Robot(String name) {this.name = name;}@Overridepublic void say(String msg) {System.out.println("到达邻居家,对邻居说:" + msg + ",我是" + this.name);}}
package com.iteye.bolide74.tester;import com.iteye.bolide74.action.Robot;import com.iteye.bolide74.impl.ISpeaker;public class Tester {public static void main(String[] args) {ISpeaker speaker = new Robot("Wall-E");speaker.say("你好");}}
public void getGift() {System.out.println("获取了一个礼物");}public void giveGift(){System.out.println("赠予一个礼物");}
package com.iteye.bolide74.action;import com.iteye.bolide74.impl.ISpeaker;public class SpeakerProxy implements ISpeaker {ISpeaker speaker;public SpeakerProxy(ISpeaker speaker) {super();this.speaker = speaker;}@Overridepublic void say(String msg) {getGift();speaker.say(msg);giveGift();}public void getGift() {System.out.println("获取了一个礼物");}public void giveGift(){System.out.println("赠予一个礼物");}}
package com.iteye.bolide74.tester;import com.iteye.bolide74.action.Robot;import com.iteye.bolide74.action.SpeakerProxy;import com.iteye.bolide74.impl.ISpeaker;public class SpeakerProxyTester {public static void main(String[] arg0) {// 没有带礼物的机器人:ISpeaker noGiftSpeaker = new Robot("空手来的Wall-E");noGiftSpeaker.say("你好");System.out.println();// 带了礼物的机器人ISpeaker speaker = new SpeakerProxy(new Robot("有礼而来的Wall-E"));speaker.say("你好");}}
package com.iteye.bolide74.action;import com.iteye.bolide74.impl.ISpeaker;//礼物的一种:一束花public class Flower extends SpeakerGiftDecorator {public Flower(ISpeaker speaker) {super(speaker);}@Overridepublic void say(String msg) {getGift();this.speaker.say(msg);giveGift();}@Overridepublic void getGift() {System.out.println("获取了一束花");}@Overridepublic void giveGift() {System.out.println("赠予一束花");}}
package com.iteye.bolide74.tester;import com.iteye.bolide74.action.Flower;import com.iteye.bolide74.action.Fruit;import com.iteye.bolide74.action.Money;import com.iteye.bolide74.action.Robot;import com.iteye.bolide74.impl.ISpeaker;public class SpeakerGiftDecoratorTester {public static void main(String[] args) {// 一种礼物:ISpeaker speaker = new Flower(new Robot("我是带花来的Wall-E"));speaker.say("Hello");System.out.println();// 两种礼物:speaker = new Money(new Flower(new Robot("我是带了花和红包的Wall-E")));speaker.say("Hello");System.out.println();// 另外两种礼物:speaker = new Fruit(new Money(new Robot("我是带了水果和钱的Wall-E")));speaker.say("Hello");System.out.println();// 三种礼物:speaker = new Fruit(new Money(new Flower(new Robot("我是三种礼物都带的Wall-E"))));speaker.say("Hello");}}