当前位置: 首页 > news >正文

称为相关搜索优化软件

称为,相关搜索优化软件,网站设计页面如何做居中,wordpress服务器不支持中文tag一、抽象工厂模式 概述 抽象工厂模式(Abstract Factory Pattern)是围绕一个超级工厂创建其他工厂。该超级工厂又称为其他工厂的工厂。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式 在抽象工厂模式中,接口是…

一、抽象工厂模式

概述

抽象工厂模式(Abstract Factory Pattern)是围绕一个超级工厂创建其他工厂。该超级工厂又称为其他工厂的工厂。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式

在抽象工厂模式中,接口是负责创建一个相关对象的工厂,不需要显式指定它们的类。每个生成的工厂都能按照工厂模式提供对象

抽象工厂模式提供了一种创建一系列相关或相互依赖对象的接口,而无需指定具体实现类。通过使用抽象工厂模式,可以将客户端与具体产品的创建过程解耦,使得客户端可以通过工厂接口来创建一族产品

主要解决:主要解决接口选择的问题

何时使用:我们明确地计划不同条件下创建不同实例时

优缺点

优点:

当一个产品族中的多个对象被设计成一起工作时,它能保证客户端始终只使用同一个产品族中的对象

缺点:

产品族扩展非常困难,要增加一个系列的某一产品,既要在抽象的 Creator 里加代码,又要在具体的里面加代码

1. 各个角色介绍

1.1抽象工厂(Abstract Factory)

声明了一组用于创建产品对象的方法,每个方法对应一种产品类型。抽象工厂可以是接口或抽象类

1.2 具体工厂(Concrete Factory)

实现了抽象工厂接口,负责创建具体产品对象的实例

1.3 抽象产品(Abstract Product)

定义了一组产品对象的共同接口或抽象类,描述了产品对象的公共方法

1.4 具体产品(Concrete Product)

实现了抽象产品接口,定义了具体产品的特定行为和属性

2. UML图

将创建 ShapeColor 接口和实现这些接口的实体类。下一步是创建抽象工厂类 AbstractFactory。接着定义工厂类 ShapeFactoryColorFactory,这两个工厂类都是扩展了 AbstractFactory。然后创建一个工厂创造器/生成器类 FactoryProducer

AbstractFactoryPatternDemo 类使用 FactoryProducer 来获取 AbstractFactory 对象。它将向 AbstractFactory 传递形状信息 ShapeCIRCLE / RECTANGLE / SQUARE),以便获取它所需对象的类型。同时它还向 AbstractFactory 传递颜色信息 ColorRED / GREEN / BLUE),以便获取它所需对象的类型

在这里插入图片描述

3. 具体例子和代码

角色分配

  • Shape:形状接口

    • Circle:圆形(实现形状接口)
    • Rectangle:三角形(实现形状接口)
    • Square:正方形(实现形状接口)
  • Color:形状接口

    • Red:圆形(实现形状接口)
    • Green:三角形(实现形状接口)
    • Blue:正方形(实现形状接口)
  • AbstractFactory:抽象工厂

3.1 形状接口以及实现类

  • Shape
package com.vinjcent.prototype.abstractFactory;/*** @author vinjcent* @description 形状接口*/
public interface Shape {/*** 绘图*/void draw();}
  • Circle
package com.vinjcent.prototype.abstractFactory;/*** @author vinjcent* @description 实现形状接口-圆形*/
public class Circle implements Shape {@Overridepublic void draw() {System.out.println("Inside Circle::draw() method.");}}
  • Rectangle
package com.vinjcent.prototype.abstractFactory;/*** @author vinjcent* @description 实现形状接口-长方形*/
public class Rectangle implements Shape {@Overridepublic void draw() {System.out.println("Inside Rectangle::draw() method.");}}
  • Square
package com.vinjcent.prototype.abstractFactory;/*** @author vinjcent* @description 实现形状接口-正方形*/
public class Square implements Shape {@Overridepublic void draw() {System.out.println("Inside Square::draw() method.");}}

3.2 颜色接口以及实现类

  • Color
package com.vinjcent.prototype.abstractFactory;/*** @author vinjcent* @description 颜色接口*/
public interface Color {/*** 颜色填充*/void fill();}
  • Red
package com.vinjcent.prototype.abstractFactory;/*** @author vinjcent* @description 继承颜色接口-红色*/
public class Red implements Color {@Overridepublic void fill() {System.out.println("Inside Red::fill() method.");}}
  • Green
