大话设计模式_外观模式

通过买基金来间接买股票获得收入为例。

package com.wzs.design;

/**
 * 大话设计模式--page105 外观模式
 * 
 * @author Administrator
 * 
 */
public class AppearanceModel {
	public static void main(String[] args) {
		Fund fund = new Fund(new Stock1(), new Stock2(), new Stock3());
		fund.buyFund();
		fund.sellFund();
	}
}

/*
 * 基金
 */
class Fund {
	private Stock1 stock1;
	private Stock2 stock2;
	private Stock3 stock3;

	public Fund(Stock1 stock1, Stock2 stock2, Stock3 stock3) {
		this.stock1 = stock1;
		this.stock2 = stock2;
		this.stock3 = stock3;
	}

	// 买基金
	public void buyFund() {
		stock1.buy();
		stock2.buy();
		stock3.buy();
	}

	// 卖基金
	public void sellFund() {
		stock1.sell();
		stock2.sell();
		stock3.sell();
	}

	public Stock1 getStock1() {
		return stock1;
	}

	public void setStock1(Stock1 stock1) {
		this.stock1 = stock1;
	}

	public Stock2 getStock2() {
		return stock2;
	}

	public void setStock2(Stock2 stock2) {
		this.stock2 = stock2;
	}

	public Stock3 getStock3() {
		return stock3;
	}

	public void setStock3(Stock3 stock3) {
		this.stock3 = stock3;
	}

}

/*
 * 股票
 */
abstract class Stock {
	public abstract void sell();

	public abstract void buy();
}

/*
 * 股票1
 */
class Stock1 extends Stock {

	@Override
	public void buy() {
		System.out.println("股票1买入.");
	}

	@Override
	public void sell() {
		System.out.println("股票1卖出.");
	}
}

/*
 * 股票2
 */
class Stock2 extends Stock {

	@Override
	public void buy() {
		System.out.println("股票2买入.");
	}

	@Override
	public void sell() {
		System.out.println("股票2卖出.");
	}
}

/*
 * 股票3
 */
class Stock3 extends Stock {

	@Override
	public void buy() {
		System.out.println("股票3买入.");
	}

	@Override
	public void sell() {
		System.out.println("股票3卖出.");
	}
}


你可能感兴趣的:(外观模式,大话设计模式,基金买卖)