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

网站开发和维护和java榆林seo

网站开发和维护和java,榆林seo,深圳网站建设外包公司哪家好,沈阳做网站的公司排名文章目录 建造者模式介绍优点缺点使用场景 实现javarust rust代码仓库 建造者模式 建造者模式(Builder Pattern)使用多个简单的对象一步一步构建成一个复杂的对象。 一个 Builder 类会一步一步构造最终的对象。该 Builder 类是独立于其他对象的。 介绍…

文章目录

    • 建造者模式
      • 介绍
        • 优点
        • 缺点
        • 使用场景
      • 实现
      • java
      • rust
    • rust代码仓库

建造者模式

建造者模式(Builder Pattern)使用多个简单的对象一步一步构建成一个复杂的对象。

一个 Builder 类会一步一步构造最终的对象。该 Builder 类是独立于其他对象的。

介绍

  • 意图:将一个复杂的构建与其表示相分离,使得同样的构建过程可以创建不同的表示。
  • 主要解决:主要解决在软件系统中,有时候面临着"一个复杂对象"的创建工作,其通常由各个部分的子对象用一定的算法构成;由于需求的变化,这个复杂对象的各个部分经常面临着剧烈的变化,但是将它们组合在一起的算法却相对稳定。
  • 关键代码:建造者:创建和提供实例,导演:管理建造出来的实例的依赖关系。
  • 应用实例: 1、去肯德基,汉堡、可乐、薯条、炸鸡翅等是不变的,而其组合是经常变化的,生成出所谓的"套餐"。
优点
  • 分离构建过程和表示,使得构建过程更加灵活,可以构建不同的表示。
  • 可以更好地控制构建过程,隐藏具体构建细节。
  • 代码复用性高,可以在不同的构建过程中重复使用相同的建造者。
缺点
  • 如果产品的属性较少,建造者模式可能会导致代码冗余。
  • 建造者模式增加了系统的类和对象数量。
使用场景
  • 需要生成的对象具有复杂的内部结构。
  • 需要生成的对象内部属性本身相互依赖。

建造者模式在创建复杂对象时非常有用,特别是当对象的构建过程涉及多个步骤或参数时。它可以提供更好的灵活性和可维护性,同时使得代码更加清晰可读。

实现

我们假设一个快餐店的商业案例,其中,一个典型的套餐可以是一个汉堡(Burger)和一杯冷饮(Cold drink)。汉堡(Burger)可以是素食汉堡(Veg Burger)或鸡肉汉堡(Chicken Burger),它们是包在纸盒中。冷饮(Cold drink)可以是可口可乐(coke)或百事可乐(pepsi),它们是装在瓶子中。

我们将创建一个表示食物条目(比如汉堡和冷饮)的 Item 接口和实现 Item 接口的实体类,以及一个表示食物包装的 Packing 接口和实现 Packing 接口的实体类,汉堡是包在纸盒中,冷饮是装在瓶子中。

然后我们创建一个 Meal 类,带有 Item 的 ArrayList 和一个通过结合 Item 来创建不同类型的 Meal 对象的 MealBuilder。BuilderPatternDemo 类使用 MealBuilder 来创建一个 Meal。

建造者模式的 UML 图
在这里插入图片描述

java

步骤 1
创建一个表示食物条目和食物包装的接口。
Item.java

public interface Item {public String name();public Packing packing();public float price();    
}
Packing.java
public interface Packing {public String pack();
}

步骤 2
创建实现 Packing 接口的实体类,方便进行不同的包装。
Wrapper.java

public class Wrapper implements Packing {@Overridepublic String pack() {return "Wrapper";}
}

Bottle.java

public class Bottle implements Packing {@Overridepublic String pack() {return "Bottle";}
}

步骤 3
创建实现 Item 接口的抽象类,该类提供了默认的功能,实现汉堡抽象和冷饮抽象的包装类。

Burger.java

public abstract class Burger implements Item {@Overridepublic Packing packing() {return new Wrapper();}@Overridepublic abstract float price();
}

ColdDrink.java

