本网页所有文字内容由 imapbox邮箱云存储,邮箱网盘, iurlBox网页地址收藏管理器 下载并得到。
ImapBox 邮箱网盘 工具地址: https://www.imapbox.com/download/ImapBox.5.5.1_Build20141205_CHS_Bit32.exe
PC6下载站地址:PC6下载站分流下载
本网页所有视频内容由 imoviebox边看边下-网页视频下载, iurlBox网页地址收藏管理器 下载并得到。
ImovieBox 网页视频 工具地址: https://www.imapbox.com/download/ImovieBox4.7.0_Build20141115_CHS.exe
本文章由: imapbox邮箱云存储,邮箱网盘,ImageBox 图片批量下载器,网页图片批量下载专家,网页图片批量下载器,获取到文章图片,imoviebox网页视频批量下载器,下载视频内容,为您提供.
1.定义 抽象工厂是一种常用的对象创建型设计模式。抽象工厂模式提供了一种方式,可以将一组具有统一主题的单独工厂封装起来,它提供一个创建一系列相关或相互依赖对象的接口,而无需制定它们具体的类。 2.结构 抽象工厂的结构图如下所示: AbstractFactory : 声明一个创建抽象产品对象的操作接口 ConcreteFactory : 实现创建具体产品对象的操作 AbstractProduct : 为一个类产品对象声明一个接口 ConcreteProduct : 定义一个将被相应的具体工厂创建产品的对象,实现AbstractProduct Client : 仅使用由AbstractFactory和AbstractProduct类声明的接口 3.适用性 举个例子说明下,我们定义一个Factory接口,它有createMessageMethod和createPayMethod两个方法,分别表示创建通信方式和创建支付方式。然后定义三个实现Factory接口的类:AliFactory, BaiduFactory, TecentFactory, 在三个类中重写createMessageMethod和createPayMedthod,分别返回每家公司对应的通信方式/支付方式的对象。然后建立MessageMeans和PayMethod接口,用于获取通信方式和支付方式,再分别定义三个类实现这两个接口,定义每家公司对应的实现方式信息。下面请看源码: Factory.java:
interface Factory { MessageMeans createMessageMethod(); PayMethod createPayMethod(); }
AliFactory.java
public class AliFactory implements Factory { @Override public MessageMeans createMessageMethod() { // TODO Auto-generated method stub return new AliMessageMeans(); } @Override public PayMethod createPayMethod() { // TODO Auto-generated method stub return new AliPayMethod(); } }
BaiduFactory.java
public class BaiduFactory implements Factory { @Override public MessageMeans createMessageMethod() { // TODO Auto-generated method stub return new BaiduMessageMeans(); } @Override public PayMethod createPayMethod() { // TODO Auto-generated method stub return new BaiduPayMethod(); } }
TecentFactory.java
public class TecentFactory implements Factory{ @Override public MessageMeans createMessageMethod() { // TODO Auto-generated method stub return new TecentMessageMeans(); } @Override public PayMethod createPayMethod() { // TODO Auto-generated method stub return new TecentPayMethod(); } }
MessageMeans.java
public interface MessageMeans { void getMessageMeans(); }
AliMessageMeans.java
public class AliMessageMeans implements MessageMeans { @Override public void getMessageMeans() { // TODO Auto-generated method stub System.out.println("MessageMeans:来往"); } }
BaiduMessageMeans.java
public class BaiduMessageMeans implements MessageMeans { @Override public void getMessageMeans() { // TODO Auto-generated method stub System.out.println("MessageMeans:百度Hi"); } }
TecentMessageMeans.java
public class TecentMessageMeans implements MessageMeans { @Override public void getMessageMeans() { // TODO Auto-generated method stub System.out.println("MessageMeans:微信"); } }
PayMethod.java
interface PayMethod { void getPaymentMethod(); }
AliPayMethod.java
public class AliPayMethod implements PayMethod { @Override public void getPaymentMethod() { // TODO Auto-generated method stub System.out.println("PaymentMethod:支付宝"); } }
BaiduPayMethod.java
public class AliFactory implements Factory { @Override public MessageMeans createMessageMethod() { // TODO Auto-generated method stub return new AliMessageMeans(); } @Override public PayMethod createPayMethod() { // TODO Auto-generated method stub return new AliPayMethod(); } }
0TecentPayMethod.java
public class AliFactory implements Factory { @Override public MessageMeans createMessageMethod() { // TODO Auto-generated method stub return new AliMessageMeans(); } @Override public PayMethod createPayMethod() { // TODO Auto-generated method stub return new AliPayMethod(); } }
1
看了这么多源码,是不是有点头晕~~,其实他们的关系也不是很复杂,我们来理一理:
下面让我们来测试一下功能:
TestFactory
public class AliFactory implements Factory { @Override public MessageMeans createMessageMethod() { // TODO Auto-generated method stub return new AliMessageMeans(); } @Override public PayMethod createPayMethod() { // TODO Auto-generated method stub return new AliPayMethod(); } }
2控制台输出:
public class AliFactory implements Factory { @Override public MessageMeans createMessageMethod() { // TODO Auto-generated method stub return new AliMessageMeans(); } @Override public PayMethod createPayMethod() { // TODO Auto-generated method stub return new AliPayMethod(); } }
3从测试中我们可以看到,ConcreteFactory(AliFactory, BaiduFactory, TecentFactory)负责创建产品的对象,隐藏了产品对象的实现细节,实现了客户与产品的实现的分离。还有一个ConcreteFactory仅在应用中出现一次,即在其初始化的时候,这使得改变一个应用的具体工厂很容易,比如我们这样写
public class AliFactory implements Factory { @Override public MessageMeans createMessageMethod() { // TODO Auto-generated method stub return new AliMessageMeans(); } @Override public PayMethod createPayMethod() { // TODO Auto-generated method stub return new AliPayMethod(); } }
4只要改一行代码就可以更换工厂对象。但是,AbstractFactory难以支持新品种的产品,这是因为AbstractFactory接口确定了可以被创建的产品集合,支持新种类的产品就需要扩展该工厂接口,这将涉及AbstractFactory及其所有子类的改变。
参考文献:
阅读和此文章类似的: 程序员专区