简单工厂模式
简单工厂模式(Simple Factory Pattern)又叫静态工厂方法模式(Static FactoryMethod Pattern)
:由一个工厂类根据传入的参数动态决定应该创建哪一种产品(产品类)。
UML图:
工厂角色(Factory)-->SimpleFactory:创建所需要的产品对象,被客户端调用。
抽象(abstract Product)-->Ball 产品角色:所有具体产品的父类,父类可以是接口也可以是抽象类,产品的公共接口。
具体产品(Concrete Product)----> FootBall & BaketBall角色:简单工厂所创建的具体产品对象。
实例:Ball简单工厂
抽象(abstract Product)产品角色:public abstract class Ball
package com.maggie.simplefactory; public abstract class Ball { private String color; public Ball(String color) { super (); this .color = color; } public Ball() { super (); } public String getColor() { return color; } public void setColor(String color) { this .color = color; } public abstract void play(); }
具体产品(Concrete Product)角色:从public abstract class Balll继承,重写方法,也可添加自己的方法。
FootBall
package com.maggie.simplefactory; public class FootBall extends Ball { @Override public void play() { String player = "the ball's color is "+ this .getColor()+",I am FootBall" ; System.out.println(player); } public FootBall() { super (); // TODO Auto-generated constructor stub } public FootBall(String color) { super (color); // TODO Auto-generated constructor stub } }
BasketBall
package com.maggie.simplefactory; public class BasketBall extends Ball { @Override public void play() { String player = "the ball's color is "+ this .getColor()+",I am BasketBall" ; System.out.println(player); } public BasketBall() { super (); // TODO Auto-generated constructor stub } public BasketBall(String color) { super (color); // TODO Auto-generated constructor stub } }
工厂角色(SimpleFactory):球生产厂类,静态方法,接收main传入的图形名称,找到相应的具体产品的class,实例化。
这里使用反射实现
package com.maggie.simplefactory; import java.lang.reflect.InvocationTargetException; public class SimpleFactory { public static Ball createBall(Class c) { Ball ball = null ; /* * ball = (Ball)Class.forName(c.getName()).newInstance(); 无参数构造函数的实例 */ try { // 有参数构造函数的实例 if (c.getName().contains("FootBall" )) { ball = (Ball) c.getConstructor(String. class ).newInstance("white+black" ); } else if (c.getName().contains("BasketBall" )){ ball = (Ball) c.getConstructor(String. class ).newInstance("orange" ); } } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchMethodException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } return ball; } }
客户端调用
package com.maggie.simplefactory; public class Client { public static void main(String[] args) { Ball ball = SimpleFactory.createBall(FootBall. class ); ball.play(); Ball ball2 = SimpleFactory.createBall(BasketBall. class ); ball2.play(); } }
输出的结果:
the ball's color is white+black,I am FootBall the ball's color is orange,I am BasketBall
简单工厂模式使得客户端的代码变得简洁,不用去关心具体是怎么实现的
但是工厂类里面包含了所有的逻辑,如果需要添加新的产品,则就需要改变工厂类的各种逻辑,这就是简单工厂不好的地方。
因此简单工厂模式违背了开放-封闭原则的缺点