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

采集的网站怎么做收录什么软件可以发布广告信息

采集的网站怎么做收录,什么软件可以发布广告信息,微网站建设多少钱,wordpress增加目录1. 引言 大家好,又见面了!在上一篇文章中,我们通过Python示例介绍了简单工厂模式,今天,我们继续深入这个话题,用Java来实现简单工厂模式。 2. 什么是简单工厂模式 简单工厂模式(Simple Facto…

1. 引言

大家好,又见面了!在上一篇文章中,我们通过Python示例介绍了简单工厂模式,今天,我们继续深入这个话题,用Java来实现简单工厂模式。

2. 什么是简单工厂模式

简单工厂模式(Simple Factory Pattern)是一种创建型设计模式,它定义一个工厂类,根据传入的参数决定创建哪一种产品类的实例。简单来说,简单工厂模式就是把创建对象的任务交给一个专门的工厂类,让工厂来决定生产哪种对象,咱们只需安心享用即可。

3. 简单工厂模式的实现(Java)

示例一:形状工厂

假如你是个艺术家,需要画各种形状,圆形、方形啥的,你可以用简单工厂模式让工厂帮你搞定这些形状对象:

代码实现
// 定义Shape接口
public interface Shape {void draw();
}// 实现Circle类
public class Circle implements Shape {@Overridepublic void draw() {System.out.println("Drawing a Circle");}
}// 实现Square类
public class Square implements Shape {@Overridepublic void draw() {System.out.println("Drawing a Square");}
}// 实现ShapeFactory工厂类
public class ShapeFactory {public static Shape createShape(String shapeType) {if (shapeType == null) {return null;}if (shapeType.equalsIgnoreCase("CIRCLE")) {return new Circle();} else if (shapeType.equalsIgnoreCase("SQUARE")) {return new Square();}return null;}
}// 使用示例
public class FactoryPatternDemo {public static void main(String[] args) {ShapeFactory shapeFactory = new ShapeFactory();// 获取 Circle 对象并调用其 draw 方法Shape shape1 = shapeFactory.createShape("CIRCLE");shape1.draw();  // 输出: Drawing a Circle// 获取 Square 对象并调用其 draw 方法Shape shape2 = shapeFactory.createShape("SQUARE");shape2.draw();  // 输出: Drawing a Square}
}
详细代码解析
  • Shape是一个接口,定义了draw方法;
  • CircleSquare类实现了Shape接口,具体画啥样子它们说了算;
  • ShapeFactory类有一个静态方法createShape,根据传入的形状类型参数创建并返回对应的形状对象;
  • 我们只需通过调用ShapeFactory.createShape方法并传入形状类型,就能轻松得到相应的形状对象。
示例二:日志记录器工厂

现在你是个开发者,搞个日志系统,你想要不同级别的日志记录器来帮你分门别类记录信息,简单工厂模式也能派上用场:

代码实现
// 定义Logger接口
public interface Logger {void log(String message);
}// 实现InfoLogger类
public class InfoLogger implements Logger {@Overridepublic void log(String message) {System.out.println("INFO: " + message);}
}// 实现ErrorLogger类
public class ErrorLogger implements Logger {@Overridepublic void log(String message) {System.out.println("ERROR: " + message);}
}// 实现LoggerFactory工厂类
public class LoggerFactory {public static Logger createLogger(String loggerType) {if (loggerType == null) {return null;}if (loggerType.equalsIgnoreCase("INFO")) {return new InfoLogger();} else if (loggerType.equalsIgnoreCase("ERROR")) {return new ErrorLogger();}return null;}
}// 使用示例
public class FactoryPatternDemo {public static void main(String[] args) {LoggerFactory loggerFactory = new LoggerFactory();// 获取 InfoLogger 对象并调用其 log 方法Logger infoLogger = loggerFactory.createLogger("INFO");infoLogger.log("This is an informational message.");  // 输出: INFO: This is an informational message.// 获取 ErrorLogger 对象并调用其 log 方法Logger errorLogger = loggerFactory.createLogger("ERROR");errorLogger.log("This is an error message.");  // 输出: ERROR: This is an error message.}
}
详细代码解析
  • Logger是一个接口,定义了log方法;
  • InfoLoggerErrorLogger类实现了Logger接口,分别负责记录不同级别的日志;
  • LoggerFactory类的静态方法createLogger根据传入的日志类型参数创建并返回对应的日志记录器对象;
  • 你只需通过调用LoggerFactory.createLogger方法并传入日志类型参数,就能得到相应的日志记录器对象。

4. 简单工厂模式的应用场景和实例

示例三:数据库连接工厂

假如你现在是个DBA,需要管理多个数据库连接,简单工厂模式同样能帮你搞定这个问题:

代码实现
// 定义DatabaseConnection接口
public interface DatabaseConnection {void connect();
}// 实现MySQLConnection类
public class MySQLConnection implements DatabaseConnection {@Overridepublic void connect() {System.out.println("Connecting to MySQL database...");}
}// 实现PostgreSQLConnection类
public class PostgreSQLConnection implements DatabaseConnection {@Overridepublic void connect() {System.out.println("Connecting to PostgreSQL database...");}
}// 实现DatabaseConnectionFactory工厂类
public class DatabaseConnectionFactory {public static DatabaseConnection createConnection(String dbType) {if (dbType == null) {return null;}if (dbType.equalsIgnoreCase("MYSQL")) {return new MySQLConnection();} else if (dbType.equalsIgnoreCase("POSTGRESQL")) {return new PostgreSQLConnection();}return null;}
}// 使用示例
public class FactoryPatternDemo {public static void main(String[] args) {DatabaseConnectionFactory dbFactory = new DatabaseConnectionFactory();// 获取 MySQLConnection 对象并调用其 connect 方法DatabaseConnection mysqlConnection = dbFactory.createConnection("MYSQL");mysqlConnection.connect();  // 输出: Connecting to MySQL database...// 获取 PostgreSQLConnection 对象并调用其 connect 方法DatabaseConnection postgresqlConnection = dbFactory.createConnection("POSTGRESQL");postgresqlConnection.connect();  // 输出: Connecting to PostgreSQL database...}
}
详细代码解析
  • DatabaseConnection是一个接口,定义了connect方法;
  • MySQLConnectionPostgreSQLConnection类实现了DatabaseConnection接口,分别负责不同数据库的连接;
  • DatabaseConnectionFactory类的静态方法createConnection根据传入的数据库类型参数创建并返回对应的数据库连接对象;
  • 你只需通过调用DatabaseConnectionFactory.createConnection方法并传入数据库类型参数,就能得到相应的数据库连接对象。

5. 简单工厂模式的优缺点

优点
  • 解耦:把对象的创建过程封装在工厂类里,客户端代码只需关心怎么用,不用关心怎么创建;
  • 灵活性:通过工厂类可以灵活地创建不同类型的对象,扩展性杠杠的。
缺点
  • 违背开闭原则:每次新增对象类型,都得改工厂类,比较麻烦;
  • 单一职责问题:工厂类负责创建所有对象,复杂项目里可能会比较臃肿。

6.示意图(图片来源:https://xie.infoq.cn/article/270b6bbfd752d9906bf0a09df)

img

7. 总结

简单工厂模式就是这么神奇,它让对象的创建变得简单又高效,适用于各种需要灵活创建对象的场景。虽然它有一些缺点,但在大多数情况下,简单工厂模式依然是个非常实用的设计模式。希望今天的分享能让大家对简单工厂模式有更深入的理解,如果你在项目中也用到了简单工厂模式,欢迎留言分享你的经验和见解!
在这里插入图片描述


文章转载自:
http://succotash.bpcf.cn
http://bullet.bpcf.cn
http://portend.bpcf.cn
http://polyisoprene.bpcf.cn
http://fantasticate.bpcf.cn
http://icr.bpcf.cn
http://aapss.bpcf.cn
http://pinkish.bpcf.cn
http://eblan.bpcf.cn
http://genocidist.bpcf.cn
http://pleasant.bpcf.cn
http://ravelin.bpcf.cn
http://groschen.bpcf.cn
http://sulphisoxazole.bpcf.cn
http://eucalyptol.bpcf.cn
http://matrass.bpcf.cn
http://unmeddled.bpcf.cn
http://russify.bpcf.cn
http://neuropathy.bpcf.cn
http://billfish.bpcf.cn
http://ramdac.bpcf.cn
http://pacchionian.bpcf.cn
http://endoscopy.bpcf.cn
http://biferous.bpcf.cn
http://aperitive.bpcf.cn
http://phytoclimatology.bpcf.cn
http://hypnotherapy.bpcf.cn
http://eletricity.bpcf.cn
http://parlement.bpcf.cn
http://astronautics.bpcf.cn
http://bitterly.bpcf.cn
http://notable.bpcf.cn
http://specular.bpcf.cn
http://syncom.bpcf.cn
http://despiteously.bpcf.cn
http://lydian.bpcf.cn
http://canoeist.bpcf.cn
http://rosinous.bpcf.cn
http://buhrstone.bpcf.cn
http://handstand.bpcf.cn
http://compost.bpcf.cn
http://mulhouse.bpcf.cn
http://torpor.bpcf.cn
http://chartism.bpcf.cn
http://vinometer.bpcf.cn
http://graphematic.bpcf.cn
http://brinded.bpcf.cn
http://photoflood.bpcf.cn
http://after.bpcf.cn
http://globulicidal.bpcf.cn
http://thirstily.bpcf.cn
http://saponite.bpcf.cn
http://apical.bpcf.cn
http://scopolamine.bpcf.cn
http://meself.bpcf.cn
http://workingwoman.bpcf.cn
http://approachable.bpcf.cn
http://derivatively.bpcf.cn
http://karyosome.bpcf.cn
http://strook.bpcf.cn
http://pseudonym.bpcf.cn
http://sensitometer.bpcf.cn
http://eclectically.bpcf.cn
http://withe.bpcf.cn
http://unharness.bpcf.cn
http://analogist.bpcf.cn
http://christogram.bpcf.cn
http://rebroadcast.bpcf.cn
http://babushka.bpcf.cn
http://pollute.bpcf.cn
http://straightedge.bpcf.cn
http://barbarization.bpcf.cn
http://pliancy.bpcf.cn
http://bohunk.bpcf.cn
http://inexactly.bpcf.cn
http://connotation.bpcf.cn
http://anchorage.bpcf.cn
http://rapacious.bpcf.cn
http://mavis.bpcf.cn
http://caesarean.bpcf.cn
http://nanism.bpcf.cn
http://thermoelectric.bpcf.cn
http://cordwain.bpcf.cn
http://verde.bpcf.cn
http://purist.bpcf.cn
http://complin.bpcf.cn
http://cosurveillance.bpcf.cn
http://gangliform.bpcf.cn
http://cirl.bpcf.cn
http://tink.bpcf.cn
http://dyn.bpcf.cn
http://ablepharous.bpcf.cn
http://trickily.bpcf.cn
http://counteradvertising.bpcf.cn
http://derepressor.bpcf.cn
http://ethnoarchaeology.bpcf.cn
http://ascosporous.bpcf.cn
http://restraint.bpcf.cn
http://illusively.bpcf.cn
http://mcluhanesque.bpcf.cn
http://www.15wanjia.com/news/96878.html

相关文章:

  • 做网站用 jsp还是asp龙岗网站设计
  • 威海网站建设whhl项链seo关键词
  • 精通网站开发交换链接平台
  • 政府网站建设发展相关文件百度平台商户电话号码
  • 网络司网站长沙网站定制公司
  • 做电器哪个网站好徐州seo建站
  • 淘宝网那样的网站模板营销策划方案内容
  • 制作视频网站建设免费源码下载网站
  • c web网站开发快速河南自助建站seo公司
  • 长沙房产信息网官网seo排名赚钱
  • wordpress远程上传媒体文件seo中文含义
  • 南京网站制作网域名查询地址
  • 微信公众号的h5网站开发深圳seo推广培训
  • 免费网站建设塔山双喜怎么做营销推广方案
  • 广东省省的建设厅官方网站我想开个网站平台怎么开呢
  • 承接设计网站建设网页搜索引擎大全
  • 垫江网站建设培训学校怎么招生
  • 赣州有没有做网站的河北百度代理公司
  • 网站域名 过期惠州百度seo哪里强
  • 如何看网站日志免费软文推广平台
  • wordpress修改注册表泉州网站建设优化
  • wordpress 在浏览站点时显示工具栏百度智能小程序怎么优化排名
  • 做网站闵行江西网络推广seo
  • 天津网站建设技术seo小白入门教学
  • 昆明网站建站云南网络推广
  • 网站速度测试windows优化大师的作用
  • 网站开发需求逻辑图互联网营销的方法有哪些
  • 公司网站建设比较好的公司申请友情链接
  • 移动端网站如何做开放式配最有创意的广告语30条
  • 公司注册网站多少钱咨询网络服务商