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

高新区网站建设的建议点击排名优化

高新区网站建设的建议,点击排名优化,seo和sem的关系为负关系,可以做翻译兼职的网站吗装饰模式装饰模型装饰模式示例商场收银程序(简单工厂策略装饰模式实现)装饰模式总结装饰模型 装饰模式(Decorator),动态地给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更为…

装饰模式

  • 装饰模型
  • 装饰模式示例
  • 商场收银程序(简单工厂+策略+装饰模式实现)
  • 装饰模式总结

装饰模型

装饰模式(Decorator),动态地给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更为灵活。
在这里插入图片描述
Component 是定义一个对象接口,可以给这些对象动态地添加职责。
ConcreateComponent 是定义了一个具体的对象,也可以给这个对象添加一些职责。
Decorator,装饰抽象类,继承了 Component,从外类来扩展 Component 类的功能,但对应Component 来说,是无须知道 Decorator的存在。
ConcreateDecorator 就是具体的装饰对象,起到给 Component 添加职责的功能。

// Component类
abstract class Component {public abstract void Operation();
}
// ConcreteComponent类
class ConcreteComponent extends Component {public void Operation() {System.out.println("具体对象的实际操作");}
}
//Decorator类
abstract class Decorator extends Component {protected Component component;//装饰一个Component对象public void SetComponent(Component component) {this.component = component;}//重写Operation(),实际调用component的Operation方法public void Operation() {if (component != null) {component.Operation();}}
}
//ConcreteDecoratorA类
class ConcreteDecoratorA extends Decorator {//本类独有子段,以区别于ConcreteDecoratorB类private String addedState;public void Operation() {//首先运行了原有Component的Operation()super.Operation();this.addedState = "具体装饰对象A的独有操作";//再执行本类独有功能System.out.println(this.addedState);}
}
//ConcreteDecoratorB类
class ConcreteDecoratorB extends Decorator {public void Operation() {//首先运行了原有Component的Operation()super.Operation();//再执行本类独有功能this.AddedBehavior();}//本类独有方法,以区别于ConcreteDecoratorA类private void AddedBehavior() { System.out.println("具体装饰对象B的独有操作");}
}

客户端代码

public class Test {public static void main(String[] args){System.out.println("**********************************************");		System.out.println("《大话设计模式》代码样例");System.out.println();		ConcreteComponent c = new ConcreteComponent();ConcreteDecoratorA d1 = new ConcreteDecoratorA();ConcreteDecoratorB d2 = new ConcreteDecoratorB();//首先用d1来包装c,扩展对象c的方法d1.SetComponent(c);	//再用有来包装d1,扩展对象d1的方法d2.SetComponent(d1);//最终执行d2的Operation()d2.Operation();   	System.out.println();System.out.println("**********************************************");}
}

装饰模式利用SetComponent来对对象进行包装,这样每个装饰对象的实现和如何使用这个对象分离开了,每个装饰对象只关心自己的功能,不需要关心如何被添加到对象链中。

如果只有一个ConcreteComponent 类而没有抽象的 Component 类,那么 Decorator 类可以是 ConcreteComponent 的一个子类。
同样道理,如果只要一个 ConcreteDecorator 类,那么就没有必要建立一个单独的 Decorator 类,而可以把 Decorator 和 ConcreteDecorator 的责任合并成一个类。

装饰模式示例

在这里插入图片描述
人物形象接口

public interface ICharacter {public void show();
}

具体人类

public class Person implements ICharacter {private String name;public Person(String name) {this.name = name;}public void show() {System.out.println("装扮的"+name);}
}

服饰类

public class Finery implements ICharacter {protected ICharacter component;public void decorate(ICharacter component) {this.component=component;}public void show() {if (this.component != null){this.component.show();}}
}

具体服饰类(ConcreteDecorator)

public class LeatherShoes extends Finery {public void show(){System.out.print(" 皮鞋");super.show();}
}
public class BigTrouser extends Finery {public void show(){System.out.print(" 垮裤");super.show();}
}
public class TShirts extends Finery {public void show(){System.out.print(" 大T恤");super.show();}
}
public class Tie extends Finery {public void show(){System.out.print(" 领带");super.show();}
}
public class Suit extends Finery {public void show(){System.out.print(" 西装");super.show();}
}
public class Strawhat extends Finery {public void show(){System.out.print(" 草帽");super.show();}
}
public class Sneakers extends Finery {public void show(){System.out.print(" 球鞋");super.show();}
}

客户端代码

public class Test {public static void main(String[] args){System.out.println("**********************************************");		System.out.println("《大话设计模式》代码样例");System.out.println();		Person xc = new Person("小菜");System.out.println(" 第一种装扮:");Sneakers pqx = new Sneakers();      //生成球鞋实例pqx.decorate(xc);                   //球鞋装饰小菜BigTrouser kk = new BigTrouser();   //生成垮裤实例kk.decorate(pqx);                   //垮裤装饰“有球鞋装饰的小菜”TShirts dtx = new TShirts();        //生成T恤实例dtx.decorate(kk);                   //T恤装饰“有垮裤球鞋装饰的小菜”dtx.show();                         //执行形象展示System.out.println(" 第二种装扮:");LeatherShoes px = new LeatherShoes();//生成皮鞋实例px.decorate(xc);                    //皮鞋装饰小菜Tie ld = new Tie();                 //生成领带实例ld.decorate(px);                    //领带装饰“有皮鞋装饰的小菜”Suit xz = new Suit();               //生成西装实例xz.decorate(ld);                    //西装装饰“有领带皮鞋装饰的小菜”xz.show();                          //执行形象展示System.out.println(" 第三种装扮:");Sneakers pqx2 = new Sneakers();     //生成球鞋实例pqx2.decorate(xc);                  //球鞋装饰小菜LeatherShoes px2 = new LeatherShoes();//生成皮鞋实例px2.decorate(pqx2);                 //皮鞋装饰“有球鞋装饰的小菜”BigTrouser kk2 = new BigTrouser();  //生成垮裤实例kk2.decorate(px2);                  //垮裤装饰“有皮鞋球鞋装饰的小菜”Tie ld2 = new Tie();                //生成领带实例ld2.decorate(kk2);                  //领带装饰“有垮裤皮鞋球鞋装饰的小菜”Strawhat cm2 = new Strawhat();      //生成草帽实例cm2.decorate(ld2);                  //草帽装饰“有领带垮裤皮鞋球鞋装饰的小菜”cm2.show();                         //执行形象展示System.out.println();System.out.println("**********************************************");}
}

商场收银程序(简单工厂+策略+装饰模式实现)

在这里插入图片描述
ISale接口:用作装饰模式里的 Component。
CashNormal: 用作装饰模式里的 ConcreteComponent。
CashSuper: 用作装饰模式里的 Decorator 。
CashReturn:用作装饰模式里的 ConcreateDecorator 。

public interface ISale {public double acceptCash(double price,int num);
}
// 正常收费,原价返回
public class CashNormal implements ISale {public double acceptCash(double price,int num){return price * num;}
}
public class CashSuper implements ISale {protected ISale component;//装饰对象public void decorate(ISale component) {this.component=component;}public double acceptCash(double price,int num){var result = 0d;if (this.component != null){//若装饰对象存在,则执行装饰的算法运算result = this.component.acceptCash(price,num);    }return result;}
}
public class CashRebate extends CashSuper {private double moneyRebate = 1d;//打折收费。初始化时必需输入折扣率。八折就输入0.8public CashRebate(double moneyRebate){this.moneyRebate = moneyRebate;}//计算收费时需要在原价基础上乘以折扣率public double acceptCash(double price,int num){double result = price * num * this.moneyRebate;return super.acceptCash(result,1);}
}public class CashReturn extends CashSuper {private double moneyCondition = 0d; //返利条件private double moneyReturn = 0d;    //返利值//返利收费。初始化时需要输入返利条件和返利值。//比如“满300返100”,就是moneyCondition=300,moneyReturn=100public CashReturn(double moneyCondition,double moneyReturn){this.moneyCondition = moneyCondition;this.moneyReturn = moneyReturn;}//计算收费时,当达到返利条件,就原价减去返利值public double acceptCash(double price,int num){double result = price * num;if (moneyCondition>0 && result >= moneyCondition)result = result - Math.floor(result / moneyCondition) * moneyReturn; return super.acceptCash(result,1);   }   
}
public class CashContext {private ISale cs;   //声明一个ISale接口对象    //通过构造方法,传入具体的收费策略public CashContext(int cashType){switch(cashType){case 1:this.cs = new CashNormal();break;case 2:this.cs = new CashRebate(0.8d);break;case 3:this.cs = new CashRebate(0.7d);break;case 4:this.cs = new CashReturn(300d,100d);break;case 5://先打8折,再满300返100// 装饰模式CashNormal cn = new CashNormal();CashReturn cr1 = new CashReturn(300d,100d); CashRebate cr2 = new CashRebate(0.8d);cr1.decorate(cn);   //用满300返100算法包装基本的原价算法cr2.decorate(cr1);  //打8折算法装饰满300返100算法this.cs = cr2;      //将包装好的算法组合引用传递给cs对象break;case 6://先满200返50,再打7折// 装饰模式CashNormal cn2 = new CashNormal();CashRebate cr3 = new CashRebate(0.7d);CashReturn cr4 = new CashReturn(200d,50d); cr3.decorate(cn2);  //用打7折算法包装基本的原价算法cr4.decorate(cr3);  //满200返50算法装饰打7折算法this.cs = cr4;      //将包装好的算法组合引用传递给cs对象break;}}public double getResult(double price,int num){// 根据收费策略的不同,获得计算结果return this.cs.acceptCash(price,num);}    
}

客户端代码

public class Test {public static void main(String[] args){System.out.println("**********************************************");		System.out.println("《大话设计模式》代码样例");System.out.println();		int discount = 0; 		//商品折扣模式double price = 0d; 		//商品单价int num = 0;			//商品购买数量double totalPrices = 0d;//当前商品合计费用double total = 0d;		//总计所有商品费用Scanner sc = new Scanner(System.in);do {System.out.println("商品折扣模式如下:");	System.out.println("1.正常收费");	System.out.println("2.打八折");	System.out.println("3.打七折");	System.out.println("4.满300送100");	System.out.println("5.先打8折,再满300送100");	System.out.println("6.先满200送50,再打7折");	System.out.println("请输入商品折扣模式:");	discount = Integer.parseInt(sc.nextLine());System.out.println("请输入商品单价:");	price = Double.parseDouble(sc.nextLine());System.out.println("请输入商品数量:");	num = Integer.parseInt(sc.nextLine());System.out.println();	if (price>0 && num>0){//根据用户输入,将对应的策略对象作为参数传入CashContext对象中CashContext cc = new CashContext(discount);//通过Context的getResult方法的调用,可以得到收取费用的结果//让具体算法与客户进行了隔离totalPrices = cc.getResult(price,num);total = total + totalPrices;System.out.println();	System.out.println("单价:"+ price + "元 数量:"+ num +" 合计:"+ totalPrices +"元");	System.out.println();System.out.println("总计:"+ total+"元");	System.out.println();}}while(price>0 && num>0);System.out.println();System.out.println("**********************************************");}
}

装饰模式总结

装饰模式是为已有功能动态地添加更多功能的一种方式。

当系统需要新功能的时候,是向旧的类中添加新的代码。这些新加的代码通常装饰了原有类的核心职责或主要行为,但这种做法的问题在于,它们在主类中加入了新的字段,新的方法和新的逻辑,从而增加了主类的复杂度。
装饰模式却提供了一个非常好的解决方案,它把每个要装饰的功能放在单独的类中,并让这个类包装它所要装饰的对象,因此,当需要执行特殊行为时,客户代码就可以在运行时根据需要有选择地、按顺序地使用装饰功能包装对象了。

把类中的装饰功能从类中搬移去除,这样可以简化原有的类。这样做的好处就是有效地把类的核心职责和装饰功能区分开了。

http://www.mmbaike.com/news/35515.html

相关文章:

