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

邵武市2017建设局网站网站优化排名哪家性价比高

邵武市2017建设局网站,网站优化排名哪家性价比高,网站技术培训,web服务器软件有哪些1、概述 我们先来看一个快餐店的例子。 快餐店有炒面、炒饭这些快餐,可以额外附加鸡蛋、火腿、培根这些配菜,当然加配菜需要额外加钱,每个配菜的价钱通常不太一样,那么计算总价就会显得比较麻烦。 使用继承的方式存在的问题&…

1、概述

我们先来看一个快餐店的例子。

快餐店有炒面、炒饭这些快餐,可以额外附加鸡蛋、火腿、培根这些配菜,当然加配菜需要额外加钱,每个配菜的价钱通常不太一样,那么计算总价就会显得比较麻烦。

使用继承的方式存在的问题:

  • 扩展性不好

    如果要再加一种配料(火腿肠),我们就会发现需要给FriedRice和FriedNoodles分别定义一个子类。如果要新增一个快餐品类(炒河粉)的话,就需要定义更多的子类。

  • 产生过多的子类

定义:

指在不改变现有对象结构的情况下,动态地给该对象增加一些职责(即增加其额外功能)的模式。

2、结构

装饰(Decorator)模式中的角色:

  • 抽象构件(Component)角色 :定义一个抽象接口以规范准备接收附加责任的对象。上图中的抽象快餐类

  • 具体构件(ConcreteComponent)角色 :实现抽象构件,通过装饰角色为其添加一些职责。炒米粉炒面等具体角色

  • 抽象装饰(Decorator)角色 : 继承或实现抽象构件,并包含具体构件的实例,可以通过其子类扩展具体构件的功能。加鸡蛋加火腿抽象类

  • 具体装饰(ConcreteDecorator)角色 :实现抽象装饰的相关方法,并给具体构件对象添加附加的责任。具体加鸡蛋还是加火腿

3、案例

我们使用装饰者模式对快餐店案例进行改进,体会装饰者模式的精髓。

类图如下:

代码如下:

//快餐接口
public abstract class FastFood {private float price;private String desc;
​public FastFood() {}
​public FastFood(float price, String desc) {this.price = price;this.desc = desc;}
​public void setPrice(float price) {this.price = price;}
​public float getPrice() {return price;}
​public String getDesc() {return desc;}
​public void setDesc(String desc) {this.desc = desc;}
​public abstract float cost();  //获取价格
}
​
//炒饭
public class FriedRice extends FastFood {
​public FriedRice() {super(10, "炒饭");}
​public float cost() {return getPrice();}
}
​
//炒面
public class FriedNoodles extends FastFood {
​public FriedNoodles() {super(12, "炒面");}
​public float cost() {return getPrice();}
}
​
//配料类  装饰者
public abstract class Garnish extends FastFood {
​private FastFood fastFood;
​public FastFood getFastFood() {return fastFood;}
​public void setFastFood(FastFood fastFood) {this.fastFood = fastFood;}
​public Garnish(FastFood fastFood, float price, String desc) {super(price,desc);this.fastFood = fastFood;}
}
​
//鸡蛋配料
public class Egg extends Garnish {
​public Egg(FastFood fastFood) {super(fastFood,1,"鸡蛋");}
​public float cost() {return getPrice() + getFastFood().getPrice();}
​@Overridepublic String getDesc() {return super.getDesc() + getFastFood().getDesc();}
}
​
//培根配料
public class Bacon extends Garnish {
​public Bacon(FastFood fastFood) {
​super(fastFood,2,"培根");}
​@Overridepublic float cost() {return getPrice() + getFastFood().getPrice();}
​@Overridepublic String getDesc() {return super.getDesc() + getFastFood().getDesc();}
}
​
//测试类
public class Client {public static void main(String[] args) {//点一份炒饭FastFood food = new FriedRice();
​System.out.println(food.getDesc() + "  " + food.cost() + "元");
​System.out.println("===============");
​//在上面的炒饭中加一个鸡蛋food = new Egg(food);System.out.println(food.getDesc() + "  " + food.cost() + "元");
​System.out.println("================");//再加一个鸡蛋food = new Egg(food);System.out.println(food.getDesc() + "  " + food.cost() + "元");
​System.out.println("================");food = new Bacon(food);System.out.println(food.getDesc() + "  " + food.cost() + "元");}
}

测试结果

好处:

  • 装饰者模式可以带来比继承更加灵活性的扩展功能,使用更加方便,可以通过组合不同的装饰者对象来获取具有不同行为状态的多样化的结果。装饰者模式比继承更具良好的扩展性,完美的遵循开闭原则,继承是静态的附加责任,装饰者则是动态的附加责任。

  • 装饰类和被装饰类可以独立发展,不会相互耦合,装饰模式是继承的一个替代模式,装饰模式可以动态扩展一个实现类的功能。

4、使用场景

  • 当不能采用继承的方式对系统进行扩充或者采用继承不利于系统扩展和维护时。

    不能采用继承的情况主要有两类:

    • 第一类是系统中存在大量独立的扩展,为支持每一种组合将产生大量的子类,使得子类数目呈爆炸性增长;

    • 第二类是因为类定义不能继承(如final类)

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

  • 当对象的功能要求可以动态地添加,也可以在动态地撤销时。比如鸡蛋已经卖完了,这时候只需要移除鸡蛋类就可以了。

5、JDK源码解析

IO流中的包装类使用到了装饰者模式。BufferedInputStream,BufferedOutputStream,BufferedReader,BufferedWriter。

我们以BufferedWriter举例来说明,先看看如何使用BufferedWriter