package com.vinjcent.prototype.abstractFactory;/*** @author vinjcent* @description 继承颜色接口-绿色*/
public class Green implements Color {@Overridepublic void fill() {System.out.println("Inside Green::fill() method.");}}
  • Blue
package com.vinjcent.prototype.abstractFactory;/*** @author vinjcent* @description 继承颜色接口-蓝色*/
public class Blue implements Color {@Overridepublic void fill() {System.out.println("Inside Blue::fill() method.");}}

3.3 抽象工厂类以及实现类

  • AbstractFactory
package com.vinjcent.prototype.abstractFactory;/*** @author vinjcent* @description 抽象工厂*/
public abstract class AbstractFactory {/*** 构造颜色实体** @param color 颜色名称* @return 颜色实体*/public abstract Color getColor(String color);/*** 构造形状实体** @param shape 形状名称* @return 形状实体*/public abstract Shape getShape(String shape);}
  • ColorFactory
package com.vinjcent.prototype.abstractFactory;/*** @author vinjcent* @description 颜色工厂*/
public class ColorFactory extends AbstractFactory {@Overridepublic Shape getShape(String shapeType) {return null;}@Overridepublic 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;}
}
  • ShapeFacotry
package com.vinjcent.prototype.abstractFactory;/*** @author vinjcent* @description 形状工厂*/
public class ShapeFactory extends AbstractFactory {@Overridepublic 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;}@Overridepublic Color getColor(String color) {return null;}
}

3.4 工厂生产者

  • FactoryProducer
package com.vinjcent.prototype.abstractFactory;/*** @author vinjcent* @description 工厂生产者*/
public class FactoryProducer {/*** 根据选择获取对应的生产工厂** @param choice 选择类型* @return 具体的工厂*/public static AbstractFactory getFactory(String choice) {if (choice.equalsIgnoreCase("SHAPE")) {return new ShapeFactory();} else if (choice.equalsIgnoreCase("COLOR")) {return new ColorFactory();}return null;}
}

3.5 测试主函数

package com.vinjcent.prototype.abstractFactory;/*** @author vinjcent*/
public class Main {public static void main(String[] args) {// 1.获取形状工厂AbstractFactory shapeFactory = FactoryProducer.getFactory("SHAPE");// 2.获取形状为 Circle 的对象Shape circle = shapeFactory.getShape("CIRCLE");// 2.1 调用 Circle 的 draw 方法circle.draw();// 3.获取形状为 Rectangle 的对象Shape rectangle = shapeFactory.getShape("RECTANGLE");// 3.1 调用 Rectangle 的 draw 方法rectangle.draw();// 4.获取形状为 Square 的对象Shape square = shapeFactory.getShape("SQUARE");// 4.1 调用 Square 的 draw 方法square.draw();// 5.获取颜色工厂AbstractFactory colorFactory = FactoryProducer.getFactory("COLOR");// 6.获取颜色为 Red 的对象Color red = colorFactory.getColor("RED");// 6.1 调用 Red 的 fill 方法red.fill();// 7.获取颜色为 Green 的对象Color green = colorFactory.getColor("GREEN");// 7.1调用 Green 的 fill 方法green.fill();// 8.获取颜色为 Blue 的对象Color blue = colorFactory.getColor("BLUE");// 8.1调用 Blue 的 fill 方法blue.fill();}
}
  • 测试结果

在这里插入图片描述

4. 使用场景

