抽象工厂模式 ( Abstract Factory Pattern )(2)

抽象工厂模式 ( Abstract Factory Pattern )

在抽象工厂模式中,接口是负责创建一个相关对象的工厂,不需要显式指定它们的类

每个生成的工厂都能按照工厂模式提供对象

抽象工厂模式属于创建型模式,它提供了一种创建对象的最佳方式。

白话解释:工厂里面生产工厂------工厂里面的产品有多类,每一类产品又包含多样产品

实例:
创建一个形状接口

package testDesignPatterns;

public interface Shape {
	   void draw();
	}

package testDesignPatterns;

public class Square implements Shape {

	   @Override
	   public void draw() {
	      System.out.println("画出一个正方形");
	   }
	}

package testDesignPatterns;

public class Circle implements Shape {

	   @Override
	   public void draw() {
	      System.out.println("画出一个圆圈");
	   }
	}

package testDesignPatterns;

public interface Color {
	   void fill();
	}

package testDesignPatterns;

public class Red implements Color {

	   @Override
	   public void fill() {
	      System.out.println("红颜色啦");
	   }
	}

package testDesignPatterns;

public class Green implements Color {

	   @Override
	   public void fill() {
	      System.out.println("绿颜色啦");
	   }
	}

package testDesignPatterns;

public class ShapeFactory extends AbstractFactory {

	   @Override
	   public Shape getShape(String shapeType){
	      if(shapeType == null){
	         return null;
	      }     
	      if(shapeType.equalsIgnoreCase("CIRCLE")){
	         return new Circle();
	      } 
	      else if(shapeType.equalsIgnoreCase("SQUARE")){
	         return new Square();
	      }
	      return null;
	   }

	   @Override
	   Color getColor(String color) {
	      return null;
	   }
	}


package testDesignPatterns;

public class ColorFactory extends AbstractFactory {

	   @Override
	   public Shape getShape(String shapeType){
	      return null;
	   }

	   @Override
	   Color getColor(String color) {
	      if(color == null){
	         return null;
	      }     
	      if(color.equalsIgnoreCase("RED")){
	         return new Red();
	      } else if(color.equalsIgnoreCase("GREEN")){
	         return new Green();
	      } 
	      return null;
	   }
	}

package testDesignPatterns;

public class FactoryProducer {
	   public static AbstractFactory getFactory(String choice){
	      if(choice.equalsIgnoreCase("SHAPE")){
	         return new ShapeFactory();
	      } else if(choice.equalsIgnoreCase("COLOR")){
	         return new ColorFactory();
	      }
	      return null;
	   }
	}
package testDesignPatterns;

public class TestAbstractFactoryPattern {
	   public static void main(String[] args) {

	      //获取形状工厂
	      AbstractFactory shapeFactory = FactoryProducer.getFactory("SHAPE");

	      //获取形状为 Circle 的对象
	      Shape shape1 = shapeFactory.getShape("CIRCLE");

	      //调用 Circle 的 draw 方法
	      shape1.draw();


	      //获取形状为 Square 的对象
	      Shape shape3 = shapeFactory.getShape("SQUARE");

	      //调用 Square 的 draw 方法
	      shape3.draw();

	      //获取颜色工厂
	      AbstractFactory colorFactory = FactoryProducer.getFactory("COLOR");

	      //获取颜色为 Red 的对象
	      Color color1 = colorFactory.getColor("RED");

	      //调用 Red 的 fill 方法
	      color1.fill();

	      //获取颜色为 Green 的对象
	      Color color2 = colorFactory.getColor("Green");

	      //调用 Green 的 fill 方法
	      color2.fill();

	      
	   }
	}

在这里插入图片描述
说实话感觉这么写好麻烦,不过看起来很有代码感觉。。。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值