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

兰州网站排名优化公司广州企业网站哪家好

兰州网站排名优化公司,广州企业网站哪家好,做信息分类网站难吗,如何上国外购物网站目录 ChatGpt问答原生代码简单工厂模式代码 简单工厂模式#xff08;Simple Factory Pattern#xff09;新增boat 对比两种方法原生代码为什么使用强制转换#xff1f;简单工厂模式 简单工厂方法总结与原生代码的区别#xff1a;优点:缺点#xff1a; 参考 本文将介绍什么… 目录 ChatGpt问答原生代码简单工厂模式代码 简单工厂模式Simple Factory Pattern新增boat 对比两种方法原生代码为什么使用强制转换简单工厂模式 简单工厂方法总结与原生代码的区别优点:缺点 参考 本文将介绍什么事简单工厂模式为什么要使用这个模式比起原生代码它是如何降低代码的耦合度的以及详细的代码对比结合ChatGPT的问答进行讲解也是自己学习的记录有不对的地方请指正。 ChatGpt问答 QGive an example to illustrate the benefits of reducing the coupling of the simple factory model in java, which is best reflected in code 举个例子来说明java中简单工厂模式减少耦合的好处在代码中最能体现 Certainly! Let’s illustrate the benefits of reducing coupling by using a simple factory model in Java. We’ll start with a coupled implementation and then refactor it to reduce coupling. 当然 让我们说明一下在 Java 中使用简单工厂模型来减少耦合的好处。 我们将从耦合实现开始然后重构它以减少耦合。 Coupled Implementation (High Coupling): Consider a simple factory that produces different types of vehicles (e.g., cars and bicycles) based on a provided type. In this coupled example, the factory method has a high level of coupling with the concrete vehicle classes. 考虑一个简单的工厂它根据提供的类型生产不同类型的车辆例如汽车和自行车。 在这个耦合示例中工厂方法与具体车辆类具有高度耦合。 原生代码 class VehicleFactory {public Vehicle createVehicle(String type) {if (car.equalsIgnoreCase(type)) {return new Car();} else if (bicycle.equalsIgnoreCase(type)) {return new Bicycle();} else {throw new IllegalArgumentException(Invalid vehicle type);}} }class Car {void start() {System.out.println(Car started.);} }class Bicycle {void start() {System.out.println(Bicycle started.);} }public class Main {public static void main(String[] args) {VehicleFactory factory new VehicleFactory();Vehicle vehicle1 factory.createVehicle(car);vehicle1.start(); // Output: Car started.Vehicle vehicle2 factory.createVehicle(bicycle);vehicle2.start(); // Output: Bicycle started.} }In this example, the VehicleFactory class has high coupling because it directly creates instances of Car and Bicycle. If we need to add more types of vehicles in the future, we would need to modify the factory method, resulting in increased coupling and reduced flexibility. 在这个例子中VehicleFactory类具有高耦合性因为它直接创建Car和Bicycle的实例。 如果以后需要增加更多类型的车辆就需要修改工厂方法导致耦合性增加灵活性降低。 简单工厂模式代码 Reduced Coupling Implementation (Low Coupling): To reduce coupling, we can use interfaces and abstract classes to define a common contract for all vehicles. This way, the factory method doesn’t need to know the concrete classes of vehicles. interface Vehicle {void start(); }class Car implements Vehicle {public void start() {System.out.println(Car started.);} }class Bicycle implements Vehicle {public void start() {System.out.println(Bicycle started.);} }class VehicleFactory {public Vehicle createVehicle(String type) {if (car.equalsIgnoreCase(type)) {return new Car();} else if (bicycle.equalsIgnoreCase(type)) {return new Bicycle();} else {throw new IllegalArgumentException(Invalid vehicle type);}} }public class Main {public static void main(String[] args) {VehicleFactory factory new VehicleFactory();Vehicle vehicle1 factory.createVehicle(car);vehicle1.start(); // Output: Car started.Vehicle vehicle2 factory.createVehicle(bicycle);vehicle2.start(); // Output: Bicycle started.} }In this refactored example, we’ve introduced an interface Vehicle, which both Car and Bicycle implement. The VehicleFactory now returns instances of the Vehicle interface instead of concrete classes. This reduces coupling because the factory method doesn’t depend on specific implementations, making it easier to extend the system with new types of vehicles without modifying existing code. 在这个重构的示例中我们引入了一个“Vehicle”接口“Car”和“Bicycle”都实现了该接口。 “VehicleFactory”现在返回“Vehicle”接口的实例而不是具体类。 这减少了耦合因为工厂方法不依赖于特定的实现从而可以更轻松地使用新型车辆扩展系统而无需修改现有代码。 简单工厂模式Simple Factory Pattern 是一种创建型设计模式它提供了一种简单的方法来创建对象而不需要直接暴露对象的创建逻辑给客户端。 简单工厂模式又称静态工厂方法模式。它存在的目的很简单定义一个用于创建对象的接口。 组成结构 工厂类角色(Creator)这是本模式的核心含有一定的商业逻辑和判断逻辑。在java中它往往由一个具体类实现。 抽象产品角色(Product)它一般是具体产品继承的父类或者实现的接口。在java中由接口或者抽象类来实现。 具体产品角色(ConcreteProduct)工厂类所创建的对象就是此角色的实例。在java中由一个具体类实现。 用类图表示它们之间的关系 抽象产品角色–Vehicle 交通工具 具体产品角色 – Car、Bicycle 工厂类角色–VehicleFactory 新增boat 对比两种方法 原生代码 Certainly! In a high coupling approach, you would directly create instances of vehicle classes within the factory method, which can lead to tight dependencies between the factory and the concrete vehicle classes. Here’s an example of a high coupling approach: 当然 在高耦合方法中您将直接在工厂方法中创建车辆类的实例这可能导致工厂和具体车辆类之间的紧密依赖关系。 这是一个高耦合方法的示例 class Car {void start() {System.out.println(Car started.);} }class Bicycle {void start() {System.out.println(Bicycle started.);} }class Boat {void start() {System.out.println(Boat started.);} }class VehicleFactory {public Object createVehicle(String type) {if (car.equalsIgnoreCase(type)) {return new Car();} else if (bicycle.equalsIgnoreCase(type)) {return new Bicycle();} else if (boat.equalsIgnoreCase(type)) {return new Boat();} else {throw new IllegalArgumentException(Invalid vehicle type);}} }public class Main {public static void main(String[] args) {VehicleFactory factory new VehicleFactory();Car car (Car) factory.createVehicle(car);car.start(); // Output: Car started.Bicycle bicycle (Bicycle) factory.createVehicle(bicycle);bicycle.start(); // Output: Bicycle started.Boat boat (Boat) factory.createVehicle(boat);boat.start(); // Output: Boat started.} }In this high coupling approach: Each vehicle class (Car, Bicycle, Boat) is directly instantiated within the createVehicle method of the VehicleFactory.To use a specific vehicle, you need to cast the object returned by the factory to the corresponding vehicle class (Car, Bicycle, Boat).Adding a new type of vehicle would require modifying the VehicleFactory to create that vehicle type directly. 在这种高耦合方法中 每个车辆类“Car”、“Bicycle”、“Boat”都直接在“VehicleFactory”的“createVehicle”方法中实例化。要使用特定的车辆需要将工厂返回的对象转换为相应的车辆类Car、Bicycle、Boat。添加新类型的车辆需要修改 VehicleFactory 以直接创建该车辆类型。 High coupling can make the code less flexible and harder to maintain because any changes to the factory method or the concrete vehicle classes may affect other parts of the code that rely on them. It’s generally a best practice to reduce coupling and use interfaces or abstract classes to achieve better flexibility and maintainability. 高耦合会使代码不太灵活且难以维护因为对工厂方法或具体车辆类的任何更改都可能会影响依赖它们的代码的其他部分。 通常最佳实践是减少耦合并使用接口或抽象类来实现更好的灵活性和可维护性。 为什么使用强制转换 Car car (Car) factory.createVehicle(“car”) In the high coupling approach I provided earlier, you are correct that type casting is required when you retrieve the created objects from the factory. This is a limitation of high coupling because you’re directly creating instances of specific classes within the factory method. Let me explain this in more detail: In the createVehicle method of the VehicleFactory, the factory creates instances of Car, Bicycle, or Boat based on the type parameter. Since the createVehicle method is declared to return an Object, when you retrieve the created object, it’s treated as an Object reference. To use the specific functionality of a Car, for example, you need to cast the Object reference back to Car to access Car-specific methods like start. This type casting introduces potential issues because if you provide the wrong type for the type parameter or if you introduce a new vehicle type in the future, you might get runtime errors (ClassCastException) if the casting doesn’t match the actual type. In contrast, a low coupling approach, as demonstrated in the previous example, uses interfaces or abstract classes to create a common contract for all vehicles. This way, you can avoid type casting and handle all vehicles uniformly by relying on the common interface. It provides better flexibility, maintainability, and type safety, as you don’t need to cast objects to specific types when using them. 在我之前提供的高耦合方法中当您从工厂检索创建的对象时需要进行类型转换这是正确的。 这是高耦合的限制因为您是在工厂方法中直接创建特定类的实例。 让我更详细地解释一下 在 VehicleFactory 的 createVehicle 方法中工厂根据类型参数创建 Car、Bicycle 或 Boat 的实例。 由于 createVehicle 方法被声明为返回一个对象因此当您检索创建的对象时它将被视为对象引用。 例如要使用汽车的特定功能您需要将对象引用强制转换回汽车以访问汽车特定的方法例如启动。 这种类型转换会带来潜在的问题因为如果您为类型参数提供了错误的类型或者如果您将来引入新的车辆类型并且转换与实际类型不匹配则可能会出现运行时错误 (ClassCastException)。 相反低耦合方法如前面的示例所示使用接口或抽象类为所有车辆创建通用契约。 这样您可以避免类型转换并依靠通用接口统一处理所有车辆。 它提供了更好的灵活性、可维护性和类型安全性因为您在使用对象时不需要将对象强制转换为特定类型。 简单工厂模式 Qshow me High Coupling method Certainly! Let’s add a new Boat vehicle to the existing code. We’ll follow the same approach of using an interface for Boat and updating the factory method to create Boat instances without modifying the existing code. 当然 让我们在现有代码中添加一个新的“Boat”车辆。 我们将遵循相同的方法使用“Boat”接口并更新工厂方法来创建“Boat”实例而无需修改现有代码。 Adding a Boat Vehicle (Low Coupling): interface Vehicle {void start(); }class Car implements Vehicle {public void start() {System.out.println(Car started.);} }class Bicycle implements Vehicle {public void start() {System.out.println(Bicycle started.);} }class Boat implements Vehicle {public void start() {System.out.println(Boat started.);} }class VehicleFactory {public Vehicle createVehicle(String type) {if (car.equalsIgnoreCase(type)) {return new Car();} else if (bicycle.equalsIgnoreCase(type)) {return new Bicycle();} else if (boat.equalsIgnoreCase(type)) {return new Boat();} else {throw new IllegalArgumentException(Invalid vehicle type);}} }public class Main {public static void main(String[] args) {VehicleFactory factory new VehicleFactory();Vehicle vehicle1 factory.createVehicle(car);vehicle1.start(); // Output: Car started.Vehicle vehicle2 factory.createVehicle(bicycle);vehicle2.start(); // Output: Bicycle started.Vehicle vehicle3 factory.createVehicle(boat);vehicle3.start(); // Output: Boat started.} }In this updated code, we’ve introduced a new Boat class that implements the Vehicle interface. The VehicleFactory has been updated to handle the creation of Boat instances when “boat” is passed as the type. This allows us to add a new type of vehicle (in this case, a boat) without modifying the existing code for the factory or the other vehicles. This demonstrates the flexibility and low coupling achieved by using interfaces and an abstract factory approach. 在此更新的代码中我们引入了一个新的“Boat”类它实现了“Vehicle”接口。 “VehicleFactory”已更新可以在“boat”作为类型传递时处理“Boat”实例的创建。 这使我们能够添加一种新型车辆在本例中为船而无需修改工厂或其他车辆的现有代码。 这展示了通过使用接口和抽象工厂方法实现的灵活性和低耦合。 简单工厂方法总结 与原生代码的区别 从三个维度来说明 抽象产品角色–Vehicle 交通工具 具体产品角色 – Car、Bicycle 工厂类角色–VehicleFactory 1 简单工厂模式中抽象产品角色Vehicle 使用interface原生代码是class 2 简单工厂模式中具体产品角色Car需要实现接口原生代码为单独类 3 简单工厂模式中具体工厂类角色–VehicleFactory 返回对象是Vehicle 接口原生代码的返回对象是Object 优点: 将对象的创建逻辑集中在工厂类中降低了客户端的复杂度。隐藏了创建对象的细节客户端只需要关心需要创建何种对象无需关心对象是如何创建的。 可以通过修改工厂类来轻松添加新的产品类 缺点 如果产品的类太多会导致工厂类中的代码变得很复杂难以维护。添加新产品时需要修改工厂类也就是会在OperationFactory类中新增case语句这违背了开闭原则。 总体而言简单工厂模式适用于创建对象的逻辑相对简单且产品类的数量较少的场景。对于更复杂的对象创建和对象之间的依赖关系可以考虑使用其他创建型设计模式如工厂方法模式或抽象工厂模式。 参考 1 设计模式-01.简单工厂方法https://juejin.cn/post/7267091509953855543 2 JAVA设计模式(三) – 工厂模式 https://juejin.cn/post/7257441765976162361?searchId202309061335355A323BC0D43AD67B950D#heading-0
http://www.dnsts.com.cn/news/224040.html