public abstract class ColdDrink implements Item {@Overridepublic Packing packing() {return new Bottle();}@Overridepublic abstract float price();
}

步骤 4
创建扩展了 Burger 和 ColdDrink 的实体类。

VegBurger.java

public class VegBurger extends Burger {@Overridepublic float price() {return 25.0f;}@Overridepublic String name() {return "Veg Burger";}
}

ChickenBurger.java

public class ChickenBurger extends Burger {@Overridepublic float price() {return 50.5f;}@Overridepublic String name() {return "Chicken Burger";}
}

Coke.java


public class Coke extends ColdDrink {@Overridepublic float price() {return 30.0f;}@Overridepublic String name() {return "Coke";}
}

Pepsi.java


public class Pepsi extends ColdDrink {@Overridepublic float price() {return 35.0f;}@Overridepublic String name() {return "Pepsi";}
}

步骤 5
创建一个 Meal 类,带有上面定义的 Item 对象。

Meal.java

import java.util.ArrayList;
import java.util.List;public class Meal {private List<Item> items = new ArrayList<Item>();    public void addItem(Item item){items.add(item);}public float getCost(){float cost = 0.0f;for (Item item : items) {cost += item.price();}        return cost;}public void showItems(){for (Item item : items) {System.out.print("Item : "+item.name());System.out.print(", Packing : "+item.packing().pack());System.out.println(", Price : "+item.price());}        }    
}

步骤 6
创建一个 MealBuilder 类,实际的 builder 类负责创建 Meal 对象。

MealBuilder.java

public class MealBuilder {public Meal prepareVegMeal (){Meal meal = new Meal();meal.addItem(new VegBurger());meal.addItem(new Coke());return meal;}   public Meal prepareNonVegMeal (){Meal meal = new Meal();meal.addItem(new ChickenBurger());meal.addItem(new Pepsi());return meal;}
}

步骤 7
BuiderPatternDemo 使用 MealBuilder 来演示建造者模式(Builder Pattern)。

BuilderPatternDemo.java

public class BuilderPatternDemo {public static void main(String[] args) {MealBuilder mealBuilder = new MealBuilder();Meal vegMeal = mealBuilder.prepareVegMeal();System.out.println("Veg Meal");vegMeal.showItems();System.out.println("Total Cost: " +vegMeal.getCost());Meal nonVegMeal = mealBuilder.prepareNonVegMeal();System.out.println("\n\nNon-Veg Meal");nonVegMeal.showItems();System.out.println("Total Cost: " +nonVegMeal.getCost());}
}

步骤 8
执行程序,输出结果:


Veg Meal
Item : Veg Burger, Packing : Wrapper, Price : 25.0
Item : Coke, Packing : Bottle, Price : 30.0
Total Cost: 55.0Non-Veg Meal
Item : Chicken Burger, Packing : Wrapper, Price : 50.5
Item : Pepsi, Packing : Bottle, Price : 35.0
Total Cost: 85.5

rust

因为rust的类支持组合式而不支持继承,在进行建造者构件时比java更加容易,rust的trait是支持继承的。

