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可以判断这个对象是否已经创建了。