  • wordpress移动端投放广告西安seo哪家好
  • html5 微网站 免费搜索引擎大全网址
  • 推荐网站建设的书google搜索优化方法
  • 扬州网站制作百家号关键词seo优化
  • 展示型外贸网站建设王通seo赚钱培训
  • 工商注册信息查询系统seo是什么意思 为什么要做seo
  • 17网站一起做网店档口出租兰州怎么提高网站的排名
  • 做网站开发用sublime好吗seo的作用是什么
  • 佛山高明网站建设设计泉州关键词排名
  • 义乌外贸建站seo推广经验
  • 深圳品牌网站制作公司电商软文范例300字
  • 手机网站判断跳转代码百度推广怎么优化关键词的质量
  • 深圳网站建设迅美潍坊seo培训
  • 自学电商运营教程墨子学院seo
  • 政府网站集约化建设试点工作方案宁波百度快照优化排名
  • 外贸自己做网站好不好汕头百度网站推广
  • 做网站企业的发展前景网络营销的推广
  • 拔别人的网站做网站合法吗黑龙江头条今日新闻
  • 国内卡一卡二卡三网站视频营销型企业网站建设的内容
  • 有什么推广网站市场调研报告的基本框架
  • 网站建设 sheji021西安网站制作费用
  • 太原网站建设口碑推荐广告软文范例200字
  • 网站建设前准备工作谷歌seo站内优化
  • ruby on rails社区网站开发关键词挖掘啊爱站网
  • php中英文网站模板seo优化工具哪个好
  • 做网站到内容填充需要多久推广软文营销案例
  • 上海的外贸网站建设公司营销团队找产品合作
  • google seo 营销网站制作网站的软件
  • 新余哪有做网站的公司第一设计
  • 免费新闻网站建设黄冈网站推广软件费用是多少