首页 > 代码库 > 读书笔记_java设计模式深入研究 第三章 工厂模式 Factory

读书笔记_java设计模式深入研究 第三章 工厂模式 Factory

1,简单工厂
 -1,定制抽象接口。
 -2,定制具体子类。
 -3,定制工厂类,通过工厂类的静态方法返回不同的子类对象。
package pattern.chp02.facroty;
/**
 *  类描述:汽车接口
 * 
 *  @author:  Jing
 *  @version  $Id: Exp$ 
 *
 *  History:  Dec 18, 2014 10:50:10 AM   Jing   Created.
 *           
 */
public interface ICar {
    
}  
/**
* 类描述:高档汽车
*
* @author: Jing
* @version $Id: Exp$
*
* History: Dec 18, 2014 10:52:06 AM Jing Created.
*
*/
public class TopCar implements ICar{
 
}
/**
* 类描述:中档汽车
*
* @author: Jing
* @version $Id: Exp$
*
* History: Dec 18, 2014 10:52:56 AM Jing Created.
*
*/
public class MidCar implements ICar {
 
}
package pattern.chp02.facroty;
 
/**
* 类描述:抵挡汽车
*
* @author: Jing
* @version $Id: Exp$
*
* History: Dec 18, 2014 10:53:25 AM Jing Created.
*
*/
public class LowCar implements ICar {
 
}
/**
*
* @(#) CarSimpleFactory.java
* @Package pattern.chp02.facroty
*
* Copyright ? JING Corporation. All rights reserved.
*
*/
 
package pattern.chp02.facroty;
 
/**
* 类描述:汽车工厂类
*
* @author: Jing
* @version $Id: Exp$
*
* History: Dec 18, 2014 10:54:16 AM Jing Created.
*
*/
public class CarSimpleFactory {
/**
*
* 方法说明:根据传入的汽车类型,返回不同的对象
*
* Author: Jing Create Date: Dec 18, 2014 11:19:05 AM
*
* @param carType
* @return
* @return ICar
*/
public static ICar createCar(CarType carType) {
ICar obj = null;
if (carType == CarType.TOP_CAR) {
 
obj = new TopCar();
} else if (carType == CarType.MID_CAR) {
 
obj = new MidCar();
} else if (carType == CarType.LOW_CAR) {
 
obj = new LowCar();
}
return obj;
}
}
/**
*
* @(#) CarType.java
* @Package pattern.chp02.facroty
*
* Copyright ? JING Corporation. All rights reserved.
*
*/
 
package pattern.chp02.facroty;
 
/**
* 类描述:汽车类型
*
* @author: Jing
* @version $Id: Exp$
*
* History: Dec 18, 2014 11:14:34 AM Jing Created.
*
*/
public enum CarType {
/**
* 高档汽车
*/
TOP_CAR,
/**
* 中档汽车
*/
MID_CAR,
/**
* 抵挡汽车
*/
LOW_CAR;
}
/**
*
* @(#) CarTest.java
* @Package pattern.chp02.facroty
*
* Copyright ? JING Corporation. All rights reserved.
*
*/
 
package pattern.chp02.facroty;
 
/**
* 类描述:
*
* @author: Jing
* @version $Id: Exp$
*
* History: Dec 18, 2014 11:29:07 AM Jing Created.
*
*/
public class CarTest {
public static void main(String[] args) {
ICar obj = CarSimpleFactory.createCar(CarType.LOW_CAR);
System.out.println(obj);//pattern.chp02.facroty.LowCar@19821f
}
}
2,工厂模式
    -1,定义抽象接口。
    -2,定义产品子类。
    -3,定义抽象工厂类。
    -4,实现不同产品类对应的具体工厂类。
工厂方法更加的易于软件的维护和开发,相比于简单工厂方式,不需要每次修改工厂类的方法,每次只需要增加新的产品类和产品类的工厂类即可。
package pattern.chp02.facroty;
/**
 *  类描述:抽象工厂接口
 * 
 *  @author:  Jing
 *  @version  $Id: Exp$ 
 *
 *  History:  Dec 18, 2014 2:27:41 PM   Jing   Created.
 *           
 */
public abstract class AbstractFactory {
    /**
     * 
     * 方法说明:创建ICar对象
     *
     * Author:       Jing                
     * Create Date:   Dec 18, 2014 2:28:12 PM
     *
     * @return
     * @return ICar
     */
    public abstract ICar create();
}
package pattern.chp02.facroty;
 