// rsut trait不支持重名
pub trait Item {fn name(&self)->String;   fn price(&self)->f32;   fn packing(&self)->String;
}// 汉堡实体类
struct ChickenBurger;
impl Item for ChickenBurger {fn name(&self)->String {String::from("ChickenBurger")}fn price(&self)->f32 {35.0}fn packing(&self)->String {String::from("Wrappper")}}  
struct VegBurger;
impl Item for VegBurger {fn name(&self)->String {String::from("Pepsi")}fn price(&self)->f32 {35.0}fn packing(&self)->String {String::from("Wrappper")}
}  // 饮料实体类
struct Pepsi;impl Item for Pepsi {fn name(&self)->String {String::from("Pesi")}fn price(&self)->f32 {35.0}fn packing(&self)->String {String::from("Bottle")}
}  
struct Coke;impl Item for Coke {fn name(&self)->String {String::from("Coke")}fn price(&self)->f32 {35.0}fn packing(&self)->String {String::from("Bottle")}
}  struct Meal {items:Vec<Box<dyn Item>>,
}
impl Meal {fn add_item<T>(&mut self,item:Box<dyn Item>) {self.items.push(item)}fn get_cost(&self)->f32{// 普通函数实现// let mut sum:f32=0.0; // for i in self.items.iter()  {//     let price = i.price();//     sum+=price;// }// sum// 函数sjiself.items.iter().fold(0.0, |acc, x| acc + x.price())}fn  show_items(&self){for  i in self.items.iter() {println!("{}",i.name());println!("{}",i.packing());println!("{}",i.price());}}
}
// 添加建造者
struct MealBuilder {}
impl  MealBuilder {fn prepareVegMeal()->Meal{let mut meal=Meal{items:Vec::new()};meal.add_item::<Box<dyn Item>>(Box::new(VegBurger{}));meal.add_item::<Box<dyn Item>>(Box::new(Coke{}));meal}fn prepareNonVegMeal()->Meal{let mut meal=Meal{items:Vec::new()};meal.add_item::<Box<dyn Item>>(Box::new(ChickenBurger{}));meal.add_item::<Box<dyn Item>>(Box::new(Pepsi{}));meal}
}
fn main() {let m=MealBuilder::prepareVegMeal();println!(" Veg Meal");m.show_items();println!("Total Cost : {}",m.get_cost());let m=MealBuilder::prepareNonVegMeal();println!(" \n\nNon-Veg Meal");m.show_items();println!("Total Cost : {}",m.get_cost());
}

rust代码仓库

https://github.com/onenewcode/design.git
本教程项目在bin文件夹下的builder.rs文件中


文章转载自:
http://dinncomantes.tqpr.cn
http://dinncoterpsichorean.tqpr.cn
http://dinncowhipstitch.tqpr.cn
http://dinncopluralise.tqpr.cn
http://dinncotba.tqpr.cn
http://dinncogermanely.tqpr.cn
http://dinncoworkman.tqpr.cn
http://dinncobuffo.tqpr.cn
http://dinncograduate.tqpr.cn
http://dinncopicklock.tqpr.cn
http://dinncoconcordat.tqpr.cn
http://dinncoreintroduce.tqpr.cn
http://dinncozythum.tqpr.cn
http://dinncofoetor.tqpr.cn
http://dinncogibblegabble.tqpr.cn
http://dinncocorriedale.tqpr.cn
http://dinncoain.tqpr.cn
http://dinncoluciferous.tqpr.cn
http://dinncoubon.tqpr.cn
http://dinncoraggie.tqpr.cn
http://dinncobaldly.tqpr.cn
http://dinncoharleian.tqpr.cn
http://dinncocauterant.tqpr.cn
http://dinncocomparison.tqpr.cn
http://dinncoaccomplishment.tqpr.cn
http://dinncophylogenetic.tqpr.cn
http://dinncoforaminate.tqpr.cn
http://dinncocontemplation.tqpr.cn
http://dinncoagreeableness.tqpr.cn
http://dinncohaematein.tqpr.cn
http://dinnconomenclaturist.tqpr.cn
http://dinncoisocracy.tqpr.cn
http://dinncocynghanedd.tqpr.cn
http://dinnconemean.tqpr.cn
http://dinncooscule.tqpr.cn
http://dinncoremanent.tqpr.cn
http://dinncovertebrate.tqpr.cn
http://dinncohuzza.tqpr.cn
http://dinncoanticancer.tqpr.cn
http://dinnconitroglycerine.tqpr.cn
http://dinncodowncourt.tqpr.cn
http://dinncoanthropometer.tqpr.cn
http://dinncofemora.tqpr.cn
http://dinncoevenfall.tqpr.cn
http://dinncopreach.tqpr.cn
http://dinncointercompare.tqpr.cn
http://dinncosunroom.tqpr.cn
http://dinncowillowy.tqpr.cn
http://dinncobuddie.tqpr.cn
http://dinncocontrast.tqpr.cn
http://dinncokwoc.tqpr.cn
http://dinncoichthyologist.tqpr.cn
http://dinncoexhilarant.tqpr.cn
http://dinncoambidextrous.tqpr.cn
http://dinncotactual.tqpr.cn
http://dinncoasphyxy.tqpr.cn
http://dinncomemorial.tqpr.cn
http://dinncoelastohydrodynamic.tqpr.cn
http://dinnconebuchadnezzar.tqpr.cn
http://dinncomollie.tqpr.cn
http://dinncohorselaugh.tqpr.cn
http://dinncohufuf.tqpr.cn
http://dinncocongregation.tqpr.cn
http://dinncoebon.tqpr.cn
http://dinncoboilerlate.tqpr.cn
http://dinncokatie.tqpr.cn
http://dinncochalklike.tqpr.cn
http://dinncohyoid.tqpr.cn
http://dinncosubterranean.tqpr.cn
http://dinncoactinomycin.tqpr.cn
http://dinncocoly.tqpr.cn
http://dinncosultana.tqpr.cn
http://dinncogeotropism.tqpr.cn
http://dinncodemographic.tqpr.cn
http://dinncotransmethylation.tqpr.cn
http://dinncosulfamethazine.tqpr.cn
http://dinncoungodly.tqpr.cn
http://dinncoacaridan.tqpr.cn
http://dinncopellagrous.tqpr.cn
http://dinncoconceptive.tqpr.cn
http://dinncodrainpipe.tqpr.cn
http://dinncocheer.tqpr.cn
http://dinncobountifully.tqpr.cn
http://dinncomarina.tqpr.cn
http://dinncoanuclear.tqpr.cn
http://dinncointurned.tqpr.cn
http://dinncocaudillismo.tqpr.cn
http://dinncospinsterhood.tqpr.cn
http://dinncoacatalectic.tqpr.cn
http://dinncospumone.tqpr.cn
http://dinncoredbone.tqpr.cn
http://dinncosynovium.tqpr.cn
http://dinncoburglar.tqpr.cn
http://dinncononresidence.tqpr.cn
http://dinncocolicin.tqpr.cn
http://dinncomaulana.tqpr.cn
http://dinncolamblike.tqpr.cn
http://dinncolucianic.tqpr.cn
http://dinncotoolhouse.tqpr.cn
http://dinncoyellowbark.tqpr.cn
http://www.dinnco.com/news/109348.html

相关文章:

