创建型模式-工厂模式

1.什么是工厂模式

    工厂模式提供了创建对象的最佳方式,在创建对象时不会对客户端暴露创建逻辑,并且通过一个共同的接口来指向创建的对象。

2.使用场景

    在不同的条件下创建不同的实例,比如Spring使用了工厂模式管理Bean的创建、生命周期以及Bean与Bean之间的关系。

3.如何实现

    定义一个创建对象的接口,让其子类自己决定实例化哪一个工厂类,工厂模式使其创建过程延迟到子类进行。

4.优点

  • 一个调用者想创建一个对象,只要知道其名称就可以了。
  • 扩展性高,如果想增加一个产品,只要扩展一个工厂类就可以。 
  • 屏蔽产品的具体实现,调用者只关心产品的接口。

5.缺点

   每次增加一个产品时,都需要增加一个具体类和对象实现工厂,使得系统中类的个数成倍增加,在一定程度上增加了系统的复杂度,同时也增加了系统具体类的依赖。

6.应用示例以及UML

   工厂模式 有三类:

  • 普通工厂模式
  • 多个工厂方法模式
  • 静态工厂方法模式

    代码示例:

        创建多种几何图形,圆形、长方形、正方形

      UML视图:

        

7.代码实现

   

7.1 接口 Shape

public interface Shape {
	
	/**
	 * 绘画
	 */
	void draw();

}

7.2 正方形Square

public class Square implements Shape{

	@Override
	public void draw() {
		System.out.println("this is Square drawing");
	}

}

7.3 长方形Rectangle

public class Rectangle implements Shape{

	@Override
	public void draw() {
		System.out.println("this is Rectangle drawing ");
	}

}

7.4圆形Circle

public class Circle implements Shape{

	@Override
	public void draw() {
		System.out.println("this is Circle drawing");
	}

}

7.5普通工厂方法OrdinaryShapeFactory

public class OrdinaryShapeFactory {
	
	public Shape getShape(String shapeType){
		if("CIRCLE".equalsIgnoreCase(shapeType)){
			return new Circle();
		}else if("RECTANGLE".equalsIgnoreCase(shapeType)){
			return new Rectangle();
		}else if("SQUARE".equalsIgnoreCase(shapeType)){
			return new Square();
		}else{
			return null;
		}
	}

}

7.6 多个工厂方法MethodFactory

public class MethodFactory {
	
	/**
	 * 创建圆形
	 * @return
	 */
	public Shape getCircleShape(){
		return new Circle();
	}
	
	/**
	 * 创建长方形
	 * @return
	 */
	public Shape getRectangleShape(){
		return new Rectangle();
	}
	
	/**
	 * 创建正方形
	 * @return
	 */
	public Shape getSquareShape(){
		return new Square();
	}
	
}

7.7静态工厂方法StaticMethodFactory

public class StaticMethodFactory {
	
	/**
	 * 创建圆形
	 * @return
	 */
	public static Shape getCircleShape(){
		return new Circle();
	}
	
	/**
	 * 创建长方形
	 * @return
	 */
	public static Shape getRectangleShape(){
		return new Rectangle();
	}
	
	/**
	 * 创建正方形
	 * @return
	 */
	public static Shape getSquareShape(){
		return new Square();
	}
	
}
8.测试类


8.1 普通工厂测试类

public class OrdinaryShapeFactoryTest {
	
	@Test
	public void test(){
		OrdinaryShapeFactory ordinaryShapeFactory = new OrdinaryShapeFactory();
		
		Shape circle = ordinaryShapeFactory.getShape("CIRCLE");
		circle.draw();
		
		Shape rectangle = ordinaryShapeFactory.getShape("RECTANGLE");
		rectangle.draw();
		
		Shape square = ordinaryShapeFactory.getShape("SQUARE");
		square.draw();
	}

}

测试结果:

this is Circle drawing
this is Rectangle drawing 
this is Square drawing

8.2 多个工厂方法测试类

public class MethodFactoryTest {
	
	@Test
	public void test(){
		MethodFactory  methodFactory = new MethodFactory();
		
		Shape circle = methodFactory.getCircleShape();
		circle.draw();
		
		Shape rectangle = methodFactory.getRectangleShape();
		rectangle.draw();
		
		Shape square = methodFactory.getSquareShape();
		square.draw();
	}

}

测试结果:

this is Circle drawing
this is Rectangle drawing 
this is Square drawing

8.3 静态工厂测试类

public class StaticMethodFactoryTest {
	
	@Test
	public void test(){
		Shape circle = StaticMethodFactory.getCircleShape();
		circle.draw();
		
		Shape rectangle = StaticMethodFactory.getRectangleShape();
		rectangle.draw();
		
		Shape square = StaticMethodFactory.getSquareShape();
		square.draw();
	}

}

测试结果:

this is Circle drawing
this is Rectangle drawing 
this is Square drawing

猜你喜欢

转载自blog.csdn.net/dxh0823/article/details/80050427