/**
* 类描述:高档汽车工厂类
*
* @author: Jing
* @version $Id: Exp$
*
* History: Dec 18, 2014 2:36:38 PM Jing Created.
*
*/
public class TopFactory extends AbstractFactory {
 
@Override
public ICar create() {
return new TopCar();
}
 
}
public class CarTest {
public static void main(String[] args) {
ICar obj = CarSimpleFactory.createCar(CarType.LOW_CAR);
System.out.println(obj);
AbstractFactory factory = new TopFactory();
ICar car = factory.create();
System.out.println(car);//pattern.chp02.facroty.TopCar@9304b1
}
}
3,抽象工厂
    -1,抽象工厂用来一个工厂生产多种类型的汽车。
/**
*
* @(#) IBus.java
* @Package pattern.chp02.facroty
*
* Copyright ? JING Corporation. All rights reserved.
*
*/
 
package pattern.chp02.facroty;
 
/**
* 类描述:公共汽车
*
* @author: Jing
* @version $Id: Exp$
*
* History: Dec 18, 2014 3:05:10 PM Jing Created.
*
*/
public interface IBus {
 
}
/**
*
* @(#) UpBus.java
* @Package pattern.chp02.facroty
*
* Copyright ? JING Corporation. All rights reserved.
*
*/
 
package pattern.chp02.facroty;
 
/**
* 类描述:高级公共汽车
*
* @author: Jing
* @version $Id: Exp$
*
* History: Dec 18, 2014 3:05:46 PM Jing Created.
*
*/
public class UpBus implements IBus{
 
}
/**
*
* @(#) MidBus.java
* @Package pattern.chp02.facroty
*
* Copyright ? JING Corporation. All rights reserved.
*
*/
 
package pattern.chp02.facroty;
 
/**
* 类描述:中档公共汽车
*
* @author: Jing
* @version $Id: Exp$
*
* History: Dec 18, 2014 3:17:48 PM Jing Created.
*
*/
public class MidBus implements IBus {
 
}
/**
*
* @(#) AbstractFactory.java
* @Package pattern.chp02.facroty
*
* Copyright ? JING Corporation. All rights reserved.
*
*/
 
package pattern.chp02.facroty;
 
/**
* 类描述:抽象工厂接口
*
* @author: Jing
* @version $Id: Exp$
*
* History: Dec 18, 2014 2:27:41 PM Jing Created.
*
*/
public abstract class AbstractFactory {
/**
*
* 方法说明:创建ICar对象
*
* Author: Jing
* Create Date: Dec 18, 2014 2:28:12 PM
*
* @return
* @return ICar
*/
public abstract ICar create();
/**
*
* 方法说明:创建公共汽车IBus对象
*
* Author: Jing
* Create Date: Dec 18, 2014 3:18:58 PM
*
* @return
* @return IBus
*/
public abstract IBus createBus();
}
/**
*
* @(#) TopFactory.java
* @Package pattern.chp02.facroty
*
* Copyright ? JING Corporation. All rights reserved.
*
*/
 
package pattern.chp02.facroty;
 
/**
* 类描述:高档汽车工厂类
*
* @author: Jing
* @version $Id: Exp$
*
* History: Dec 18, 2014 2:36:38 PM Jing Created.
*
*/
public class TopFactory extends AbstractFactory {
 
@Override
public ICar create() {
return new TopCar();
}
 
@Override
public IBus createBus() {
return new UpBus();
}
 
}
4,具体应用:
    编写读文件功能。读取文件,包括编码(GBK,UTF-8,UNIONCODE)的文本文件,要求获取文本内容。读取图像文件(BMP,GIF,JPG),要求获取图像宽度、高度、长度、以及每一点的RGB基色信息。
-1,首先我们可以写出一个基本的工厂类模式,如下所示,生成对应的读取总接口,以及对应的文本和图像抽象类,及对应的子列,对应子类的工厂方法,实现要求,部分代码如下:
    
/**
*
* @(#) IRead.java
* @Package pattern.chp02.facroty.file
*
* Copyright ? JING Corporation. All rights reserved.
*
*/
 
package pattern.chp02.facroty.file;
 
/**
* 类描述:读取文件接口类
*
* @author: Jing
* @version $Id: Exp$
*
* History: Dec 18, 2014 3:27:54 PM Jing Created.
*
*/
public interface IRead {
/**
*
* 方法说明:根据文件名称读取文件
*
* Author: Jing
* Create Date: Dec 18, 2014 3:28:25 PM
*
* @param fileName
* @return void
*/
public void read(String fileName);
}
/**
*
* @(#) AbstractImgRead.java
* @Package pattern.chp02.facroty.file
*
* Copyright ? JING Corporation. All rights reserved.
*
*/
 
package pattern.chp02.facroty.file;
 
