GOF业务场景的设计模式-----工厂模式
定义:定义一个用于创建对象的接口,让子类决定实例化哪一个类,工厂方法使一个类的实例化延迟到其子类。
工厂方法模式 基本代码
interface IProduct {
public void productMethod();
}
class Product implements IProduct {
public void productMethod() {
System.out.println("产品");
}
}
interface IFactory {
public IProduct createProduct();
}
class Factory implements IFactory {
public IProduct createProduct() {
return new Product();
}
}
public class Client {
public static void main(String[] args) {
IFactory factory = new Factory();
IProduct prodect = factory.createProduct();
prodect.productMethod();
}
}
业务场景
工厂模式是一个用于实例化对象的模式,是用工厂方法代替new操作的一种方式。工厂模式在Java项目中到处都是,因为工厂模式就相当于创建实例对象的new,如在我们的系统中经常需要记日志,如果创建logger实例时所做的初始化工作可能是很长一段代码,可能要初始化、赋值、查询数据等等,则会导致代码臃肿而难看
private static Logger logger = LoggerFactory.getLogger(MyBusinessRPC.class);
logger.error("数据查询失败,table" + ...);
//源码分析
public static Logger getLogger(String name) {
ILoggerFactory iLoggerFactory = getILoggerFactory();
return iLoggerFactory.getLogger(name);
}
//源码分析
public static ILoggerFactory getILoggerFactory() {
if (INITIALIZATION_STATE == UNINITIALIZED) {
INITIALIZATION_STATE = ONGOING_INITIALIZATION;
performInitialization();
}
switch (INITIALIZATION_STATE) {
case SUCCESSFUL_INITIALIZATION:
return StaticLoggerBinder.getSingleton().getLoggerFactory();
case NOP_FALLBACK_INITIALIZATION:
return NOP_FALLBACK_FACTORY;
case FAILED_INITIALIZATION:
throw new IllegalStateException(UNSUCCESSFUL_INIT_MSG);
case ONGOING_INITIALIZATION:
// support re-entrant behavior.
// See also http://bugzilla.slf4j.org/show_bug.cgi?id=106
return TEMP_FACTORY;
}
throw new IllegalStateException("Unreachable code");
}
业务场景
比如我们要从集合(库存中)中取出一个数据,或者一些调度算法,我们有很策略实现(先进先出,最少使用,最近最少使用,最近最少使用 等)
public class ArithmeticFactory {
public Arithmetic createFiFoArithmetic(){
Arithmetic arithmetic = new FIFOStockArithmetic();
return arithmetic;
}
public Arithmetic createNoUseArithmetic(){
Arithmetic arithmetic = new NoUseStockArithmetic();
return arithmetic;
}
}
public interface Arithmetic {
public void invoke();
}
public class FIFOStockArithmetic implements Arithmetic {
@Override
public void invoke() {
System.out.println("使用了先进先出算法!");
//具体算法的业务逻辑
}
}
public class NoUseStockArithmetic implements Arithmetic {
@Override
public void invoke() {
System.out.println("使用了最少使用算法!");
//具体算法的业务逻辑
}
}
测试 结果
public class FactoryTest {
public static void main(String args[]){
ArithmeticFactory arithmeticFactory = new ArithmeticFactory();
Arithmetic fiFoArithmetic = arithmeticFactory.createFiFoArithmetic();
Arithmetic noUserarithmetic = arithmeticFactory.createNoUseArithmetic();
fiFoArithmetic.invoke();
noUserarithmetic.invoke();
}
}

