����ģʽ

����ģʽ��Factory Pattern���� Java ����õ����ģʽ֮һ���������͵����ģʽ���ڴ�����ģʽ�����ṩ��һ�ִ����������ѷ�ʽ��

�ڹ���ģʽ�У������ڴ�������ʱ����Կͻ��˱�¶�����߼���������ͨ��ʹ��һ����ͬ�Ľӿ���ָ���´����Ķ���

����

��ͼ������һ����������Ľӿڣ����������Լ�����ʵ������һ�������࣬����ģʽʹ�䴴�������ӳٵ�������С�

��Ҫ�������Ҫ����ӿ�ѡ������⡣

��ʱʹ�ã�������ȷ�ؼƻ���ͬ�����´�����ͬʵ��ʱ��

��ν������������ʵ�ֹ����ӿڣ����ص�Ҳ��һ������IJ�Ʒ��

�ؼ����룺����������������ִ�С�

Ӧ��ʵ���� 1������Ҫһ������������ֱ�Ӵӹ������������������ȥ��������������ô�������ģ��Լ������������ľ���ʵ�֡� 2��Hibernate �����ݿ�ֻ�軻���Ժ������Ϳ��ԡ�

�ŵ㣺

  • 1��һ���������봴��һ������ֻҪ֪�������ƾͿ����ˡ� 2����չ�Ըߣ����������һ����Ʒ��ֻҪ��չһ��������Ϳ��ԡ� 3�����β�Ʒ�ľ���ʵ�֣�������ֻ���IJ�Ʒ�Ľӿڡ�

ȱ�㣺ÿ������һ����Ʒʱ������Ҫ����һ��������Ͷ���ʵ�ֹ�����ʹ��ϵͳ����ĸ����ɱ����ӣ���һ���̶���������ϵͳ�ĸ��Ӷȣ�ͬʱҲ������ϵͳ��������������Ⲣ����ʲô���¡�

ʹ�ó�����

  • 1����־��¼������¼���ܼ�¼������Ӳ�̡�ϵͳ�¼���Զ�̷������ȣ��û�����ѡ���¼��־��ʲô�ط���
  • 2�����ݿ���ʣ����û���֪�����ϵͳ������һ�����ݿ⣬�Լ����ݿ�����б仯ʱ��
  • 3�����һ�����ӷ������Ŀ�ܣ���Ҫ����Э�飬”POP3“��”IMAP“��”HTTP“�����԰���������Ϊ��Ʒ�࣬��ͬʵ��һ���ӿڡ�

ע�������Ϊһ�ִ�����ģʽ�����κ���Ҫ���ɸ��Ӷ���ĵط���������ʹ�ù�������ģʽ����һ����Ҫע��ĵط����Ǹ��Ӷ����ʺ�ʹ�ù���ģʽ�����򵥶����ر���ֻ��Ҫͨ�� new �Ϳ�����ɴ����Ķ�������ʹ�ù���ģʽ�����ʹ�ù���ģʽ������Ҫ����һ�������࣬������ϵͳ�ĸ��Ӷȡ�

ʵ��

���ǽ�����һ�� Shape �ӿں�ʵ�� Shape �ӿڵ�ʵ���ࡣ��һ���Ƕ��幤���� ShapeFactory��

FactoryPatternDemo ��ʹ�� ShapeFactory ����ȡ Shape ���������� ShapeFactory ������Ϣ��CIRCLE / RECTANGLE / SQUARE�����Ա��ȡ�������������͡�

01工厂模式 - 图1

���� 1

����һ���ӿ�:

  1. public interface Shape {
  2. void draw();
  3. }

���� 2

����ʵ�ֽӿڵ�ʵ���ࡣ

  1. public class Rectangle implements Shape {
  2. @Override
  3. public void draw() {
  4. System.out.println("Inside Rectangle::draw() method.");
  5. }
  6. }
  1. public class Square implements Shape {
  2. @Override
  3. public void draw() {
  4. System.out.println("Inside Square::draw() method.");
  5. }
  6. }
  1. public class Circle implements Shape {
  2. @Override
  3. public void draw() {
  4. System.out.println("Inside Circle::draw() method.");
  5. }
  6. }

���� 3

����һ�����������ɻ��ڸ�����Ϣ��ʵ����Ķ���

  1. public class ShapeFactory {
  2. //ʹ�� getShape ������ȡ��״���͵Ķ���
  3. public Shape getShape(String shapeType){
  4. if(shapeType == null){
  5. return null;
  6. }
  7. if(shapeType.equalsIgnoreCase("CIRCLE")){
  8. return new Circle();
  9. } else if(shapeType.equalsIgnoreCase("RECTANGLE")){
  10. return new Rectangle();
  11. } else if(shapeType.equalsIgnoreCase("SQUARE")){
  12. return new Square();
  13. }
  14. return null;
  15. }
  16. }

���� 4

ʹ�øù�����ͨ������������Ϣ����ȡʵ����Ķ���

  1. public class FactoryPatternDemo {
  2. public static void main(String[] args) {
  3. ShapeFactory shapeFactory = new ShapeFactory();
  4. //��ȡ Circle �Ķ��󣬲��������� draw ����
  5. Shape shape1 = shapeFactory.getShape("CIRCLE");
  6. //���� Circle �� draw ����
  7. shape1.draw();
  8. //��ȡ Rectangle �Ķ��󣬲��������� draw ����
  9. Shape shape2 = shapeFactory.getShape("RECTANGLE");
  10. //���� Rectangle �� draw ����
  11. shape2.draw();
  12. //��ȡ Square �Ķ��󣬲��������� draw ����
  13. Shape shape3 = shapeFactory.getShape("SQUARE");
  14. //���� Square �� draw ����
  15. shape3.draw();
  16. }
  17. }

���� 5

ִ�г�����������

  1. Inside Circle::draw() method.
  2. Inside Rectangle::draw() method.
  3. Inside Square::draw() method.