Factory.java
上传用户:hensond
上传日期:2021-12-27
资源大小:817k
文件大小:1k
- package com.company.section6;
- import java.util.HashMap;
- import java.util.Map;
- /**
- * @author cbf4Life cbf4life@126.com
- * I'm glad to share my knowledge with you all.
- */
- public class Factory {
- private static final Map<String,Product> prMap = new HashMap();
-
- public static synchronized Product createProduct(String type) throws Exception{
- Product product =null;
-
- //如果Map中已经有这个对象
- if(prMap.containsKey(type)){
- product = prMap.get(type);
- }else{
- if(type.equals("Product1")){
- product = new ConcreteProduct1();
- }else{
- product = new ConcreteProduct2();
- }
- //同时把对象放到缓存容器中
- prMap.put(type,product);
- }
- return product;
- }
- }