业务场景
spring 和 mybatis 的sqlSessonFactory
<!-- 配置集成Mybatis -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:/config/SQLMapConfig.xml" />
<property name="mapperLocations" value="classpath*:com/*/*/**/infra/mybatis/*Mapper.xml"/>
</bean>
<bean id="simpleTempalte" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
<constructor-arg index="1" value="SIMPLE"/>
</bean>
<bean id="batchTempalte" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
<constructor-arg index="1" value="BATCH"/>
</bean>
GOF业务场景的设计模式-----工厂模式的更多相关文章
- GOF业务场景的设计模式-----策略模式
定义:定义一组算法,将每个算法都封装起来,并且使他们之间可以互换. 策略模式代码实现 interface IStrategy { public void doSomething(); } class ...
- GOF业务场景的设计模式-----单例模式
个人觉得 纯粹的学习设计模式,是不对的.也不能为了使用设计模式,而硬搬设计模式来使用 单例模式可能是 最简单的设计模式也是 大家知道最多的设计模式.当然 ,有很多种写法 定义:确保一个类只有一个实例, ...
- GOF业务场景的设计模式-----观察者模式
定义:定义对象间一种一对多的依赖关系,使得当每一个对象改变状态,则所有依赖于它的对象都会得到通知并自动更新. 在软件系统中经常会有这样的需求:如果一个对象的状态发生改变,某些与它相关的对象也要随之做出 ...
- GOF业务场景的设计模式-----责任链模式
定义:使多个对象都有机会处理请求,从而避免了请求的发送者和接收者之间的耦合关系.将这些对象连成一条链,并沿着这条链传递该请求,直到有对象处理它为止. 首先来看一段代码: public void tes ...
- GOF业务场景的设计模式-----设计模式六大原则
单一职责原则(Single Responsibility Principle) 定义:不要存在多于一个导致类变更的原因.通俗的说,即一个类只负责一项职责. 问题由来:类T负责两个不同的职责:职责P1, ...
- .NET设计模式: 工厂模式
.NET设计模式: 工厂模式(转) 转自:http://www.cnblogs.com/bit-sand/archive/2008/01/25/1053207.html .NET设计模式(1): ...
- 【设计模式】Java设计模式 -工厂模式
[设计模式]Java设计模式 -工厂模式 不断学习才是王道 继续踏上学习之路,学之分享笔记 总有一天我也能像各位大佬一样 一个有梦有戏的人 @怒放吧德德 分享学习心得,欢迎指正,大家一起学习成长! 目 ...
- 转载 用Python实现设计模式——工厂模式
转载自 SegmentFault作者 夏秋, https://segmentfault.com/a/1190000013053013 非常感谢这位作者的深入浅出的讲解. 前言 工厂模式,顾名思义就是我 ...
- [Design Pattern With Go]设计模式-工厂模式
这次介绍的设计模式是工厂模式,这是一个比较常见的创建型模式.一般情况下,工厂模式分为三种:简单工厂.工厂方法和抽象工厂,下面慢慢举例介绍下. 简单工厂 考虑一个加密程序的应用场景,一个加密程序可能提供 ...
随机推荐
- bzoj3283: 运算器
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...
- sql查找最后一个字符匹配
DECLARE @str AS VARCHAR(25)='123_234_567' select substring(@str,1,LEN(@str)-CHARINDEX('_',reverse(@s ...
- UVa 101 The Blocks Problem Vector基本操作
UVa 101 The Blocks Problem 一道纯模拟题 The Problem The problem is to parse a series of commands that inst ...
- DirectX的引用找不到问题
今天要用C#开发(vs2010下)DirectSound应用,按照网上说得在: 但我系统里怎么也找不到,我确定安装了DirectX9.0SDK ,因为在C++里做的开发都好好的. 最后看到Dircet ...
- 【问题】R文件报错原因及解决办法 (转)
错误如图.下面是几种解决方法(网上搜集的). 1.如果是导入项目出现这个问题,一般是R文件没有更新造成(据说导入项目,R不会自动更新).可以Project——clean以下,R文件会重新生成. 2.选 ...
- JavaWeb学习总结-09 JDBC 学习和使用
一 JDBC相关概念介绍 1.1 数据库驱动 这里的驱动的概念和平时听到的那种驱动的概念是一样的,比如平时购买的声卡,网卡直接插到计算机上面是不能用的,必须要安装相应的驱动程序之后才能够使用声卡和网卡 ...
- Nginx个人简单理解
首先我们来补充下一些基本知识: 什么是代理服务器? 先举个简单的例子,现在我们在百度访问谷歌的网站,发现现在进不去,这个时候我们可以FQ(关于FQ,可以借鉴下这个博文:http://zhangge.n ...
- easyUI-combobox 后台导入Json数据的方法
一.前台页面: <input id="List" class="easyui-combobox" data-options="valueFiel ...
- cpg数据库处理_找到未提取的pdf
cpg数据库处理_找到未提取的pdf,存放于文件夹Chinese_undeal_pdfs move_unextracted_pdfs.py # -*- coding: utf-8 -*- " ...
- python类的高级属性
---恢复内容开始--- 类方法:通过@classmethod装饰器实现,类方法和普通方法的区别是,类方法只能访问类变量,不能访问实例变量,代码如下: class Person(object): de ...