/**
* 类描述:图像读取抽象类
*
* @author: Jing
* @version $Id: Exp$
*
* History: Dec 18, 2014 3:32:14 PM Jing Created.
*
*/
public abstract class AbstractImgRead implements IRead {
 
public abstract void read(String fileName);
}
/**
*
* @(#) AbstractTextRead.java
* @Package pattern.chp02.facroty.file
*
* Copyright ? JING Corporation. All rights reserved.
*
*/
 
package pattern.chp02.facroty.file;
 
/**
* 类描述:获取文本文件的抽象类
*
* @author: Jing
* @version $Id: Exp$
*
* History: Dec 18, 2014 3:29:14 PM Jing Created.
*
*/
public abstract class AbstractTextRead implements IRead {
 
public abstract void read(String fileName) ;
}
/**
*
* @(#) BMPImgRead.java
* @Package pattern.chp02.facroty.file
*
* Copyright ? JING Corporation. All rights reserved.
*
*/
 
package pattern.chp02.facroty.file;
 
/**
* 类描述:BMP图片读取
*
* @author: Jing
* @version $Id: Exp$
*
* History: Dec 18, 2014 3:35:57 PM Jing Created.
*
*/
public class BMPImgRead extends AbstractImgRead {
 
@Override
public void read(String fileName) {
// TODO Auto-generated method stub
 
}
 
}
/**
*
* @(#) AbstractFactory.java
* @Package pattern.chp02.facroty.file
*
* Copyright ? JING Corporation. All rights reserved.
*
*/
 
package pattern.chp02.facroty.file;
 
/**
* 类描述:工厂类
*
* @author: Jing
* @version $Id: Exp$
*
* History: Dec 18, 2014 3:49:15 PM Jing Created.
*
*/
public abstract class AbstractFactory {
/**
*
* 方法说明:创建对应的文件读取实例
*
* Author: Jing
* Create Date: Dec 18, 2014 3:49:46 PM
*
* @return
* @return IRead
*/
public abstract IRead create();
}
/**
*
* @(#) GBKTextFactory.java
* @Package pattern.chp02.facroty.file
*
* Copyright ? JING Corporation. All rights reserved.
*
*/
 
package pattern.chp02.facroty.file;
 
/**
* 类描述:GBK文本读取工厂类
*
* @author: Jing
* @version $Id: Exp$
*
* History: Dec 18, 2014 3:50:24 PM Jing Created.
*
*/
public class GBKTextFactory extends AbstractFactory {
 
@Override
public IRead create() {
return null;
}
}

2,使用基于jdk  API的方法实现文件读写,使用反射实现生成对应的类名。
/**
*
* @(#) IRead.java
* @Package pattern.chp02.facroty.read
*
* Copyright ? JING Corporation. All rights reserved.
*
*/
 
package pattern.chp02.facroty.read;
 
/**
* 类描述:读文本、图像文件接口
*
* @author: Jing
* @version $Id: Exp$
*
* History: Dec 18, 2014 4:14:28 PM Jing Created.
*
*/
public interface IRead<T> {
/**
*
* 方法说明:根据传入的文件名读取文件
*
* Author: Jing Create Date: Dec 18, 2014 4:15:27 PM
*
* @param in
* @return
* @return T
*/
T read(String... in);
}
/**
*
* @(#) ImageRead.java
* @Package pattern.chp02.facroty.read
*
* Copyright ? JING Corporation. All rights reserved.
*
*/
 
package pattern.chp02.facroty.read;
 
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
 
import javax.imageio.ImageIO;
 
/**
* 类描述:图像文件读取
*
* @author: Jing
* @version $Id: Exp$
*
* History: Dec 18, 2014 5:16:26 PM Jing Created.
*
*/
public class ImageRead implements IRead<ImageInfo>{
 
public ImageInfo read(String... in) {
BufferedImage bi = null;
File f = new File(in[0]);//in[0]存储文件名
try {
bi = ImageIO.read(f);
} catch (IOException e) {
e.printStackTrace();
}
int width = bi.getWidth();
int height = bi.getHeight();
int rgb[] = new int[width * height];
bi.getRGB(0, 0, width, height, rgb, width, height);
ImageInfo info = new ImageInfo();
info.setWidth(width);
info.setHeight(height);
info.setRGB(rgb);
return info;
}
 
}
/**
*
* @(#) TextRead.java
* @Package pattern.chp02.facroty.read
*
* Copyright ? JING Corporation. All rights reserved.
*
*/
 
package pattern.chp02.facroty.read;
 
