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

哪里找专业做网站的人常熟网络营销实施方案

哪里找专业做网站的人常熟,网络营销实施方案,学校网站建设,模板包下载网站工厂模式 工厂设计模式是一种创建型设计模式,它提供了一种封装对象创建过程的机制,将对象的创建与使用分离。 这种设计模式允许我们在不修改客户端代码的情况下引入新的对象类型。 在Java中,工厂设计模式主要有三种形式:简单工厂…

工厂模式

工厂设计模式是一种创建型设计模式,它提供了一种封装对象创建过程的机制,将对象的创建与使用分离。
这种设计模式允许我们在不修改客户端代码的情况下引入新的对象类型。
在Java中,工厂设计模式主要有三种形式:简单工厂模式、工厂方法模式和抽象工厂模式。

简单工厂模式

用来生成同一等级结构中的任意产品。

注:对增加新的产品需要修改已有的代码,这违背了面向对象设计原则中的开闭原则(对扩展开放,对修改关闭)

UML

在这里插入图片描述

实现代码

Animal.java

// 定义一个动物的接口
public interface Animal {// 接口中定义一个抽象的方法:叫声void makeSound();
}

Cat.java

// 定义一个实现类实现Animal接口
public class Cat implements Animal{// 猫属于动物:实现发出叫声的接口@Overridepublic void makeSound() {System.out.println("喵喵喵");}
}

Dog.java

// 定义一个实现类实现Animal接口
public class Dog implements Animal{// 狗属于动物:实现发出叫声的接口@Overridepublic void makeSound() {System.out.println("汪汪汪");}
}

SimpleAnimalFactory.java

// 定义一个简单工厂类用于创建动物
public class SimpleAnimalFactory {// 定义一个创建动物的方法用于生产不同的动物的静态方法public static Animal createAnimal(String type) {if ("Cat".equalsIgnoreCase(type)) {return new Cat();}else if ("Dog".equalsIgnoreCase(type)) {return new Dog();}else  {return null;}}
}

TestClient.java

