介绍
提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。
工厂方法模式和抽象工厂模式基本类似,可以这么理解:当工厂只生产一个产品的时候,即为工厂方法模式,而工厂如果生产两个或以上的商品即变为抽象工厂模式。
UML
总结
抽象工厂模式的缺点在于产品类的扩展,将会是十分费力的,假如在需要加入新的产品,那么几乎所有的工厂类都需要进行修改,所以在使用抽象工厂模式时,对产品等级结构的划分是十分重要的。
工厂模式横向扩展很方便,假如该工厂又有新的产品要生产,那么只需要创建相应的工厂类和产品类去实现抽象工厂接口和抽象产品接口即可,而不用去修改原有已经存在的代码。
无论是简单工厂模式,工厂方法模式,还是抽象工厂模式,他们都属于工厂模式,在形式和特点上也是极为相似的,他们的最终目的都是为了解耦。在使用时,我们不必去在意这个模式到底工厂方法模式还是抽象工厂模式,因为他们之间的演变常常是令人琢磨不透的。经常你会发现,明明使用的工厂方法模式,当新需求来临,稍加修改,加入了一个新方法后,由于类中的产品构成了不同等级结构中的产品族,它就变成抽象工厂模式了;而对于抽象工厂模式,当减少一个方法使的提供的产品不再构成产品族之后,它就演变成了工厂方法模式。
所以,在使用工厂模式时,只需要关心降低耦合度的目的是否达到了。使用工厂方法后,调用端的耦合度大大降低了。并且对于工厂来说,是可以扩展的,以后如果想组装其他的产品,只需要再增加一个工厂类的实现就可以。无论是灵活性还是稳定性都得到了极大的提高。
实例
package HomeWork3;
public interface Shape {
public void printShape();
}
package HomeWork3;
public interface Shape {
public void printShape();
}
package HomeWork3;
public class Round implements Shape{
public void printShape(){
System.out.print("this is Round");
}
}
package HomeWork3;
public interface ShapeFactory {
public Shape createShape();
public Heart createHeart(String s);
}
package HomeWork3;
public class RectangleFactory implements ShapeFactory{
public Shape createShape(){
return new Rectangle();
}
public Heart createHeart(String s){
if(s.equals("Solid")) return new Solid();
else return new Hollow();
}
}
package HomeWork3;
public class RoundFactory implements ShapeFactory{
public Shape createShape(){
return new Round();
}
public Heart createHeart(String s){
if(s.equals("Solid")) return new Solid();
else return new Hollow();
}
}
package HomeWork3;
public class TriangleFactory implements ShapeFactory{
public Shape createShape(){
return new Triangle();
}
public Heart createHeart(String s){
if(s.equals("Solid")) return new Solid();
else return new Hollow();
}
}
package HomeWork3;
public interface Heart {
public void printHeart();
}
package HomeWork3;
public class Hollow implements Heart{
public void printHeart(){
System.out.print("the shape is Hollow");
}
}
package HomeWork3;
public class Solid implements Heart{
public void printHeart(){
System.out.print("the shape is Solid");
}
}
package HomeWork3;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
enum ape{
Triangle,Rectangle,Round,Solid,Hollow;
public static ape getshape(String ss){
return valueOf(ss);
}
}
public class Client {
public void draw(Shape s){
System.out.println("I draw a Shape");
s.printShape();
}
public void draw_heart(Heart h){
h.printHeart();
}
public void showlist(List<Shape> shape,List<Heart> heart){
System.out.println();
System.out.println("现在画板里有:");
Shape t;
Heart h;
for(int i=0;i<shape.size();i++){
t=shape.get(i);
h=heart.get(i);
System.out.print("<");
t.printShape();
System.out.print(",");
h.printHeart();
System.out.print(">");
if((i+1)%2==0) System.out.println();
}
System.out.println();
}
public void printMenu(){
System.out.println("1:添加图形");
System.out.println("2:删除图形");
System.out.println("3:退出");
}
public List<Shape> delete(List<Shape> shape,List<Heart> heart){
shape.remove(shape.size()-1);
heart.remove(heart.size()-1);
return shape;
}
public void operate(){
//ShapeFactory sf=new ShapeFactory();
List<Shape> shape=new ArrayList<>();
List<Heart> heart=new ArrayList<>();
Scanner scanner=new Scanner(System.in);
while(true)
{ //System.out.println("----------------------------------------------");
System.out.println("请选择你需要的功能:");
printMenu();
ShapeFactory sf=null;
int id=scanner.nextInt();
switch(id){
case 1: System.out.println("请输入你要画的图形:");
String type=scanner.next();
System.out.println("请输入图形是Solid还是Hollow:");
String Heart_type=scanner.next();
switch(ape.getshape(type)){
case Triangle:sf=new TriangleFactory();break;
case Rectangle:sf=new RectangleFactory();break;
case Round:sf=new RoundFactory();break;
default:System.out.println("请拼写正确的图形!");
}
Shape s=sf.createShape();
Heart h=null;
switch(ape.getshape(Heart_type)){
case Solid:h=sf.createHeart(Heart_type);break;
case Hollow:h=sf.createHeart(Heart_type);break;
default:System.out.println("拼写错误!");
}
draw(s);
draw_heart(h);
shape.add(s);
heart.add(h);
showlist(shape,heart);
break;
case 2:System.out.print("删除上一次所画的图形:");
shape=delete(shape,heart);
showlist(shape,heart);
break;
case 3:return;
default:System.out.println("请输入正确的服务号!");
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Client c=new Client();
c.operate();
}
}