设计模式之享元模式

flyWeight也称蝇量模式。首先设计一个接口,再设计一个类实现该接口,再设计一个创建该对象的工厂,最后是客户端主类。

接口:

package com.freshbin.pattern.flyweight.myexample.order;

/**
 * 订单类
 * 
 * @author freshbin
 * @date 2019年1月15日 下午8:56:33
 */
public interface Order {
	void sell();
}

实现类:

package com.freshbin.pattern.flyweight.myexample.order.coffee;

import com.freshbin.pattern.flyweight.myexample.order.Order;

/**
 * 咖啡类
 * 
 * @author freshbin
 * @date 2019年1月18日 下午5:04:32
 */
public class CoffeeOrder implements Order {

	private static Integer count = 0;
	
	private String type;

	public CoffeeOrder(String type) {
		CoffeeOrder.count++;
		this.type = type;
	}

	@Override
	public void sell() {
		System.out.println("卖出" + type + "咖啡");
	}

	public static Integer getCount() {
		return count;
	}

}

工厂:

package com.freshbin.pattern.flyweight.myexample.order.coffee;

import java.util.HashMap;
import java.util.Map;

import com.freshbin.pattern.flyweight.myexample.order.Order;

public class CoffeeOrderFactory {
	private static volatile CoffeeOrderFactory coffeeOrderFactory;

	private static Map<String, Order> coffeeMap = new HashMap<>();

	private CoffeeOrderFactory() {

	}

	public static CoffeeOrderFactory getCoffeeOrderFactory() {
		if (coffeeOrderFactory == null) {
			synchronized (CoffeeOrderFactory.class) {
				if (coffeeOrderFactory == null) {
					coffeeOrderFactory = new CoffeeOrderFactory();
				}
			}
		}

		return coffeeOrderFactory;
	}

	public static Order getOrder(String type) {
		if (!coffeeMap.containsKey(type)) {
			switch (type) {
			case "摩卡":
				coffeeMap.put(type, new CoffeeOrder(type));
				break;
			case "拿铁":
				coffeeMap.put(type, new CoffeeOrder(type));
				break;

			default:
				break;
			}
		}

		return coffeeMap.get(type);
	}

	public static Order getOrderNotFlyWeight(String type) {
		switch (type) {
		case "摩卡":
			coffeeMap.put(type, new CoffeeOrder(type));
			break;
		case "拿铁":
			coffeeMap.put(type, new CoffeeOrder(type));
			break;

		default:
			break;
		}

		return coffeeMap.get(type);
	}
}

客户端主类:

package com.freshbin.pattern.flyweight.myexample;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.freshbin.pattern.bridge.example.bridge.newTvControl;
import com.freshbin.pattern.flyweight.myexample.order.Order;
import com.freshbin.pattern.flyweight.myexample.order.coffee.CoffeeOrder;
import com.freshbin.pattern.flyweight.myexample.order.coffee.CoffeeOrderFactory;

/**
 * 享元模式
 * 
 * @author freshbin
 * @date 2019年1月15日 下午8:55:15
 */
public class FlyWeightPatternDemo {
	public static List<Order> orders = new ArrayList<>(100);
	public static List<Order> ordersNotFlyWeight = new ArrayList<>(100);
	public static Map<Integer, String> coffeeTypeMap;
	
	static {
		coffeeTypeMap = new HashMap<>(100);
		coffeeTypeMap.put(0, "摩卡");
		coffeeTypeMap.put(1, "拿铁");
	}
	
	public static void main(String[] args) {
		System.out.println("===============蝇量模式===========");
		for(int i = 0; i < 100; i++) {
			Integer type = i % 2;
			countSellNum(coffeeTypeMap.get(type));
		}
		
		System.out.println("卖出的咖啡数:" + orders.size());
		
		System.out.println("=================");
		System.out.println("新增的coffee对象:" + CoffeeOrder.getCount());
		
		/*System.out.println("===============非蝇量模式===========");
		for(int i = 0; i < 100; i++) {
			Integer type = i % 2;
			countSellNumNotFlyWeight(coffeeTypeMap.get(type));
		}
		
		System.out.println("卖出的咖啡数:" + ordersNotFlyWeight.size());
		
		System.out.println("=================");
		System.out.println("新增的coffee对象:" + CoffeeOrder.getCount());*/
	}
	
	public static void countSellNum(String type) {
		orders.add(CoffeeOrderFactory.getCoffeeOrderFactory().getOrder(type));
	}
	
	public static void countSellNumNotFlyWeight(String type) {
		ordersNotFlyWeight.add(CoffeeOrderFactory.getCoffeeOrderFactory().getOrderNotFlyWeight(type));
	}
}

效果:

本来写了一个比较复杂的,后来发现自己把控不来,所以就删减成这版了,我觉得蝇量模式,应该是第一次没有该对象,就new一个,有的话就从之前存起来的map或者其他集合中取出来,不过应该一般都是用map存,毕竟有key可以判断这个对象是否已经创建了。

github地址:https://github.com/freshbin/designPattern

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值