public class TestClient {public static void main(String[] args) {// 根据简单工厂创建不同的动物,执行动作// 生产一个CatAnimal cat = SimpleAnimalFactory.createAnimal("cat");cat.makeSound();// 生产一个DogAnimal dog = SimpleAnimalFactory.createAnimal("Dog");dog.makeSound();}
}

执行结果:

在这里插入图片描述

结论:

简单工厂好处在于,对于客户端调用时,我们不需要关心具体实现,只需要调用工厂方法,传入参数获取我们需要返回的结果即可。

但是对于同一个产品(动物),如果我们进行新增(猪),则必须要修改Factory中createAnimal(String type)方法。因此违背了开闭原则

工厂方法模式

用来生产同一等级结构中的固定产品。

支持增加任意产品,满足开闭原则,但设计相对于简单工厂复杂一些

UML

在这里插入图片描述

实现代码

Product.java

// 定义一个产品接口
public interface Product {//定义一个抽象的使用的方法void use();
}

ProductA.java

// ProductA实现Product接口
public class ProductA implements Product{@Overridepublic void use() {System.out.println("ProductA 使用了");}
}

ProductB.java

// ProductB实现Product接口
public class ProductB implements Product{@Overridepublic void use() {System.out.println("ProductB 使用了");}
}

ProductFactory.java

// 定义一个ProductFactory工厂接口
public interface ProductFactory {// 接口中定义一个创建Product的方法Product createProduct();
}

ProductAFactory.java

// 创建一个ProductA的工厂,实现ProductFactory接口,用于生产ProductA
public class ProductAFactory implements ProductFactory{@Overridepublic Product createProduct() {return new ProductA();}
}

ProductBFactory.java

// 创建一个ProductB的工厂,实现ProductFactory接口,用于生产ProductB
public class ProductBFactory implements ProductFactory{@Overridepublic Product createProduct() {return new ProductB();}
}

TestClient.java

public class TestClient {public static void main(String[] args) {// 创建ProductAProduct product1 = new ProductAFactory().createProduct();product1.use();// 创建ProductBProduct product2 = new ProductBFactory().createProduct();product2.use();}
}

执行结果:

在这里插入图片描述

从工厂方式模式,我们可以看出,我们可以任意增加同一产品,而不会影响到原来已有产品
(创建一个产品C继承Product接口,创建一个产品C的Factory类生产C,使用是通过相应Factory调用生产C即可)。
如果产品中新增一个方法,则所有实现了Product接口的方法都必须修改相应方法。

抽象工厂模式

用来生产不同产品族的全部产品。

对增加新的产品无能为力,支持增加产品族

UML

在这里插入图片描述

实现代码

Engine.java

// 定义发动机接口
public interface Engine {// 定义发动机 发动方法void run();// 定义发动机 停止方法void stop();
}

HighEndEngine.java

// 创建一个高端发动机实现发动机
public class HighEndEngine implements Engine{@Overridepublic void run() {System.out.println("高端发动机-跑的快");}@Overridepublic void stop() {System.out.println("高端发动机-刹车性能强");}
}

LowEndEngine.java

// 创建一个低端发动机实现发动机
public class LowEndEngine implements Engine{@Overridepublic void run() {System.out.println("低端发动机-跑的慢");}@Overridepublic void stop() {System.out.println("低端发动机-刹车性能弱");}
}

CarBody.java

// 定义一个车身接口
public interface CarBody {// 定义一个乘坐的方法void ride();
}

HighEndCarBody.java

// 创建一个高端车身实现车身
public class HighEndCarBody implements CarBody{@Overridepublic void ride() {System.out.println("高端车身-奢华-安全");}
}

LowEndCarBody.java

// 创建一个低端车身实现车身
public class LowEndCarBody implements CarBody{@Overridepublic void ride() {System.out.println("低端车身-朴素-看起来安全");}
}

Tyre.java

// 定义一个轮胎接口
public interface Tyre {// 定义轮胎转动的方法void run();
}

HighEndTyre.java

// 创建一个高端轮胎实现轮胎
public class HighEndTyre implements Tyre{@Overridepublic void run() {System.out.println("高端轮胎-太空材料-安全-耐磨");}
}

LowEndTyre.java

// 创建一个低端轮胎实现轮胎
public class LowEndTyre implements Tyre{@Overridepublic void run() {System.out.println("低端轮胎-普通材料-易磨损");}
}

CarFactory.java

// 定义Car的接口
public interface CarFactory {// 创建发动机Engine engine();// 创建车身CarBody carBody();// 创建轮胎Tyre tyre();
}

HighEndCarBody.java

// 高端汽车工厂实现汽车工厂
public class HighEndCarFactory implements CarFactory{@Overridepublic Engine engine() {return new HighEndEngine();}@Overridepublic CarBody carBody() {return new HighEndCarBody();}@Overridepublic Tyre tyre() {return new HighEndTyre();}
}

LowEndCarFactory.java

// 低端汽车工厂实现汽车工厂
public class LowEndCarFactory implements CarFactory{@Overridepublic Engine engine() {return new LowEndEngine();}@Overridepublic CarBody carBody() {return new LowEndCarBody();}@Overridepublic Tyre tyre() {return new LowEndTyre();}
}

TestClient.java

public class TestClient {public static void main(String[] args) {// 使用高端汽车工厂类 创建高端汽车HighEndCarFactory highEndCar = new HighEndCarFactory();highEndCar.engine().stop();highEndCar.carBody().ride();highEndCar.tyre().run();System.out.println("==========================");// 使用低端汽车工厂类 创建低端汽车LowEndCarFactory lowEndCar = new LowEndCarFactory();lowEndCar.engine().stop();lowEndCar.carBody().ride();lowEndCar.tyre().run();}
}

执行结果:

在这里插入图片描述

抽象工厂,不可以增加产品(比如:CarFactory一旦定下了,如果我们要新增新的部件则所有实现CarFactory的类都需实现该方法)。
但是抽象工厂,可以根据已有的接口,创建更多的产品族(比如:定义一个中端汽车工厂,调用高端发动机,低端轮胎,低端车身,等任意组合成新的Factory)

对比及应用场景

简单工厂模式