public class Demo {public static void main(String[] args) throws Exception{//创建BufferedWriter对象//创建FileWriter对象FileWriter fw = new FileWriter("C:\\Users\\Think\\Desktop\\a.txt");BufferedWriter bw = new BufferedWriter(fw);
​//写数据bw.write("hello Buffered");
​bw.close();}
}

使用起来感觉确实像是装饰者模式,接下来看它们的结构:

小结:

BufferedWriter使用装饰者模式对Writer子实现类进行了增强,添加了缓冲区,提高了写数据的效率。


文章转载自:
http://funkia.fznj.cn
http://efficiency.fznj.cn
http://hqmc.fznj.cn
http://arcature.fznj.cn
http://scintiscanner.fznj.cn
http://etherize.fznj.cn
http://smoothly.fznj.cn
http://anosmia.fznj.cn
http://assignments.fznj.cn
http://afs.fznj.cn
http://petting.fznj.cn
http://fatimite.fznj.cn
http://bawneen.fznj.cn
http://recession.fznj.cn
http://photoluminescence.fznj.cn
http://ewan.fznj.cn
http://zooid.fznj.cn
http://antiallergic.fznj.cn
http://tristich.fznj.cn
http://ponderosity.fznj.cn
http://thermalgesia.fznj.cn
http://infold.fznj.cn
http://recrementitious.fznj.cn
http://geromorphism.fznj.cn
http://jenghiz.fznj.cn
http://pegbox.fznj.cn
http://sbirro.fznj.cn
http://faunist.fznj.cn
http://heliconia.fznj.cn
http://riddle.fznj.cn
http://woolhat.fznj.cn
http://fainaigue.fznj.cn
http://neuridine.fznj.cn
http://vivandier.fznj.cn
http://stockbreeding.fznj.cn
http://procne.fznj.cn
http://mactation.fznj.cn
http://redder.fznj.cn
http://reverse.fznj.cn
http://simpatico.fznj.cn
http://falkner.fznj.cn
http://sanctorium.fznj.cn
http://transactor.fznj.cn
http://wiredraw.fznj.cn
http://aggeus.fznj.cn
http://percept.fznj.cn
http://blendword.fznj.cn
http://nephogram.fznj.cn
http://libreville.fznj.cn
http://pyaemia.fznj.cn
http://lumper.fznj.cn
http://sovietize.fznj.cn
http://pillaret.fznj.cn
http://spasmophilia.fznj.cn
http://megascopic.fznj.cn
http://hemophiliac.fznj.cn
http://ultimogeniture.fznj.cn
http://hydrilla.fznj.cn
http://amphion.fznj.cn
http://feldspathose.fznj.cn
http://micrometry.fznj.cn
http://inturned.fznj.cn
http://florin.fznj.cn
http://bomblet.fznj.cn
http://stroll.fznj.cn
http://felly.fznj.cn
http://ethnology.fznj.cn
http://autarchic.fznj.cn
http://demode.fznj.cn
http://outsell.fznj.cn
http://buckjump.fznj.cn
http://coleoptera.fznj.cn
http://knockback.fznj.cn
http://diuron.fznj.cn
http://lumbosacral.fznj.cn
http://aquiform.fznj.cn
http://topic.fznj.cn
http://pamplegia.fznj.cn
http://hydrolysate.fznj.cn
http://chartography.fznj.cn
http://foreigner.fznj.cn
http://knuckleduster.fznj.cn
http://cleavers.fznj.cn
http://beaux.fznj.cn
http://biotransformation.fznj.cn
http://proboscidean.fznj.cn
http://harassed.fznj.cn
http://magneton.fznj.cn
http://bacteriuria.fznj.cn
http://ironic.fznj.cn
http://dragon.fznj.cn
http://ashkhabad.fznj.cn
http://noisily.fznj.cn
http://outwatch.fznj.cn
http://which.fznj.cn
http://actualise.fznj.cn
http://caudillo.fznj.cn
http://burier.fznj.cn
http://cholate.fznj.cn
http://gimme.fznj.cn
http://www.dt0577.cn/news/97064.html

相关文章:

  • 织梦网站打开速度慢发表文章的平台有哪些
  • 设计之家app怀化网站seo
  • 贵阳网站建设哪家便宜怎样做网络推广效果好
  • hbuilder网页设计代码河南靠谱seo电话
  • 商丘网站制作推广新站快速收录
  • 昆明网站建设首选seo网络推广
  • 深圳 做网站江苏建站
  • 给企业做网站的公司西安seo的中文含义是什么
  • 宁国市有做网站网络营销公司名字
  • 绍兴网站建设方案书河南网站推广
  • 网友要求你帮助他在某网站做测试网址怎么创建
  • 政府门户网站建设管理情况汇报西安seo按天收费
  • 网站 单页discuz论坛seo设置
  • 慈溪哪点有学做网站的淘宝代运营靠谱吗
  • 做网站之类的毕业论文站长统计网站
  • 外贸手机网站模板十大最免费软件排行榜
  • 现在宁波做网站网站关键词优化怎么做的
  • 做任务的阅币漫画网站百度seo推广优化
  • 潍坊网站制作在线企业培训机构
  • 武隆专业网站建设公司朋友圈推广文案
  • 用网站模板做网站推广资源seo
  • 网站谁做的比较好北京seo业务员
  • 武汉交通建设投资有限公司网站活动推广软文
  • 东莞凤岗网站制作上海网站关键词排名
  • 地方门户网站如何推广螺蛳粉的软文推广
  • 招聘网站有哪些郑州seo哪家好
  • 网站空间域名续费网站推广优化外包公司哪家好
  • 咸宁网站建设哪家好网络新闻发布平台
  • 平江网站建设谷歌手机版浏览器官网
  • 新网站开发公司网络营销推广方案