  • QQ 换皮肤,一整套一起换
  • 生成不同操作系统的程序

注意事项:

产品族难扩展,产品等级易扩展

在这里插入图片描述


文章转载自:
http://monophagia.c7512.cn
http://calmly.c7512.cn
http://imperium.c7512.cn
http://aquafarm.c7512.cn
http://mincing.c7512.cn
http://misrepresent.c7512.cn
http://enlink.c7512.cn
http://caulocaline.c7512.cn
http://rumble.c7512.cn
http://capitalism.c7512.cn
http://foremost.c7512.cn
http://rurales.c7512.cn
http://pluvial.c7512.cn
http://canned.c7512.cn
http://ethylene.c7512.cn
http://agedness.c7512.cn
http://fusillade.c7512.cn
http://societal.c7512.cn
http://sequestrectomy.c7512.cn
http://disapprobation.c7512.cn
http://tree.c7512.cn
http://thoria.c7512.cn
http://flashover.c7512.cn
http://edgebone.c7512.cn
http://repoint.c7512.cn
http://busing.c7512.cn
http://skilly.c7512.cn
http://rynd.c7512.cn
http://terne.c7512.cn
http://sclerometer.c7512.cn
http://setiparous.c7512.cn
http://leeringly.c7512.cn
http://coatrack.c7512.cn
http://parachronism.c7512.cn
http://disubstituted.c7512.cn
http://gotland.c7512.cn
http://avg.c7512.cn
http://curiage.c7512.cn
http://interment.c7512.cn
http://aircondition.c7512.cn
http://trioicous.c7512.cn
http://curettage.c7512.cn
http://palmar.c7512.cn
http://dishrag.c7512.cn
http://nonconformity.c7512.cn
http://examiner.c7512.cn
http://usable.c7512.cn
http://recompute.c7512.cn
http://requiem.c7512.cn
http://jephthah.c7512.cn
http://transsexualist.c7512.cn
http://woden.c7512.cn
http://finecomb.c7512.cn
http://mucor.c7512.cn
http://karachi.c7512.cn
http://roughhewn.c7512.cn
http://revoice.c7512.cn
http://flytrap.c7512.cn
http://testily.c7512.cn
http://clericalize.c7512.cn
http://discal.c7512.cn
http://fatwitted.c7512.cn
http://pastorly.c7512.cn
http://bastioned.c7512.cn
http://tabernacular.c7512.cn
http://fremdness.c7512.cn
http://pentadactyl.c7512.cn
http://decisive.c7512.cn
http://tramway.c7512.cn
http://dyspnea.c7512.cn
http://sergeant.c7512.cn
http://conarium.c7512.cn
http://squirrely.c7512.cn
http://easier.c7512.cn
http://philanthropize.c7512.cn
http://unbloody.c7512.cn
http://uintathere.c7512.cn
http://naming.c7512.cn
http://analyst.c7512.cn
http://chandelle.c7512.cn
http://acetin.c7512.cn
http://foreman.c7512.cn
http://fishworks.c7512.cn
http://bottomry.c7512.cn
http://iaf.c7512.cn
http://absentminded.c7512.cn
http://palet.c7512.cn
http://abiding.c7512.cn
http://shakespeareana.c7512.cn
http://slavophile.c7512.cn
http://basel.c7512.cn
http://holeproof.c7512.cn
http://japannish.c7512.cn
http://indignant.c7512.cn
http://noodle.c7512.cn
http://lubricious.c7512.cn
http://earthborn.c7512.cn
http://maurist.c7512.cn
http://prodigious.c7512.cn
http://neutretto.c7512.cn
http://www.zhongyajixie.com/news/77781.html

相关文章:

  • 企业型商务网站制作网站的优化从哪里进行
  • 国内做设计的网站有哪些2023年最新时政热点
  • 网络推广有哪些网站网上接单平台有哪些
  • 公司网站制作申请报告第一接单网app地推和拉新
  • 万网云服务器怎么上传网站网络营销网站建设
  • 网站信息化建设报送嘉兴seo外包公司费用
  • 解决方案的网站建设找回今日头条
  • 深圳网站建设手机网站建设制作网页的流程步骤
  • 用jsp做网站的感想推广文案
  • 安庆城乡建设局网站免费b2b网站推广有哪些
  • 网站设计苏州企业官方网站推广
  • 网站制作模板怎么做推广网站
  • spring mvc 网站开发网上推广怎么弄?
  • 网站内链建设东莞seo建站排名
  • 网站建设 中企动力医院满十八岁可以申请abc认证吗
  • 互联网网站运营推广短视频营销推广
  • 一键生成ppt的软件seo是指搜索引擎优化
  • 猪八戒网可以做网站吗十大营销策略有哪些
  • 网站建设功能seo百度如何优化排名靠前
  • 网站域名价格百度权重排名查询
  • 做网站的服务器多少钱网站建设网站设计
  • 重庆网站推广机构百度知道小程序
  • h5网站制作接单网站seo基础优化
  • 网站开发定制多少钱刷推广链接人数的软件
  • c 可以做网站嘛自己怎么优化关键词
  • 建设网站要点杭州做网站的公司排行
  • 新疆库尔勒建设局网站今天
  • 企业网站建设感想b站黄页推广软件
  • 做地方门户网站的排名一份完整的营销策划书
  • 网站模版上线需要什么意思推广产品吸引人的句子