import java.io.File;
import java.io.FileInputStream;
 
 
/**
* 类描述:文本读取
*
* @author: Jing
* @version $Id: Exp$
*
* History: Dec 18, 2014 4:17:19 PM Jing Created.
*
*/
public class TextRead implements IRead<String> {
 
public String read(String... in) {
String result = null;
try{
File file = new File(in[0]);
long len = file.length();//文件长度
FileInputStream fis = new FileInputStream(in[0]);//in[0] 存储文件名称
byte buf[] = new byte[(int)len];
fis.read(buf);
result = new String(buf, in[1]);//in[1]存储编码方式
fis.close();
}catch(Exception e){
System.out.println(e.getMessage());
}
return result;
}
 
}
/**
*
* @(#) ImageInfo.java
* @Package pattern.chp02.facroty.read
*
* Copyright ? JING Corporation. All rights reserved.
*
*/
 
package pattern.chp02.facroty.read;
 
/**
* 类描述:图像信息
*
* @author: Jing
* @version $Id: Exp$
*
* History: Dec 18, 2014 4:31:28 PM Jing Created.
*
*/
public class ImageInfo {
/**
* 图像宽度
*/
private int width;
/**
* 图像高度
*/
private int height;
/**
* 红色分量
*/
private int r[][];
/**
* 绿色分量
*/
private int g[][];
/**
* 颜色分量
*/
private int b[][];
 
/**
*
* 方法说明:设置三基色信息
*
* Author: Jing Create Date: Dec 18, 2014 4:33:51 PM
*
* @param rgb
* @return void
*/
public void setRGB(int rgb[]) {
 
r = new int[height][width];
g = new int[height][width];
b = new int[height][width];
 
int pos = 0;
for (int i = 0; i < height; i++) {
 
pos = width * i;
for (int j = 0; j < width; j++) {
 
r[i][j] = (rgb[pos + j] & 0xff0000) >> 16;
g[i][j] = (rgb[pos + j] & 0xff0000) >>8;
b[i][j] = (rgb[pos + j] & 0xff0000);
}
}
}
 
/**
* @return the width
*/
public int getWidth() {
return width;
}
 
/**
* @param width
* the width to set
*/
public void setWidth(int width) {
this.width = width;
}
 
/**
* @return the height
*/
public int getHeight() {
return height;
}
 
/**
* @param height
* the height to set
*/
public void setHeight(int height) {
this.height = height;
}
 
/**
* @return the r
*/
public int[][] getR() {
return r;
}
 
/**
* @param r
* the r to set
*/
public void setR(int[][] r) {
this.r = r;
}
 
/**
* @return the g
*/
public int[][] getG() {
return g;
}
 
/**
* @param g
* the g to set
*/
public void setG(int[][] g) {
this.g = g;
}
 
/**
* @return the b
*/
public int[][] getB() {
return b;
}
 
/**
* @param b
* the b to set
*/
public void setB(int[][] b) {
this.b = b;
}
 
}

/**
*
* @(#) AbstractFactory.java
* @Package pattern.chp02.facroty.read
*
* Copyright ? JING Corporation. All rights reserved.
*
*/
 
package pattern.chp02.facroty.read;
 
/**
* 类描述:抽象工厂
*
* @author: Jing
* @version $Id: Exp$
*
* History: Dec 18, 2014 5:27:02 PM Jing Created.
*
*/
public abstract class AbstractFactory {
/**
*
* 方法说明:获取对应的读取对象
*
* Author: Jing
* Create Date: Dec 18, 2014 5:30:08 PM
*
* @return
* @return IRead
*/
@SuppressWarnings("unchecked")
public abstract IRead create();
/**
*
* 方法说明:使用反射,根据掺入的类名生成对应的对象
*
* Author: Jing
* Create Date: Dec 18, 2014 5:32:59 PM
*
* @param className
* @return
* @return AbstractFactory
*/
static AbstractFactory create(String className){
AbstractFactory factory = null;
try{
Class<?> clazz = Class.forName(className);
factory = (AbstractFactory) clazz.newInstance();
}catch(Exception e){
e.printStackTrace();
}
return factory;
}
}

/**
*
* @(#) ImageFactory.java
* @Package pattern.chp02.facroty.read
*
* Copyright ? JING Corporation. All rights reserved.
*
*/
 
package pattern.chp02.facroty.read;
 
/**
* 类描述:图像读取
*
* @author: Jing
* @version $Id: Exp$
*
* History: Dec 18, 2014 5:30:34 PM Jing Created.
*
*/
public class ImageFactory extends AbstractFactory {
 
@SuppressWarnings("unchecked")
@Override
public IRead create() {
return new ImageRead();
}
 
}


简而言之,工厂模式是提供一种对象的创建方式。

读书笔记_java设计模式深入研究 第三章 工厂模式 Factory