一、创建产品角色接口:

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

工厂模式-Spring的InitializingBean实现的更多相关文章

  1. 简单工厂模式,工厂方法模式,抽象工厂模式,spring的狂想

    菜鸟D在项目中遇见一个比较纠结的高耦合,所以就想办法来解耦.情况是这样的:系统通过用户选择treeview控件的节点判断调用不同的处理,这些处理中某些东西又是类似的.同事的建议是采用简单工厂,耦合就耦 ...

  2. 使用工厂模式解耦和IoC思想

    使用工厂模式解耦. 一.需求场景: 某一层功能需要改动,但其他层代码不变 实现类1:MyDaoImpl查询自己的数据库. ====改为====> 实现类2:MyDaoImpl2从其它地址得到数据 ...

  3. Java设计模式之工厂模式的两种实现方式

    工厂模式(Factory Pattern)是 Java 中最常用的设计模式之一.这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式. 1. 为什么要有工厂模式? "Talk i ...

  4. 工厂模式模拟Spring的bean加载过程

    一.前言    在日常的开发过程,经常使用或碰到的设计模式有代理.工厂.单例.反射模式等等.下面就对工厂模式模拟spring的bean加载过程进行解析,如果对工厂模式不熟悉的,具体可以先去学习一下工厂 ...

  5. Spring中的工厂模式和单例模式

    Spring预备知识(适合中小型项目) 作用:集成和管理其他框架 工厂模式: A  a  = new A( ); 将类所要创建的对象写入工厂,统一进行管理 package com.spring; pu ...

  6. 从基础知识到重写Spring的Bean工厂中学习java的工厂模式

    1.静态工厂模式其他对象不能直接通过new得到某个类,而是通过调用getInstance()方法得到该类的对象这样,就可以控制类的产生过程.顺带提一下单例模式和多例模式:  单例模式是指控制其他对象获 ...

  7. Spring 实现两种设计模式:工厂模式和单态模式(单例模式)

    本文摘自:李刚 著 <轻量级 Java EE企业应用实战 Struts2+Spring+hibernate整合开发> 在Spring 中大量使用的以下两种设计模式:工厂模式和单态模式. 工 ...

  8. Spring学习13-中IOC(工厂模式)和AOP(代理模式)的详细解释

    我们是在使用Spring框架的过程中,其实就是为了使用IOC,依赖注入,和AOP,面向切面编程,这两个是Spring的灵魂. 主要用到的设计模式有工厂模式和代理模式. IOC是工厂模式参考:设计模式- ...

  9. spring通过工厂模式解决页面耦合问题

    spring通过工厂模式解决页面耦合问题

随机推荐

  1. 100-days: nineteen

    Title: Figure skating(花样滑冰): Olympic(奥林匹克的) champion Hanyu says '100 percent' on(引出时机) return to ice ...

  2. FortiGate防火墙HA下联堆叠交换机

    1.拓扑图 2.防火墙配置 3.交换机配置 interface GigabitEthernet1/0/47 switchport access vlan 30 switchport mode acce ...

  3. 顶级项目孵化的故事系列——Kylin的心路历程【转】

    现在已经名满天下的 Apache Kylin,是 Hadoop 大数据生态系统不可或缺的一部分,要知道在 Kylin 项目早期,可是以华人为主的开源团队,一路披荆斩棘经过几年的奋斗,才在 Apache ...

  4. 742. Closest Leaf in a Binary Tree查找最近的叶子节点

    [抄题]: Given a binary tree where every node has a unique value, and a target key k, find the value of ...

  5. springboot + @KafkaListener 手动提交及消费能力优化

    转载 https://blog.csdn.net/asd5629626/article/details/82776450  https://blog.csdn.net/asd5629626/artic ...

  6. Spark2.0学习(一)--------Spark简介

    官网对Spark的介绍 http://spark.apache.org/ Apache Spark™ is a unified analytics engine for large-scale dat ...

  7. vue.$nextTick 解决了哪些问题

    转载:https://www.cnblogs.com/xuewuhen/p/7860989.html $nextTick 是在下次 DOM 更新循环结束之后执行延迟回调,在修改数据之后使用 $next ...

  8. linux ">/dev/null 2>&1 &"

    0:表示键盘输入(stdin)1:表示标准输出(stdout),系统默认是1 2:表示错误输出(stderr) command >/dev/null 2>&1 &  == ...

  9. Monotonic Array LT896

    An array is monotonic if it is either monotone increasing or monotone decreasing. An array A is mono ...

  10. Spring 添加属性集中常见方法

    //创建容器,索要对象, package cn.lijun.Test; import org.junit.Test;import org.springframework.context.Applica ...