设计模式之抽象工厂模式(三)
来源:互联网 发布:python 自定义函数 编辑:程序博客网 时间:2024/05/21 17:35
设计模式之抽象工厂模式(三)
抽象工厂模式(Abstract Factory Pattern)是围绕一个超级工厂创建其他工厂。该超级工厂又称为其他工厂的工厂。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。
在抽象工厂模式中,接口是负责创建一个相关对象的工厂,不需要显式指定它们的类。每个生成的工厂都能按照工厂模式提供对象。
抽象工厂的创建步骤:
1.为形状创建一个接口
2.创建实现形状接口的实体类
3.为颜色创建一个接口
4.创建实现颜色接口的实体类
5.为形状和颜色对象创建抽象类来获取工厂
6.创建扩展了抽象工厂的工厂类,基于给定的信息生成实体类的对象
7.创建一个工厂创造器/生成器类,通过传递形状或颜色信息来获取工厂
8.使用工厂创造器/生成器类来获取抽象工厂,通过传递类型信息来获取实体类的对象
步骤一:
public interface Shape { void draw();}
步骤二:
public class Rectangle implements Shape { @Override public void draw() { System.out.println("Inside Rectangle::draw() method."); }}
public class Square implements Shape { @Override public void draw() { System.out.println("Inside Square::draw() method."); }}
public class Circle implements Shape { @Override public void draw() { System.out.println("Inside Circle::draw() method."); }}
步骤三:
public interface Color { void fill();}
步骤四:
public class Red implements Color { @Override public void fill() { System.out.println("Inside Red::fill() method."); }}
public class Green implements Color { @Override public void fill() { System.out.println("Inside Green::fill() method."); }}
public class Blue implements Color { @Override public void fill() { System.out.println("Inside Blue::fill() method."); }}
步骤五:
public abstract class AbstractFactory { abstract Color getColor(String color); abstract Shape getShape(String shape) ;}
步骤六:
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("RECTANGLE")){ return new Rectangle(); } else if(shapeType.equalsIgnoreCase("SQUARE")){ return new Square(); } return null; } @Override Color getColor(String color) { return null; }}
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(); } else if(color.equalsIgnoreCase("BLUE")){ return new Blue(); } return null; }}
步骤七:
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; }}
步骤八:
public class AbstractFactoryPatternDemo { public static void main(String[] args) { //获取形状工厂 AbstractFactory shapeFactory = FactoryProducer.getFactory("SHAPE"); //获取形状为 Circle 的对象 Shape shape1 = shapeFactory.getShape("CIRCLE"); //调用 Circle 的 draw 方法 shape1.draw(); //获取形状为 Rectangle 的对象 Shape shape2 = shapeFactory.getShape("RECTANGLE"); //调用 Rectangle 的 draw 方法 shape2.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(); //获取颜色为 Blue 的对象 Color color3 = colorFactory.getColor("BLUE"); //调用 Blue 的 fill 方法 color3.fill(); }}
阅读全文
0 0
- 三 设计模式之抽象工厂模式
- 设计模式之三--抽象工厂模式
- 设计模式之三:抽象工厂模式
- 设计模式之三抽象工厂模式
- 设计模式之三:抽象工厂模式
- 设计模式之三:抽象工厂模式
- 设计模式之三:抽象工厂模式
- 设计模式之抽象工厂模式(三)
- 设计模式之工厂模式(三)之抽象工厂模式
- 设计模式培训之三:抽象工厂
- 设计模式培训之三:抽象工厂
- Java设计模式之三(抽象工厂模式)
- 设计模式之三 抽象工厂模式 Abstract Factory
- 设计模式系列之三:抽象工厂模式
- 设计模式(三)---抽象工厂模式
- 设计模式(三)--抽象工厂模式
- 设计模式(三)---抽象工厂模式
- 设计模式(三)抽象工厂模式
- FX3主机程序
- 初识halcon
- 基于单链表和环形队列的生产消费模型
- 鼠标事件获取鼠标坐标
- 素数筛——Eratosthenes 素数筛、Euler 素数筛
- 设计模式之抽象工厂模式(三)
- ios 自动换行flowLayout
- 二叉树的前,中,后遍历(非递归)
- struts2请求无法跳转到后台执行acton方法
- 支付宝接口程序、文档及解读(ASP.NET)
- 红外接收电路自己制作,我用过的
- 电脑的并口针脚定义
- OnCreate函数
- PADS快捷键定义