ScalersTalk成长会 2018-04-22
装饰者模式:<br />1.动态的将新功能附加到对象上,在对象功能扩展方面,他比继承更具有弹性<br />2.一个基类,多个分支<br /><br />自我理解:<br />用人来举例,人有各个国家的人,这是一个分支,另一个分支就是穿着分支,冬季冷了添衣服就要添加衣服(这个黎姿不是特别合适)<br /><br />视频中的例子:<br />使用饮品来举例,coffer有各个品种的咖啡,选定主coffer还可以选定辅料,继续添加辅料,最后计算总价0<br />下面是我的代码例子,视频中的例子放在下一章
package com.wz.decorate;<br /><br />import java.math.BigDecimal;<br /><br />/**<br /> * people类,描述和计算人衣服总价<br /> *<br /> * @author Administrator<br /> * @create 2018-04-22 13:47<br /> */<br />public abstract class PeopleMain {<br /><br /> private String description;<br /><br /> private BigDecimal money = new BigDecimal(0);<br /><br /> public String getDescription() {<br /> return description;<br /> }<br /><br /> public BigDecimal getMoney() {<br /> return money;<br /> }<br /><br /> public void setDescription(String description) {<br /> this.description = description;<br /> }<br /><br /> public void setMoney(BigDecimal money) {<br /> this.money = money;<br /> }<br /><br /> public abstract BigDecimal cost();<br />}
package com.wz.decorate;<br /><br />import java.math.BigDecimal;<br /><br />/**<br /> * 国家人的分类<br /> *<br /> * @author Administrator<br /> * @create 2018-04-22 13:49<br /> */<br />public class People extends PeopleMain{<br /><br /> @Override<br /> public BigDecimal cost() {<br /> return super.getMoney();<br /> }<br />}
package com.wz.decorate;<br /><br />/**<br /> * 中国人<br /> *<br /> * @author Administrator<br /> * @create 2018-04-22 13:54<br /> */<br />public class China extends People {<br /><br /> public China() {<br /> super.setDescription("China");<br /> }<br />}
package com.wz.decorate;<br /><br />/**<br /> * 英国人<br /> *<br /> * @author Administrator<br /> * @create 2018-04-22 13:54<br /> */<br />public class Englisher extends People {<br /><br /> public Englisher() {<br /> super.setDescription("Englisher");<br /> }<br />}
package com.wz.decorate;<br /><br />import java.math.BigDecimal;<br /><br />/**<br /> * 衣服类<br /> *<br /> * @author Administrator<br /> * @create 2018-04-22 13:58<br /> */<br />public class Clothing extends PeopleMain {<br /><br /> private PeopleMain pm ;<br /><br /> public Clothing(PeopleMain pm) {<br /> this.pm = pm;<br /> }<br /><br /> @Override<br /> public String getDescription() {<br /> return super.getDescription() + "-" +pm.getDescription();<br /> }<br /><br /> @Override<br /> public BigDecimal cost() {<br /> return super.getMoney().add(pm.cost());<br /> }<br />}<br /><br />
package com.wz.decorate;<br /><br />import java.math.BigDecimal;<br /><br />/**<br /> * 裤子<br /> *<br /> * @author Administrator<br /> * @create 2018-04-22 14:08<br /> */<br />public class Pants extends Clothing {<br /><br /> public Pants(PeopleMain pm) {<br /> super(pm);<br /> super.setDescription("Pants");<br /> super.setMoney( new BigDecimal(150));<br /> }<br />}<br /><br />
package com.wz.decorate;<br /><br />import java.math.BigDecimal;<br /><br />/**<br /> * 鞋<br /> *<br /> * @author Administrator<br /> * @create 2018-04-22 14:06<br /> */<br />public class Shoe extends Clothing {<br /><br /> public Shoe(PeopleMain pm) {<br /> super(pm);<br /> super.setDescription("Show");<br /> super.setMoney(new BigDecimal(600));<br /> }<br />}
package com.wz.decorate;<br /><br />import java.math.BigDecimal;<br /><br />/**<br /> * 外套<br /> *<br /> * @author Administrator<br /> * @create 2018-04-22 14:03<br /> */<br />public class Outerwear extends Clothing {<br /><br /> public Outerwear(PeopleMain pm) {<br /> super(pm);<br /> super.setDescription("Outerwear");<br /> super.setMoney( new BigDecimal(300));<br /> }<br /><br /><br />}
package com.wz.decorate;<br /><br />/**<br /> * 测试类<br /> *<br /> * @author Administrator<br /> * @create 2018-04-22 14:10<br /> */<br />public class TestMain {<br /><br /> public static void main(String[] args) {<br /> PeopleMain pm ;<br /> pm = new Englisher();<br /> System.out.println(pm.getDescription());<br /> System.out.println(pm.cost());<br /><br /> System.out.println("==========================================");<br /><br /> pm = new China();<br /> pm = new Outerwear(pm);<br /> pm = new Pants(pm);<br /> pm = new Shoe(pm);<br /> System.out.println(pm.getDescription());<br /> System.out.println(pm.cost());<br /><br /> }<br />}