您的位置:首页 > 其它

抽象工厂设计模式

2015-07-05 17:06 411 查看
/**

抽象工厂设计模式:与工厂设计模式是一样的,只不过抽象工厂是多产品系的。

工厂模式是单产品系的。



一个生产汽车的工厂,生产TopCar、TopBus,MidCar、MidBus,LowCar、LowBus.

利用抽象设计模式的方法,提取共同的特征。

当产生第四个SuperCar()类的时候,只需要在原来代码上增添SuperCar类实现接口,创建工厂即可。

而不需要再更改原来的代码。

*/

interface ICar{

//System.out.println("定义一些生产汽车共有的方法");

}

class TopCar implements ICar{

public TopCar(){

System.out.println("生产出高档汽车");

}

}

class MidCar implements ICar{

public MidCar(){

System.out.println("生产出中档汽车");

}

}

class LowCar implements ICar{

public LowCar(){

System.out.println("生产出低档汽车");

}

}

interface IBus{

//System.out.println("定义一些生产公共汽车共有的方法");

}

class TopBus implements IBus{

public TopBus(){

System.out.println("生产出高档公共汽车");

}

}

class MidBus implements IBus{

public MidBus(){

System.out.println("生产出中档公共汽车");

}

}

abstract class AbstractFactory{

public abstract ICar creatCar();

public abstract IBus creatBus();

}

class TopCarFactory extends AbstractFactory{

public ICar creatCar(){

return new TopCar(); //调用此方法后,相当于:ICar ic = new TopCar();

}

public IBus creatBus(){

return new TopBus();

}

}

class MidCarFactory extends AbstractFactory{

public ICar creatCar(){

return new MidCar(); //调用此方法后,相当于:ICar ic = new MidCar();

}

public IBus creatBus(){

return new MidBus();

}

}

class LowCarFactory extends AbstractFactory{

public ICar creatCar(){

return new LowCar(); //调用此方法后,相当于:ICar ic = new LowCar();

}

public IBus creatBus(){

return null;

}

}

public class AbstractFactoryDemos{

public static void main(String []args){

LowCarFactory lcf = new LowCarFactory();

lcf.creatCar();

lcf.creatBus();

}

}

//抽象工厂------例子2(文件读取)

/**

要求:

编写读文件功能。具体功能是:读取文本文件,包括(GBK,UTF-8,UNICoDe)编码下的文本文件,要求获得全文内容;

获取图像文件(BMP,GIF,JPG)文件,要求获得图像宽度、长度、每一点的RGB三基色信息。

定义泛型接口是解决返回值类型不同的较好方法,屏蔽方法参数个数差异利用“String ... in”形式实现。

*/

import java.io.*;

import javax.imageio.*;

import java.awt.image.*;

interface IRead<T>{

T read(String ... in);

}

class FileRead implements IRead<String>{

public String read(String ... in){ //可以传入多个参数

String result = null;

try{

File file = new File(in[0]); //in[0]表示文件名

int len = 0;

FileInputStream input = new FileInputStream(file);

byte buf[] = new byte[1024]; //缓冲区大小等于文件长度

// FileOutputStream output = new FileOutputStream("");

// while((len=input.read(buf)) != -1){

// output.write(buf,0,len);

// }

input.read(buf); //一次性读完文件长度

result = new String(buf,in[1]); //按in[1]编码方式转换成课件字符串

input.close();

}catch(Exception e){

e.getMessage();

}

return result;

}

}

class ImageInfo{

private int width;

private int height;

private int r[][];

private int g[][];

private int b[][];

public void setWidth(int width){

this.width = width;

}

public void setHeight(int height){

this.height = height;

}

public int[][] getR(){

return r;

}

public int[][] getG(){

return g;

}

public int[][] getB(){

return b;

}

public int getHeight() {

// TODO Auto-generated method stub

return height;

}

public int getWidth() {

// TODO Auto-generated method stub

return width;

}

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]&0x00ff00)>>8;

b[i][j] = rgb[pos + j]&0x0000ff;

}

}

}

public String toString(){

return "width:"+this.getWidth()+" height:"+this.getHeight()+" r:"+this.getR()+" g:"+this.getG()+" b:"+this.getB();

}

}

class ImageRead implements IRead<ImageInfo>{ //读图像文件

BufferedImage bi = null;

public ImageInfo read(String ... in){

File file = new File(in[0]);
//in[0]表示图像的文件名

try{

/**

static BufferedImage read(File input)

返回一个 BufferedImage,作为使用 ImageReader(它是从当前已注册 ImageReader 中自动选择的)解码所提供 File 的结果。

*/

bi = ImageIO.read(file);

}catch(Exception e){

e.printStackTrace();

}

int width = bi.getWidth();

int height = bi.getHeight();

int rgb[] = new int[width * height];

//将数据图像读到result缓冲区

/**

int[] getRGB(int startX, int startY, int w, int h, int[] rgbArray, int offset, int scansize)

从图像数据的某一部分返回默认 RGB 颜色模型 (TYPE_INT_ARGB) 和默认 sRGB 颜色空间中整数像素数组。 */

bi.getRGB(0,0,width,height,rgb,width,height);

ImageInfo obj= new ImageInfo(); //设置图像信息

obj.setWidth(width);

obj.setHeight(height);

obj.setRGB(rgb);

return obj;

}

}

//自动选择工厂

abstract class AbstractFactory{

private static final String TEXT = "text";

private static final String IMAGE = "image";

abstract IRead creat();

static AbstractFactory creat(String mark){

AbstractFactory factory = null;

if(mark.equals(TEXT)){ //相当于:AbstractFactory factory = new TextFactory();

factory = new TextFactory();

}else if(mark.equals(IMAGE)){

factory = new ImageFactory();

}

return factory;

}

}

class TextFactory extends AbstractFactory{

public IRead creat(){
//IRead ifileread = new FileRead();

return new FileRead();

}

}

class ImageFactory extends AbstractFactory{

public IRead creat(){

return new ImageRead();

}

}

public class ReadInfoDemo{

public static void main(String []args){

String result = (String)AbstractFactory.creat("text").creat().read("F://JavaOracle/OOP/AbstractDemos.java","GBK");

//TextFactory tf = AbstractFactory.creat("text");

// IRead ifread = tf.creat();

//String result = ifread.read("F:/JavaOracle/OOP/AbstractDemos.java","GBK");

System.out.println("读取文件的信息:"+result);

ImageInfo ir = (ImageInfo)AbstractFactory.creat("image").creat().read("F://JavaOracle/18.png");

System.out.println("读取图片的信息:"+ir);

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: