一、创建产品角色接口:
package org.burning.sport.design.pattern.factorypattern.spring.factory; public interface SignService { /** * 获取签名的工具 * @return */ EnumSign getSignTool(); /** * 签名 * @param name 用户姓名 */ void write(String name); }
二、创建产品角色抽象类:
package org.burning.sport.design.pattern.factorypattern.spring.factory; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; public abstract class AbstractSignService implements SignService { /** * 把内容写入到文件中 * @param content 内容 */ public void writeFileContent(String content) { File file = new File("d:/test.txt"); FileOutputStream fos = null; try { if(file.exists()) { file.delete(); } fos = new FileOutputStream(file); fos.write(content.getBytes()); } catch (Exception e) { e.printStackTrace(); } finally { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } }
三、具体产品角色
package org.burning.sport.design.pattern.factorypattern.spring.factory.impl; import org.burning.sport.design.pattern.factorypattern.spring.factory.AbstractSignService; import org.burning.sport.design.pattern.factorypattern.spring.factory.EnumSign; import org.springframework.stereotype.Component; @Component public class BrushPenSignServiceImpl extends AbstractSignService { @Override public EnumSign getSignTool() { return EnumSign.BRUSH_PEN; } @Override public void write(String name) { String content = "大家好,我是" + name + ",现在在用毛笔签名"; writeFileContent(content); } }
package org.burning.sport.design.pattern.factorypattern.spring.factory.impl; import org.burning.sport.design.pattern.factorypattern.spring.factory.AbstractSignService; import org.burning.sport.design.pattern.factorypattern.spring.factory.EnumSign; import org.springframework.stereotype.Component; @Component public class PencilSignServiceImpl extends AbstractSignService { @Override public EnumSign getSignTool() { return EnumSign.PENCIL; } @Override public void write(String name) { String content = "大家好,我是" + name + ",现在在用铅笔签名"; writeFileContent(content); } }
package org.burning.sport.design.pattern.factorypattern.spring.factory.impl; import org.burning.sport.design.pattern.factorypattern.spring.factory.AbstractSignService; import org.burning.sport.design.pattern.factorypattern.spring.factory.EnumSign; import org.springframework.stereotype.Component; @Component public class PenSignServiceImpl extends AbstractSignService { @Override public EnumSign getSignTool() { return EnumSign.PEN; } @Override public void write(String name) { String content = "大家好,我是" + name + "现在在用钢笔签名"; writeFileContent(content); } }
四、工厂角色
SignServiceFactory实现了Spring的InitializingBean方法,在容器启动时,就会运行afterPropertiesSet()方法,通过applicationContext把产品角色都存储到一个HashMap中去
package org.burning.sport.design.pattern.factorypattern.spring.factory; import org.springframework.beans.BeansException; import org.springframework.beans.factory.InitializingBean; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; import java.util.HashMap; import java.util.Map; @Component public class SignServiceFactory implements ApplicationContextAware, InitializingBean { private ApplicationContext applicationContext; private static Map<EnumSign, SignService> map = new HashMap<>(); public SignService getInstance(EnumSign enumSign) { return map.get(enumSign); } @Override public void afterPropertiesSet() throws Exception { Map<String, SignService> beansOfType = applicationContext.getBeansOfType(SignService.class); for(Map.Entry<String, SignService> entry : beansOfType.entrySet()) { map.put(entry.getValue().getSignTool(), entry.getValue()); } } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; } }
五,客户端访问
package org.burning.sport.design.pattern.factorypattern.spring.factory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class ClientTest { public static void main(String[] args) { ApplicationContext apx = new ClassPathXmlApplicationContext("applicationContext.xml"); SignServiceFactory signServiceFactory = apx.getBean("signServiceFactory", SignServiceFactory.class); SignService signService = signServiceFactory.getInstance(EnumSign.PEN); signService.write("红孩儿"); } }
代码:https://gitee.com/play-happy/base-project
org.burning.sport.design.pattern.factorypattern.spring.factory