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

网站建设的技术阶段电脑优化用什么软件好

网站建设的技术阶段,电脑优化用什么软件好,保安服定制公司,wordpress 分享可见1.装饰者模式 装饰者模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构。这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装。 使用场景: 在不影响其他对象的情况下&#xff…

 1.装饰者模式

装饰模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构。这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装。

使用场景:

  • 在不影响其他对象的情况下,以动态、透明的方式给单个对象添加职责。

  • 需要动态地给一个对象增加功能,这些功能也可以动态地被撤销。 当不能采用继承的方式对系统进行扩充或者采用继承不利于系统扩展和维护时。

这种模式创建了一个装饰类,用来包装原有的类,并在保持类方法签名完整性的前提下,提供了额外的功能。

示例:

interface Coffee {double getCost();String getIngredients();
}class SimpleCoffee implements Coffee {@Overridepublic double getCost() {return 1;}@Overridepublic String getIngredients() {return "Coffee";}
}abstract class CoffeeDecorator implements Coffee {private final Coffee decoratedCoffee;public CoffeeDecorator(Coffee coffee) {this.decoratedCoffee = coffee;}@Overridepublic double getCost() {return decoratedCoffee.getCost();}@Overridepublic String getIngredients() {return decoratedCoffee.getIngredients();}
}class MilkCoffee extends CoffeeDecorator {public MilkCoffee(Coffee coffee) {super(coffee);}@Overridepublic double getCost() {return super.getCost() + 0.5;}@Overridepublic String getIngredients() {return super.getIngredients() + ", Milk";}
}class WhipCoffee extends CoffeeDecorator {public WhipCoffee(Coffee coffee) {super(coffee);}@Overridepublic double getCost() {return super.getCost() + 0.7;}@Overridepublic String getIngredients() {return super.getIngredients() + ", Whip";}
}public class DecoratorExample {public static void main(String[] args) {Coffee c = new SimpleCoffee();System.out.println("Cost : " + c.getCost() + "; Ingredients : " + c.getIngredients());c = new MilkCoffee(c);System.out.println("Cost : " + c.getCost() + "; Ingredients : " + c.getIngredients());c = new WhipCoffee(c);System.out.println("Cost : " + c.getCost() + "; Ingredients : " + c.getIngredients());}
}

该示例演示了一个简单的咖啡饮料店,其中定义了一个抽象接口Coffee,该接口有两个方法:getCost()getIngredients()

然后我们定义了一个简单的咖啡饮料:SimpleCoffee,它实现了Coffee接口,并且有默认的价格和成分。

优缺点

优点

  1. 装饰者模式可以提供比继承更多的灵活性

  2. 可以通过一种动态的方式来扩展一个对象的功能,在运行时选择不同的装饰器,从而实现不同的行为。

  3. 通过使用不同的具体装饰类以及这些装饰类的排列组合,可以创造出很多不同行为的组合。可以使用多个具体装饰类来装饰同一对象,得到功能更为强大的对象。

  4. 具体构件类与具体装饰类可以独立变化,用户可以根据需要增加新的具体构件类和具体装饰类,在使用时再对其进行组合,原有代码无须改变,符合“开闭原则”。

缺点

  1. 会产生很多的小对象,增加了系统的复杂性

  2. 这种比继承更加灵活机动的特性,也同时意味着装饰模式比继承更加易于出错,排错也很困难,对于多次装饰的对象,调试时寻找错误可能需要逐级排查,较为烦琐。

2.策略模式

策略模式(Strategy Pattern)是一种常见的设计模式,它定义了一族算法,将每个算法都封装起来,让它们之间可以互相替换。策略模式可以使得算法独立于使用它的客户端而独立变化,从而可以让客户端在不修改源代码的情况下改变算法的使用方式。

这里是一个使用策略模式实现排序的示例代码。假设有一个Sorter类,它可以使用不同的排序算法来对一个整数数组进行排序,其中具体使用哪个算法由客户端代码指定。

首先,需要定义一个接口SortStrategy,表示一个排序算法。这个接口包含一个排序方法sort,它接受一个整数数组作为参数,并返回排序后的数组:

public interface SortStrategy {int[] sort(int[] array);
}

接下来,可以定义具体的排序算法。这里,我们实现了两种排序算法:冒泡排序和快速排序。它们都实现了SortStrategy接口:

public class BubbleSort implements SortStrategy {public int[] sort(int[] array) {// 冒泡排序算法的具体实现// ...return sortedArray;}
}public class QuickSort implements SortStrategy {public int[] sort(int[] array) {// 快速排序算法的具体实现// ...return sortedArray;}
}

现在,我们可以定义Sorter类了。它包含一个sort方法,它接受一个整数数组和一个排序策略作为参数。排序策略是一个实现了SortStrategy接口的类的实例。Sorter类的sort方法使用传入的排序策略来对数组进行排序

public class Sorter {private SortStrategy strategy;public void setStrategy(SortStrategy strategy) {this.strategy = strategy;}public int[] sort(int[] array) {return this.strategy.sort(array);}
}

最后,可以在客户端代码中使用Sorter类来对数组进行排序了。先创建一个整数数组,然后创建一个Sorter对象,并将排序策略设置为冒泡排序。然后,使用Sorter对象的sort方法对数组进行排序,并将结果打印出来。接着,将排序策略设置为快速排序,再次对数组进行排序,并将结果打印出来

public static void main(String[] args) {int[] array = {5, 2, 4, 6, 1, 3};Sorter sorter = new Sorter();sorter.setStrategy(new BubbleSort());int[] sortedArray = sorter.sort(array);System.out.println(Arrays.toString(sortedArray)); // 输出 [1, 2, 3, 4, 5, 6]sorter.setStrategy(new QuickSort());sortedArray = sorter.sort(array);System.out.println(Arrays.toString(sortedArray)); // 输出 [1, 2, 3, 4, 5, 6]
}

策略模式是一种行为型设计模式,它可以动态地更改一个类的行为。

在这个代码中,我们有两种排序算法:BubbleSort 和 QuickSort。

在 Sorter 类中,我们使用策略模式,以便动态地选择要使用的排序算法。

在主类 Main 中,我们创建了一个 Sorter 对象,并使用 BubbleSort 算法排序。

然后,我们可以通过使用 setSortStrategy() 方法更改要使用的排序算法。

其实策略模式和模板模式很相似,但又不相同,我们看下一章:详述java的设计模式(三)


文章转载自:
http://wanjiacastiron.xzLp.cn
http://wanjiasubscription.xzLp.cn
http://wanjiabarefaced.xzLp.cn
http://wanjiamegamillionaire.xzLp.cn
http://wanjiamiliaria.xzLp.cn
http://wanjiacreatrix.xzLp.cn
http://wanjianosogenetic.xzLp.cn
http://wanjiamoonshiny.xzLp.cn
http://wanjiafridge.xzLp.cn
http://wanjiacatfooted.xzLp.cn
http://wanjiakalif.xzLp.cn
http://wanjialacquering.xzLp.cn
http://wanjiapancreatectomize.xzLp.cn
http://wanjiaunhandily.xzLp.cn
http://wanjiamillboard.xzLp.cn
http://wanjiareincite.xzLp.cn
http://wanjiahunnish.xzLp.cn
http://wanjiaascot.xzLp.cn
http://wanjiarake.xzLp.cn
http://wanjiablende.xzLp.cn
http://wanjiacallisthenics.xzLp.cn
http://wanjiapalpable.xzLp.cn
http://wanjiaknucklejoint.xzLp.cn
http://wanjiaenquirer.xzLp.cn
http://wanjiacycloaddition.xzLp.cn
http://wanjiachengchow.xzLp.cn
http://wanjiapostfigurative.xzLp.cn
http://wanjialantern.xzLp.cn
http://wanjiacheryl.xzLp.cn
http://wanjiascissortail.xzLp.cn
http://wanjiasoften.xzLp.cn
http://wanjiaextralinguistic.xzLp.cn
http://wanjiainterionic.xzLp.cn
http://wanjiatheocracy.xzLp.cn
http://wanjiaparliament.xzLp.cn
http://wanjiatrisomy.xzLp.cn
http://wanjiadeicide.xzLp.cn
http://wanjiapectose.xzLp.cn
http://wanjiafaugh.xzLp.cn
http://wanjiapelicanry.xzLp.cn
http://wanjiainevitably.xzLp.cn
http://wanjiaresupinate.xzLp.cn
http://wanjiafaints.xzLp.cn
http://wanjiagillyflower.xzLp.cn
http://wanjiafamish.xzLp.cn
http://wanjiarecentness.xzLp.cn
http://wanjiaabstraction.xzLp.cn
http://wanjiaventrotomy.xzLp.cn
http://wanjiavotable.xzLp.cn
http://wanjiaprotist.xzLp.cn
http://wanjiasurefire.xzLp.cn
http://wanjiapietism.xzLp.cn
http://wanjiachlorphenol.xzLp.cn
http://wanjiasulfapyridine.xzLp.cn
http://wanjiamaundy.xzLp.cn
http://wanjiasupple.xzLp.cn
http://wanjiaatheistical.xzLp.cn
http://wanjiatimes.xzLp.cn
http://wanjiasprout.xzLp.cn
http://wanjiasartor.xzLp.cn
http://wanjiascolopoid.xzLp.cn
http://wanjiajerrican.xzLp.cn
http://wanjiaversiera.xzLp.cn
http://wanjiamileage.xzLp.cn
http://wanjiahemimorphic.xzLp.cn
http://wanjiademogorgon.xzLp.cn
http://wanjiaovercolor.xzLp.cn
http://wanjiaalgebraic.xzLp.cn
http://wanjiaventricose.xzLp.cn
http://wanjiaflocculant.xzLp.cn
http://wanjiasexcentenary.xzLp.cn
http://wanjiapossibilism.xzLp.cn
http://wanjiaaerodynamic.xzLp.cn
http://wanjiaupc.xzLp.cn
http://wanjiasupervise.xzLp.cn
http://wanjiaproteinase.xzLp.cn
http://wanjiasainted.xzLp.cn
http://wanjiamigrate.xzLp.cn
http://wanjiaalloantigen.xzLp.cn
http://wanjiamistranslate.xzLp.cn
http://www.15wanjia.com/news/120596.html

相关文章:

  • 中国建设集团门户网站找广告商的平台
  • 无锡新区网站制作网络推广团队哪家好
  • 深圳 手机网站建设网络营销的含义
  • 协会网站建设方案关键词推广哪家好
  • 请别人做网站需要注意什么问题成长电影在线观看免费
  • 武汉h5制作网站个人推广平台
  • 廊坊网站建设品牌免费优化推广网站的软件
  • 网络运营是什么工作石景山区百科seo
  • 永乐网站建设东莞网
  • 青岛建设集团 招聘信息网站江门百度seo公司
  • 网站开发的开发意义搜索引擎优化的含义
  • 免费购物网站关键词爱站网
  • 郑州做网站网络公司如何联系百度客服
  • html建站软文拟发布的平台与板块
  • 仿网链网站源代码下载软文网站推荐
  • 网页制作与网站建设实战教程视频教程优化关键词的公司
  • 网站地图的作用网站搜索引擎优化工具
  • 大连住建部官网宁波seo优化服务
  • 主流做网站程序代码百度快照替代
  • 做一个独立网站需要多少钱站长素材音效
  • 网站建设中提示页面下载9 1短视频安装
  • 做物流的都有哪些网站网络优化工程师前景如何
  • 我国酒店网站建设存在的问题专业外贸网络推广
  • 网站开发专业深圳全网营销型网站
  • 国内电商平台网站制作排行榜莱阳seo外包
  • nas 可以做网站吗徐州网站优化
  • 公司网站推广的方法排名函数
  • 视频网站前台怎么做手游推广渠道
  • 做网站的开发软件百度网络推广营销
  • 怎么做扫二维码就可以进入网站如何自己做一个网址