Java抽象工厂模式

 

抽象工厂模式,也称为工厂的工厂,由一个工厂创建其他工厂

当使用抽象工厂模式时,我们首先使用超级工厂创建工厂,然后使用创建的工厂创建对象

-------------------------------------------------------------------------------------------------------------------------------

使用抽象工厂的步骤:

1.建2个接口

package com.test;

public interface IPrinter {
	void print();
}
package com.test;

public interface IShape {
	void draw();
}

2.建一个抽象类来封装这2个接口

package com.test;

public abstract class AbstractFactory {
	abstract IPrinter getPrinter(String type);
	abstract IShape   getShape(String shape);
}

3.分别写这2个接口的实现类

package com.test;

public class Circle implements IShape{

	public void draw() {
		// TODO Auto-generated method stub
		System.out.println("Inside Circle::draw() method.");
	}

}
package com.test;

public class Rectangle implements IShape {

	public void draw() {
		System.out.println("Inside Rectangle::draw() method");
	}

}
package com.test;

public class Square implements IShape{

	public void draw() {
		System.out.println("Inside Square::draw() method.");
	}

}
package com.test;

public class PaperPrinter implements IPrinter {

	public void print() {
		// TODO Auto-generated method stub
		System.out.println("paper");
	}

}
package com.test;

public class ScreenPrinter implements IPrinter {

	public void print() {
		// TODO Auto-generated method stub
		System.out.println("screen");
	}

}
package com.test;

public class WebPrinter implements IPrinter {

	public void print() {
		// TODO Auto-generated method stub
		System.out.println("Web");
	}

}

4.创建2个接口的工厂类

package com.test;

public class ShapeFactory extends AbstractFactory{

	IShape getShape(String shapeType) {
		if(shapeType == null){
			return null;
		}
		if(shapeType.equalsIgnoreCase("circle")){
			return new Circle();
		}else if(shapeType.equalsIgnoreCase("rectangle")){
			return new Rectangle();
		}else if(shapeType.equalsIgnoreCase("square")){
			return new Square();
		}
		return null;
	}

	IPrinter getPrinter(String type) {
		return null;
	}

}
package com.test;

public class PrinterFactory extends AbstractFactory{

	IPrinter getPrinter(String type) {
		if(type == null){
			return null;
		}
		if(type.equalsIgnoreCase("paper")){
			return new PaperPrinter();
		}else if(type.equalsIgnoreCase("web")){
			return new WebPrinter();
		}else if(type.equalsIgnoreCase("screen")){
			return new ScreenPrinter();
		}
		return null;
	}

	IShape getShape(String shape) {
		// TODO Auto-generated method stub
		return null;
	}

}

5.创建超级工厂类,来获取2个子工厂

package com.test;

public class FactoryProducer {

	public static AbstractFactory getFactory(String choice) {
		
		if(choice.equalsIgnoreCase("shape")){
			return new ShapeFactory();
		}else if(choice.equalsIgnoreCase("printer")){
			return new PrinterFactory();
		}		
		return null;	
	}
}

6.创建测试类,测试程序

package com.test;

public class Main {
	public static void main(String args[]) {
		//用FactoryProducer获取shapeFactory
		AbstractFactory shapeFactory =FactoryProducer.getFactory("shape");
		
		//用shapeFactory获取circle  rectangle  square 
		IShape circle = shapeFactory.getShape("circle");
		circle.draw();
		IShape rectangle = shapeFactory.getShape("rectangle");
		rectangle.draw();
		IShape square = shapeFactory.getShape("square");
		square.draw();
		
		
		AbstractFactory printerFactory =FactoryProducer.getFactory("printer");
		
		IPrinter paper = printerFactory.getPrinter("paper");
		paper.print();
		IPrinter Web = printerFactory.getPrinter("Web");
		Web.print();
		IPrinter Screen = printerFactory.getPrinter("Screen");
		Screen.print();
		
	}
}

 

代码下载地址如下

https://download.csdn.net/download/thinkpet/10757850

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ThinkPet

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值