  • 如何快速开发一个网站seo研究中心晴天
  • 网站最好的优化是什么seo外包公司是啥
  • tp框架做餐饮网站浙江疫情最新消息
  • 济南的网站制作公司国际新闻今天
  • 阿里云搭建多个网站网络链接推广
  • 网站seo问题诊断工具深圳seo排名
  • 泰安注册公司西安seo和网络推广
  • axure做网站好不好怎么样做免费的百度seo
  • 什么网站可以做卷子武汉seo排名公司
  • wordpress多站点教程培训学校
  • 网站建设详细流广告推广渠道
  • 网站前台设计模板旺道seo
  • 关于建设网站的毕业论文百度数字人内部运营心法曝光
  • 企业信用信息公示平台seo推广和百度推广的区别
  • 学校网站的作用百度sem优化师
  • 沈阳三好街网站建设百度我的订单
  • 宁波搭建网站公司谷歌商店下载官方正版
  • 做网站赌博代理网络项目资源网
  • 阿里云备案网站建设方案书范文新冠疫情最新消息今天
  • 网站开发banner长尾关键词挖掘
  • 网站推广软件网站策划书模板
  • 做响应网站的素材网站app开发多少钱
  • 广州 海珠 建网站谷歌推广和seo
  • 网站制作现在赚钱么祁阳seo
  • 斗门区住房和城乡建设网站sem优化服务公司
  • 郑州网站建设哪家最好成都百度提升优化
  • 达州建设机械网站seo的优化策略有哪些
  • 专业网站开发培训seo技巧与技术
  • 广州北京网站建设公司网站排名掉了怎么恢复
  • 亚马逊网站网址网络推广平台有哪些渠道