Guava ---- EventBus事件驅動模型

  在軟件開發過程當中, 不免有信息的共享或者對象間的協做。 怎樣讓對象間信息共享高效, 並且耦合性低。 這是一個難題。 而耦合性高將帶來編碼改動牽一髮而動全身的連鎖效應。 Spring的風靡正是因爲攻克了高耦合問題。 本篇介紹的EventBus中也用到了Spring中的依賴注入。 來進行對象和對象間的解耦(如@Subscribe)。java

  

  Guava解決高耦合採用的是事件驅動模型的思路。 對象可以訂閱(subscribe)特定的事件或者公佈(publish)特定的事件去被消費。 從如下的代碼可以看出, EventBus對生產者和消費者是透明的, 它無需知道他們的類型。 從而實現瞭解耦。post


TradeAccountEvent: 基本對象兼測試類  this

package com.wenniuwuren.eventbus;
import com.google.common.eventbus.EventBus;

import java.util.Date;

/**
 *  不管何時買賣交易運行, 都會產生一個TradeAccountEvent實例
 */
public class TradeAccountEvent {
    private double amount;
    private Date tradeExecutionTime;
    private String tradeType;
    private String tradeAccount;

    public TradeAccountEvent(String account, double amount,
                             Date tradeExecutionTime, String tradeType) {
        this.amount = amount;
        this.tradeExecutionTime =tradeExecutionTime;
        this.tradeAccount = account;
        this.tradeType = tradeType;
    }

    public static void main(String[] args) {
        // 消費者和生產者依據EventBus對象來一一相應
        EventBus eventBus1 = new EventBus();
        SimpleTradeAuditor simpleTradeAuditor = new SimpleTradeAuditor(eventBus1);
        SimpleTradeExecutor simpleTradeExecutor = new SimpleTradeExecutor(eventBus1);
        simpleTradeExecutor.executeTrade("zhangsan", 10, "Money");
        
        System.out.println("----This is devil dividing line------");
        
        EventBus eventBus2 = new EventBus();
        BuySellTradeExecutor buySellTradeExecutor = new BuySellTradeExecutor(eventBus2);
        AllTradesAuditor allTradesAuditor = new AllTradesAuditor(eventBus2);
        buySellTradeExecutor.executeTrade("lisi", 100, "SELL");
        System.out.println("---------------------");
        buySellTradeExecutor.executeTrade("wangwu", 1000, "BUY");
    }
}




 

AllTradesAuditor:依據不一樣生產者訂閱不一樣內容google

package com.wenniuwuren.eventbus;

import java.util.List;

import com.google.common.collect.Lists;
import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;

public class AllTradesAuditor {
	private List<BuyEvent> buyEvents = Lists.newArrayList();
	private List<SellEvent> sellEvents = Lists.newArrayList();

	public AllTradesAuditor(EventBus eventBus) {
		eventBus.register(this);
	}

	@Subscribe
	public void auditSell(SellEvent sellEvent) {
		sellEvents.add(sellEvent);
		System.out.println("Received TradeSellEvent " + sellEvent);
	}

	@Subscribe
	public void auditBuy(BuyEvent buyEvent) {
		buyEvents.add(buyEvent);
		System.out.println("Received TradeBuyEvent " + buyEvent);
	}
}



BuyEvent:

package com.wenniuwuren.eventbus;

import java.util.Date;

/**
 * 購買事件
 * @author wenniuwuren
 *
 */
public class BuyEvent extends TradeAccountEvent {
	public BuyEvent(String tradeAccount, double amount,
			Date tradExecutionTime) {
		super(tradeAccount, amount, tradExecutionTime, "BUY");
	}
}


SellEvent:
編碼

package com.wenniuwuren.eventbus;

import java.util.Date;

/**
 * 銷售事件
 * @author wenniuwuren
 *
 */
public class SellEvent extends TradeAccountEvent {
	public SellEvent(String tradeAccount, double amount, Date tradExecutionTime) {
		super(tradeAccount, amount, tradExecutionTime, "SELL");
	}
}



BuySellTradeExecutor: 分類型(BUY、SELL)公佈事件
spa

package com.wenniuwuren.eventbus;

import java.util.Date;

import com.google.common.eventbus.EventBus;