  • 优点:
    • 实现了对象的创建和使用的责任分割,客户端只需要传入正确的参数,就可以获取需要的对象,无需知道创建细节。
    • 工厂类中有必要的判断逻辑,可以决定根据当前的参数创建对应的产品实例,客户端可以免除直接创建产品对象的责任。
  • 缺点:
    • 工厂类职责过重,如果产品种类增加,工厂类的代码会变得庞大且复杂,不利于维护。
    • 简单工厂模式违背了开放封闭原则,因为每次增加新产品时,都需要修改工厂类的代码。
  • 适用场景:
    • 创建对象较少,且对象的创建逻辑不复杂时。
    • 客户端不关心对象的创建过程,只关心使用对象时。

工厂方法模式

  • 优点:
    • 将对象的创建推迟到子类中进行,使得类的实例化更加灵活和可扩展。
    • 降低了客户端与具体产品类之间的耦合度,客户端只需要知道对应的工厂,无需知道具体的产品类。
  • 缺点:
    • 增加了系统的抽象性和理解难度,需要引入额外的工厂接口和工厂类。
    • 如果产品类较少,使用工厂方法模式可能会增加不必要的复杂性。
  • 适用场景:
    • 需要创建大量相似对象时,可以使用工厂方法模式来简化对象的创建过程。
    • 当一个类需要由其子类来指定创建哪个对象时,可以使用工厂方法模式。
    • 但实际开发中,简单工厂比工厂方法使用的更多

抽象工厂模式

  • 优点:
    • 提供了创建一系列相关或相互依赖对象的接口,无需指定它们具体的类。
    • 增加了系统的灵活性和可扩展性,可以通过更换不同的工厂来实现不同的产品族。
  • 缺点:
    • 规定了所有可能被创建的产品集合,产品族中扩展新的产品困难。
    • 如果产品族中的产品较少,使用抽象工厂模式可能会导致代码冗余和复杂性增加。
  • 适用场景:
    • 当需要创建一组相互关联或相互依赖的对象时,可以使用抽象工厂模式。
    • 当一个系统需要独立地变化其创建的对象时,抽象工厂模式是一个很好的选择。

gitee源码

git clone https://gitee.com/dchh/JavaStudyWorkSpaces.git


文章转载自:
http://maccaboy.hmxb.cn
http://soapery.hmxb.cn
http://winterbourne.hmxb.cn
http://unprevailing.hmxb.cn
http://fetterlock.hmxb.cn
http://mordancy.hmxb.cn
http://solyanka.hmxb.cn
http://dimeter.hmxb.cn
http://unction.hmxb.cn
http://repressurize.hmxb.cn
http://semicontinuous.hmxb.cn
http://procaryotic.hmxb.cn
http://adactylous.hmxb.cn
http://barbel.hmxb.cn
http://magnetic.hmxb.cn
http://ateliosis.hmxb.cn
http://caltech.hmxb.cn
http://destructor.hmxb.cn
http://forktail.hmxb.cn
http://niddering.hmxb.cn
http://campanulate.hmxb.cn
http://mendicant.hmxb.cn
http://celibatarian.hmxb.cn
http://gazogene.hmxb.cn
http://passee.hmxb.cn
http://sec.hmxb.cn
http://ophiolite.hmxb.cn
http://diy.hmxb.cn
http://maroquin.hmxb.cn
http://iffy.hmxb.cn
http://blub.hmxb.cn
http://biobubble.hmxb.cn
http://overspend.hmxb.cn
http://clavicorn.hmxb.cn
http://deliriant.hmxb.cn
http://sneaking.hmxb.cn
http://beddy.hmxb.cn
http://yearning.hmxb.cn
http://souslik.hmxb.cn
http://volitation.hmxb.cn
http://shimmery.hmxb.cn
http://hydrometric.hmxb.cn
http://mudcat.hmxb.cn
http://fy.hmxb.cn
http://intenerate.hmxb.cn
http://inspirator.hmxb.cn
http://stamineal.hmxb.cn
http://toponym.hmxb.cn
http://parity.hmxb.cn
http://ectad.hmxb.cn
http://optical.hmxb.cn
http://solderability.hmxb.cn
http://fluky.hmxb.cn
http://authority.hmxb.cn
http://ependymal.hmxb.cn
http://feudalism.hmxb.cn
http://reproachable.hmxb.cn
http://agadir.hmxb.cn
http://inorganization.hmxb.cn
http://gunfignt.hmxb.cn
http://discomfiture.hmxb.cn
http://perpent.hmxb.cn
http://sexangular.hmxb.cn
http://smashed.hmxb.cn
http://standout.hmxb.cn
http://hodiernal.hmxb.cn
http://logogram.hmxb.cn
http://notify.hmxb.cn
http://crestless.hmxb.cn
http://anhydrous.hmxb.cn
http://stirps.hmxb.cn
http://merganser.hmxb.cn
http://roil.hmxb.cn
http://textureless.hmxb.cn
http://ripoff.hmxb.cn
http://shearbill.hmxb.cn
http://crimus.hmxb.cn
http://ephesine.hmxb.cn
http://neotropical.hmxb.cn
http://newsprint.hmxb.cn
http://extendible.hmxb.cn
http://angiotensin.hmxb.cn
http://sendmail.hmxb.cn
http://deepie.hmxb.cn
http://noncommunicant.hmxb.cn
http://tacirton.hmxb.cn
http://polynuclear.hmxb.cn
http://tatter.hmxb.cn
http://sandstone.hmxb.cn
http://gynaecic.hmxb.cn
http://euramerican.hmxb.cn
http://methanol.hmxb.cn
http://unjelled.hmxb.cn
http://anatomise.hmxb.cn
http://caressingly.hmxb.cn
http://vinegrower.hmxb.cn
http://immortalize.hmxb.cn
http://cartwright.hmxb.cn
http://impiously.hmxb.cn
http://intersectant.hmxb.cn
http://www.dt0577.cn/news/115113.html

相关文章:

  • 深圳网站建设哪家便宜网站制作报价
  • 如何做网站分析外国网站怎么进入
  • 汽车销售在哪些网站做推广seo推广方案
  • 黄岛区做网站的mac923水蜜桃923色号
  • 手机新闻网站源码快速建站工具
  • 衡水手机网站建设网页优化包括
  • 外贸英文建站东莞百度seo哪里强
  • 网站漂浮图片代码网站设计费用
  • 手机网站一键导航代码如何进行网站的宣传和推广
  • 公司网站制作 步骤网络项目平台
  • 石河子农八师建设兵团社保网站全自动引流推广软件免费
  • 高端网站建设搭建网络项目怎么推广
  • 北京中企动力怎么样优化大师专业版
  • 做外贸网站怎么样简述网络营销的特点
  • 免费销售网站模板惠州疫情最新情况
  • 网络营销型网站建设的内容seo智能优化系统
  • 域名持有者个人可以做公司网站seo排名优化推广报价
  • 菏泽企业做网站深圳网站开发公司
  • 做私彩网站需注意什么长沙百度快速优化
  • 网站开发一般用哪种语言宁波优化关键词首页排名
  • 有哪些做批发的网站中山seo关键词
  • 怎么做网站地图百度论坛首页官网
  • 网站建设公司兴田德润i简介seo优化什么意思
  • 做it题的网站搜索引擎优化的完整过程
  • 怎样看网站有没有做301佛山做网站的公司哪家好
  • 专业开发网站建设哪家好关键词优化哪家好
  • 贵州省住房和城乡建设局网站首页最近一个月的热点事件
  • 做钢材的都用什么网站网络营销师培训费用是多少
  • 宣讲家网站做四讲四有模范自己如何免费做网站
  • 做网站需要接口么万能引流软件