【设计模式】FactoryPattern工厂模式
Factory Pattern
简单工厂模式
将变化的部分封装起来
//简单工厂
class SimpleProductFactory{
Product createProduct(String type){
//选择--varied
if (type.equals("type1")){
product = new ...
}else if(...){
...
}else{
...
}
return product;
}
} Product orderProduct(String type){ SimpleProductFactory factory = new SimpleProductFactory(); Product product = factory.createProduct(type); //other operations... } //进一步封装
public class ProductStore{
SimpleProductFactory factory;
public ProductStore(SimpleProductFactory factory){
this.factory = factory;
} public Product orderProduct(String type){
Product product = factory.createProduct(type);
//other operations... }
} /*
这一步:
使用了一个Store类,包含了生产产品的方法。
factory在构造函数的时候传进来。
推迟了new操作。
*/
工厂方法模式
It defines an interface for creating an object, but lets subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclass.
//工厂方法模式
public abstract class ProductStore{
public Product orderProduct(String type){
Product product = createProduct(type);
//product.test();
//other methods...
return product;
} protected abstract Product createProduct; //->工厂方法
//other methods...
}
抽象工厂模式
Abstract Factory creates a family of concrete objects together needed by the client.
//抽象工厂模式 //原料工厂
public interface ProductIngredientFactory{ public Ingredient1 createIngredient1();
public Ingredient2 createIngredient2();
public Ingredient3 createIngredient3();
//...
}
//具体的原料工厂类
//实现每一种对应的ingredient public class Product1 extends Product{
ProductIngredientFactory ingredientFactory;
public Product1(ProductIngredientFactory ingredientFactory){
this.ingredientFactory = ingredientFactory;
}
void prepare(){
ingredient1 = ingredientFactory.createIngredient1();
ingredient2 = ingredientFactory.createIngredient2();
ingredient3 = ingredientFactory.createIngredient3();
} } //重构产品商店
//每个具体的商店(继承自抽象的ProductStore)都要实现里面的工厂方法
//可能不止createProduct这一种工厂方法
//在这个工厂方法里,实现具体工厂的实例(包含了多个abstract方法),将具体工厂传入产品中 public class ProductStore1 extends ProductStore{ //implement an abstract method
protected Product createProduct(String type){
Product product = null;
ProductIngredientFactory ingredientFactory = new ProductIngredientFactory1(); //指定是原料工厂1,具体的类 if (type.equals("type1")){
product = new Product1(ingredientFactory);
}else if(...){
...
}else{
...
}
return product;
}
}
【设计模式】FactoryPattern工厂模式的更多相关文章
- 设计模式——抽象工厂模式及java实现
设计模式--抽象工厂模式及java实现 设计模式在大型软件工程中很重要,软件工程中采用了优秀的设计模式有利于代码维护,方便日后更改和添加功能. 设计模式有很多,而且也随着时间在不断增多,其中最著名的是 ...
- 5. 星际争霸之php设计模式--抽象工厂模式
题记==============================================================================本php设计模式专辑来源于博客(jymo ...
- 3. 星际争霸之php设计模式--简单工厂模式
题记==============================================================================本php设计模式专辑来源于博客(jymo ...
- iOS 设计模式之工厂模式
iOS 设计模式之工厂模式 分类: 设计模式2014-02-10 18:05 11020人阅读 评论(2) 收藏 举报 ios设计模式 工厂模式我的理解是:他就是为了创建对象的 创建对象的时候,我们一 ...
- 设计模式之工厂模式(Factory)
设计模式的工厂模式一共有三种:简单工厂模式,工厂模式,抽象工厂模式 简单工厂模式原理:只有一个工厂类,通过传参的形式确定所创建的产品对象种类 代码如下: #include <stdio.h> ...
- php设计模式:工厂模式
php设计模式:工厂模式 意图: 定义一个用于创建对象的接口,让子类决定实例化哪一个类. 工厂模式实现: 工厂模式中任何创建对象的工厂类都要实现这个接口,实现接口的方法体中都要实现接口中的方法,它声明 ...
- 浅析JAVA设计模式之工厂模式(一)
1 工厂模式简单介绍 工厂模式的定义:简单地说,用来实例化对象,取代new操作. 工厂模式专门负责将大量有共同接口的类实例化.工作模式能够动态决定将哪一个类实例化.不用先知道每次要实例化哪一个类. 工 ...
- java 设计模式之工厂模式与反射的结合
工厂模式: /** * @author Rollen-Holt 设计模式之 工厂模式 */ interface fruit{ public abstract void eat(); } ...
- C#学习之设计模式:工厂模式
最近研究一下设计模式中工厂模式的应用,在此记录如下: 什么是工厂模式? 工厂模式属于设计模式中的创造型设计模式的一种.它的主要作用是协助我们创建对象,为创建对象提供最佳的方式.减少代码中的耦合程度,方 ...
- [JS设计模式]:工厂模式(3)
简单工厂模式是由一个方法来决定到底要创建哪个类的实例, 而这些实例经常都拥有相同的接口. 这种模式主要用在所实例化的类型在编译期并不能确定, 而是在执行期决定的情况. 说的通俗点,就像公司茶水间的饮料 ...
随机推荐
- 在Eclipse的kepler中执行OSGIproject出错的解决方式
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/canlets/article/details/29620301 今天学习OSGI的过程中依照书上所述 ...
- MyBatis 配置/注解 SQL CRUD 经典解决方案(2019.08.15持续更新)
本文旨在记录使用各位大神的经典解决方案. 2019.08.14 更新 Mybatis saveOrUpdate SelectKey非主键的使用 MyBatis实现SaveOrUpdate mybati ...
- Zookeeper——启动闪退
Zookeeper好久不启动了,昨天项目要用Zookeeper了,我昨天突然启动它,调皮的zk居然害羞不让我看见它,启动不了,一启动就闪退,为啥呢?其实是因为报错了,有错zk启动时就会报错,所以昨 ...
- git-ssh-keygen
ssh-keygen 先看本地是否已经有了密钥 cd ~/.ssh 该文件夹下会包含两个文件 id_rsa --私钥 id_rsa.pub --公钥 如果没有这两个文件的话就需要重新生成(有的话使用一 ...
- 记一次用Linux curl命令获取Django url返回值异常的问题
问题描述: curl 检测 URL 返回值以判断服务器是否正常 原命令:curl -I -m 10 -o /dev/null -s -w %{http_code} --insecure $url 问题 ...
- 一键部署YApi
编写docker-compose.yml version: '2.1' services: yapi: image: mrjin/yapi:latest # build: ./ container_n ...
- 数据库_MHA群集搭建
MHA概念介绍,群集搭建与测试 一, MHA介绍 1.概念:MHA master high availability,由日本DeNA公司开发,解决mysql故障切换可以做到0-30秒,而且在故障切换过 ...
- windows10安装nodejs 10和express 4
最进做一个个人博客系统,前端用到了semanticUI,但是要使用npm工具包,所以需要安装nodejs,nodejs自带npm 下载 去官网下载自己系统对应的版本,我的是windows:下载 可以在 ...
- 解释ARP协议和RARP协议
解释ARP(地址解析协议) 首先,每个主机都会在自己的ARP缓冲区中建立一个ARP列表,以表示IP地址和MAC地址之间的对应关系. 当源主机要发送数据时,首先检查ARP列表中是否有对应IP地址的目的主 ...
- openssl版本升级
1.查看openssl版本 [root@node2 openssh-8.0p1]# openssl version OpenSSL 1.0.1e-fips 11 Feb 2013 2.下载指定版本的o ...