相关文章:

  • 域名有了怎么制作网站怎么建设阿里巴巴国际网站首页
  • 网站推广六种方法网站锚点怎么做
  • 深圳需要做网站的公司有哪些惠州网站建设米普可思
  • 用ps如何做短视频网站生活服务网站建设
  • 文章内容网站系统百度收录公司网站
  • 做精彩网站分析的方向是犀牛建筑网校
  • 建设网站要准备什么站酷网官网
  • 必应网站管理员工具栾城区城乡建设局网站
  • 会同县做网站wordpress使用qq登录
  • 专业建站分销商城广州外贸网站建设 open
  • 专门做门业的网站网站如何做查询表单
  • 山东免费网站建设汝南企业网站建设
  • 成都html5网站建设电子商务网站建设主管的策划书
  • 织梦网站栏目调用黑色大气网站
  • 邯郸网站优化怎么用新邵县住房和城乡建设局网站
  • 中山网站优化排名网站设计模板简约
  • 杭州做产地证去哪个网站做网站找哪家好
  • 搜索视频 网站开发网站开发进度把握
  • 合肥 电子商务 网站推广如何查看网站cms系统
  • 做接口自动化是网站登录加密设计网站开发费用计入什么科目
  • 免费网站制作教程旅游网站建设 策划书
  • 昆明网站建设方案策划wordpress开启多站点后台没显示
  • 专业的网站开发团队云南信息港
  • 网站设计好做吗wordpress仿砍柴网
  • 广告公司起名字大全网站快速排名优化报价
  • 做网站服务器还是虚拟空间好游戏代理
  • 做资讯网站怎么挣钱哪个网站代做装修效果图
  • 深圳网站设计公司怎么做微信公众平台注册要钱吗
  • .net 网站 源代码广州网站建设优化方案
  • 网站seo注意事项昆明网站开发的公司