/**
 * 分類型(SELL BUY)運行器
 * @author wenniuwuren
 *
 */

public class BuySellTradeExecutor {

	private EventBus eventBus;

    public BuySellTradeExecutor(EventBus eventBus) {

        this.eventBus = eventBus;
    }
	
	private TradeAccountEvent processTrade(String tradeAccount, double amount,
			String tradeType) {
		Date executionTime = new Date();
		String message = String.format("Processed trade for" + tradeAccount
				+ "of amount" + amount + "type" + tradeType + "@"
				+ executionTime);
		TradeAccountEvent tradeAccountEvent;
		if (tradeType.equals("BUY")) {
			tradeAccountEvent = new BuyEvent(tradeAccount, amount,
					executionTime);
		} else {
			tradeAccountEvent = new SellEvent(tradeAccount, amount,
					executionTime);
		}
		System.out.println(message);
		return tradeAccountEvent;
	}
	
	public void executeTrade(String tradeAccount, double amount, String tradeType) {
        TradeAccountEvent tradeAccountEvent = processTrade(tradeAccount, amount, tradeType);
        // 公佈, 通知訂閱者
        eventBus.post(tradeAccountEvent);
    }
}


SimpleTradeAuditor: 最簡單的事件消費者(或者說訂閱者)
package com.wenniuwuren.eventbus;
import com.google.common.collect.Lists;
import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;

import java.util.List;

/**
 * 審覈交易
 */
public class SimpleTradeAuditor {
    private List<TradeAccountEvent> tradeEvents = Lists.newArrayList();

    public SimpleTradeAuditor(EventBus eventBus) {
        // 註冊, 以便獲取TradeAccountEvent的通知
        eventBus.register(this);
    }

    /**
     * 事件處理(用@Subscribe註解表示)
     * @param tradeAccountEvent
     */
    @Subscribe
    public void auditTrade(TradeAccountEvent tradeAccountEvent) {
        tradeEvents.add(tradeAccountEvent);
        System.out.println("Received trade " + tradeAccountEvent);
    }
}



TradeBuyAuditor:分類型事件消費

package com.wenniuwuren.eventbus;

import java.util.List;

import com.google.common.collect.Lists;
import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;

/**
 * 購買審查
 * @author wenniuwuren
 *
 */
public class TradeBuyAuditor {
	private List<BuyEvent> buyEvents = Lists.newArrayList();

	public TradeBuyAuditor(EventBus eventBus) {
		eventBus.register(this);
	}

	@Subscribe
	public void auditBuy(BuyEvent buyEvent) {
		buyEvents.add(buyEvent);
		System.out.println("Received TradeBuyEvent " + buyEvent);
	}

	public List<BuyEvent> getBuyEvents() {
		return buyEvents;
	}
}


 

TradeSellAuditor: 
code

package com.wenniuwuren.eventbus;

import java.util.List;

import com.google.common.collect.Lists;
import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;

/**
 * 銷售審查
 * @author wenniuwuren
 *
 */
public class TradeSellAuditor {
	private List<SellEvent> sellEvents = Lists.newArrayList();

	public TradeSellAuditor(EventBus eventBus) {
		eventBus.register(this);
	}

	@Subscribe
	public void auditSell(SellEvent sellEvent) {
		sellEvents.add(sellEvent);
		System.out.println("Received SellEvent " + sellEvent);
	}

	public List<SellEvent> getSellEvents() {
		return sellEvents;
	}
}


輸出結果:orm

Processed trade forzhangsanof amount10.0typeMoney@Fri Jun 12 02:29:03 CST 2015
Received trade com.wenniuwuren.eventbus.TradeAccountEvent@7c53a9eb
----This is devil dividing line------
Processed trade forlisiof amount100.0typeSELL@Fri Jun 12 02:29:03 CST 2015
Received TradeSellEvent com.wenniuwuren.eventbus.SellEvent@14899482
---------------------
Processed trade forwangwuof amount1000.0typeBUY@Fri Jun 12 02:29:03 CST 2015
Received TradeBuyEvent com.wenniuwuren.eventbus.BuyEvent@21588809



參考資料:對象

                《Getting Started with Google Guava》blog

相關文章
相關標籤/搜索