什么是工厂模式
public interface Const {?? ?public static final int SHAPE_CIRCLE =1;?? ?public static final int SHAPE_SQUARE =2;?? ?public static final int SHAPE_HEXAGON =3;?}??//一个抽象的产品工厂public class ShapeFactory {?? ? public abstract Shape getShape(int shapeId);?}??//产品工厂的具体实现public class SimpleShapeFactory extends ?? ? ? ? ? ?ShapeFactory ?throws BadShapeException ?{?? ? ?public Shape?getShape(int shapeTypeId){?? ? ? ? ?//通过传入不同的参数,来判断返回的是什么子对象? ? ? ? ?Shape shape = null;?? ? ? ? ?if(shapeTypeId == Const.SHAPE_CIRCLE) {?? ? ? ? ? ? ? ? //in future can reuse or cache objects. ?? ? ? ? ? ? ? ? shape = new Circle();?? ? ? ? ?}?? ? ? ? ?else if(shapeTypeId == Const.SHAPE_SQUARE) {?? ? ? ? ? ? ? ?//in future can reuse or cache objects ?? ? ? ? ? ? ? ? shape = new Square();?? ? ? ? ?}?? ? ? ? ?else throw new BadShapeException?? ? ? ? ? ? ? ? ? ? ? ?(“ShapeTypeId=”+ shapeTypeId); ??? ? ? ? ??? ? ? ? return shape;?? ? ?}?}?调用方式:
?
ShapeFactory factory = new SimpleShapeFactory();??//returns a ?Shape but whether it is a ?Circle or a?//Square is not known to the caller.?Shape s = ?factory.getShape(1);?s.draw(); // circle is drawn ??//returns a ?Shape but whether it is a ?Circle or a?//Square is not known to the caller.?s = factory.getShape(2);?s.draw(); //Square is drawn?抽象工厂模式:他与工厂方法不同的是他返回的是工厂类,这个工厂类才是最后返回子产品的类。?代码页很简单:假设我们有另一套产品的工厂类:public class ComplexShapeFactory extends ?ShapeFactory {?? ? ? ? ? ? ? ? ? ? ? ? ?throws BadShapeException ?{?? ? public Shape getShape(int shapeTypeId){?? ? ? ? Shape shape = null;?? ? ? ? if(shapeTypeId == Const.SHAPE_HEXAGON) {?? ? ? ? ? ? ? ? shape = new Hexagon();//complex shape?? ? ? ? ?}?? ? ? ? ?else throw new BadShapeException?? ? ? ? ? ? ? ? ? ? ? ? ? (“shapeTypeId=” + ?shapeTypeId); ??? ? ? ? ? return ?shape;?? ? ?}?}??然后我们提供一个抽象工厂:public class ShapeFactoryType ?? ? ? ? ? ? ? ?throws ? BadShapeFactoryException {??? ? ?public static final int TYPE_SIMPLE = 1;?? ? ?public static final int TYPE_COMPLEX = 2;??? ? ?public ShapeFactory getShapeFactory(int type) {?? ? ? ? ??? ? ? ? ?ShapeFactory sf = null; ? ? ? ? ??? ? ? ? ?if(type == TYPE_SIMPLE) {?? ? ? ? ? ? ?sf = new SimpleShapeFactory();?? ? ? ? ?}?? ? ? ? ?else if (type == TYPE_COMPLEX) {?? ? ? ? ? ? ?sf = new ComplexShapeFactory();?? ? ? ? ?}?? ? ? ? ?else throw new BadShapeFactoryException(“No factory!!”);??? ? ? ? return sf;?? ? ?}?}?这样,就能实现根据不同的参数,返回不同的工厂,而到底返回了什么工厂,对用户是隐藏的,因为他们只关心的是最后得到的产品。ShapeFactoryType abFac = new ?ShapeFactoryType();?ShapeFactory factory = null;?Shape s = null;??//returns a ShapeFactory but whether it is a?//SimpleShapeFactory or a ComplexShapeFactory is not?//known to the caller.??factory = abFac.getShapeFactory(1);//returns SimpleShapeFactory?//returns a Shape but whether it is a Circle or a Pentagon is?//not known to the caller.??s = factory.getShape(2); //returns square.?s.draw(); //draws a square??//returns a ShapeFactory but whether it is a?//SimpleShapeFactory or a ComplexShapeFactory is not?//known to the caller.??factory = abFac.getShapeFactory(2); ?//returns a Shape but whether it is a Circle or a Pentagon is?//not known to the caller.?s = factory.getShape(3); //returns a pentagon.?s.draw(); //draws a pentagon??为什么我们要使用抽象工厂模式或者工厂模式呢??工厂模式返回一个类众多子类中的一个实例。,但是调用方无需知道具体的实现类。也就是说:工厂模式减少调用方代码与被调用对象的耦合度或者依赖度。 比如你想要一个苹果汁,但是你不需要知道他是如何制造的,你只要一个卖果汁的工厂,告诉他想要什么,你就能得到你想要的。不用着急去创造。这也意味着另一个好处,工厂类中,可以对这些对象进行缓存之类的处理。?工厂模式与单例模式的结合使用工厂模式如果不使用单例模式,每次想要或者对象,都需要先new Factory(),才能得到产品。而使用了单例模式后,你可以避免每次都创建一个工厂, Factory fac = Factory.getInstance();????????