设计模式 之 外观模式

       

下载 23种设计模式源码 :http://download.csdn.net/download/knight_black_bob/8936043

 

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


创建型模式,共五种:
工厂方法模式 抽象工厂模式 单例模式 建造者模式 原型模式

结构型模式,共七种:
适配器模式 装饰器模式 代理模式 外观模式 桥接模式 组合模式 享元模式

行为型模式,共十一种:
策略模式 模板方法模式 观察者模式 迭代子模式 责任链模式 命令模式

备忘录模式 状态模式 访问者模式 中介者模式 解释器模式

 

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

 

 

 

 

package 设计模式.外观模式;

public class Product {

	private String name;
	private double   price;
	private int   count;
	
	
	public Product(){}
	public Product(String name){
		this.name = name;
	}
	public Product(String name, double price, int count) {
		this.name = name;
		this.price = price;
		this.count = count;
	}
 

	public int getCount() {
		return count;
	}
	public void setCount(int count) {
		this.count = count;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	
	
	
	
	@Override
	public boolean equals(Object obj) {
		if (obj != null)
			if (obj instanceof Product)
				return this.name == ((Product) obj).getName();
		return false;
	}

	
	@Override
	public String toString() {
		StringBuffer sb =new StringBuffer();
		sb.append(getClass().getName().substring(getClass().getName().lastIndexOf(".")+1,getClass().getName().length()));
		sb.append("[");
		sb.append("\"name\":\""+name+"\",");
		sb.append("\"count\":"+count+",");
		sb.append("\"price\":"+price); 
		sb.append("]");
		return sb.toString();
	}
}

 

package 设计模式.外观模式;

import java.util.ArrayList;
import java.util.List;

public class Shop {

	static  List<Product> plist = new ArrayList<Product>();
	
	static {
		Product  apple = new Product("apple",2.5,200);
		Product  pear = new Product("pear",1.0,200);
		plist.add(apple);
		plist.add(pear);
	}
	
	public  double getProPrice(String name){
		Product pro =null;
		for (Product p : plist) { 
			if (p.equals(new Product(name)) == true) {
				pro =p;
				break;
			}
		}
		if (pro !=null) 
			return pro.getPrice();
		else 
			return 0.0;
	}
	
	public boolean rebuy(String name ,int count ){
		boolean flag =false; 
		Product pro =null;
		for (Product p : plist) {
			flag = flag | p.equals(new Product(name));
			if (flag == true) {
				pro =p;
				break;
			}
		}
		if (flag == true && pro !=null) 
			if (pro.getCount() >= count)  
				flag = true;
			else flag = false;
		else flag =false;
		return flag;
	}
	
	public boolean buy(String name ,int count ){
		boolean flag =false; 
		Product pro =null;
		for (Product p : plist) {
			flag = flag | p.equals(new Product(name));
			if (flag == true) {
				pro =p;
				break;
			}
		}
		plist.remove(pro);
		pro.setCount(pro.getCount()-count);
		plist.add(pro);
	 
		return flag;
	}
	
	public  List<Product> getList(){
		return plist;
	} 
}

 

package 设计模式.外观模式;

public class Wallet {

	static  double money = 10000.00;
	
	public  boolean check(double price){
		boolean flag =false;
		if (money >  price ){ 
			flag = true; 
		}else 
			flag =false;
		return flag;
	}
	
	public  boolean pay(double price){
		boolean flag =false;
		if (money >  price ){ 
			money -= price;
			flag = true; 
		}else 
			flag =false;
		return flag;
	}
	
	public  double  balance(){
		return money;
	}
}

 

 

package 设计模式.外观模式;

import java.util.ArrayList;
import java.util.List;

public class Order {
	static  List<Product> plist = new ArrayList<Product>();
	
	public boolean addOrder(Product p){
		plist.add(p);
		return true;
	}
	
	public boolean addOrder(String name, double price, int count){
		Product p = new Product ( name ,price ,count) ;
		return this.addOrder(p);
	}
	
	public   List<Product>  getOrderList(){
		return plist;
	}
}

 

package 设计模式.外观模式;

import java.util.List;

public class Pay {

	Shop shop = new Shop();
	Wallet mWallet = new Wallet();
	Order order= new Order();
	
	public boolean doPay(String name ,int count){
		boolean flag = false;
		
		flag = flag | shop.rebuy(name, count);
		if (flag == false) return flag; 
		
		flag = flag & mWallet.check(shop.getProPrice(name) * count);
		if (flag == false) return flag;
		
		flag = flag &shop.buy(name, count);
		if (flag == false) return flag; 
		
		flag = flag &mWallet.pay(shop.getProPrice(name) * count);
		if (flag == false) return flag;
		
		flag = flag & order.addOrder(name, shop.getProPrice(name) * count, count);
		return flag;
	}
	
	public  double balance(){
		return  mWallet.balance();
	}
	
	public  String  myOrder(){
		StringBuffer sb = new StringBuffer();
		sb.append("{");
		  List<Product> list = order.getOrderList();
		for (int i = 0; i < list.size(); i++) {
			Product p = list.get(i);
			sb.append("[\"name\":\""+p.getName()+"\"");
			sb.append("\"price\":\""+p.getPrice()+"\"");
			sb.append("\"count\":\""+p.getCount()+"\"],");
			if (i == list.size()-1) 
				sb = sb.replace(sb.length()-1, sb.length(), "");  
		}
		sb.append("}");
		return sb.toString();
	}
	
	public  String  productList(){
		StringBuffer sb = new StringBuffer();
		sb.append("{");
		  List<Product> list = shop.getList();
		for (int i = 0; i < list.size(); i++) {
			Product p = list.get(i);
			sb.append("[\"name\":\""+p.getName()+"\"");
			sb.append("\"price\":\""+p.getPrice()+"\"");
			sb.append("\"count\":\""+p.getCount()+"\"],");
			if (i == list.size()-1) 
				sb = sb.replace(sb.length()-1, sb.length(), "");  
		}
		sb.append("}");
		return sb.toString();
	}
	
}

 

package 设计模式.外观模式;

public class FacadeTest {

	//简化外部客户程序和系统间的交互接口
	//为子系统中的一组接口提供一个一致的界面,Facade模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。[GOF 《设计模式》]
	public static void main(String[] args) {
		Pay pay = new Pay();
		System.out.println("余额:" +pay.balance());
		System.out.println("商品信息:" +pay.productList());
		System.out.println("我的购物车:" +pay.myOrder());
		
		System.out.println("支付是否成功:" + pay.doPay("apple", 2));
		System.out.println("支付是否成功:" + pay.doPay("apple", 200000));
		System.out.println("支付是否成功:" + pay.doPay("banana", 2));
		
		System.out.println("余额:" +pay.balance());
		System.out.println("商品信息:" +pay.productList());
		System.out.println("我的购物车:" +pay.myOrder());
	}
}

 


设计模式 之 外观模式_第1张图片
 

 

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(设计模式,外观模式)