Drink.java
package com.godv.decorator;
public abstract class Drink {
private String name;
private float price;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public abstract float getCost();
public abstract String getDes();
}
Coffee.java
package com.godv.decorator;
public class Coffee extends Drink {
@Override
public float getCost() {
// TODO Auto-generated method stub
return super.getPrice();
}
@Override
public String getDes() {
// TODO Auto-generated method stub
return super.getName();
}
}
MeishiCoffee.java
package com.godv.decorator;
public class MeishiCoffee extends Coffee {
public MeishiCoffee() {
setName(" 美式咖啡 ");
setPrice(2.0f);
}
}
YidaliCoffee.java
package com.godv.decorator;
public class YidaliCoffee extends Coffee {
public YidaliCoffee() {
setName(" 意大利咖啡 ");
setPrice(1.0f);
}
}
Decorator.java
package com.godv.decorator;
//装饰者类
public class Decorator extends Drink {
private Drink drink;
public Decorator(Drink drink) {
this.drink = drink;
}
//注意此处的写法
@Override
public float getCost() {
// TODO Auto-generated method stub
return drink.getCost() + super.getPrice();
}
//注意此处的写法
@Override
public String getDes() {
// TODO Auto-generated method stub
return drink.getDes() + " && " + super.getName();
}
}
Milk.java
package com.godv.decorator;
public class Milk extends Decorator {
public Milk(Drink drink) {
super(drink);
// TODO Auto-generated constructor stub
setName(" 牛奶 ");
setPrice(0.5f);
}
}
Water.java
package com.godv.decorator;
public class Water extends Decorator {
public Water(Drink drink) {
super(drink);
// TODO Auto-generated constructor stub
setName(" 水 ");
setPrice(1.0f);
}
}
Clinet.java
package com.godv.decorator;
public class Clinet {
public static void main(String[] args) {
// TODO Auto-generated method stub
Drink drink = new MeishiCoffee();
System.out.println(" 价格 :" + drink.getCost());
System.out.println(" 描述 :" + drink.getDes());
drink = new Milk(drink);
System.out.println(" 价格 :" + drink.getCost());
System.out.println(" 描述 :" + drink.getDes());
drink = new Milk(drink);
System.out.println(" 价格 :" + drink.getCost());
System.out.println(" 描述 :" + drink.getDes());
System.out.println("================================");
Drink drink2 = new YingshiCoffee();
System.out.println(" 价格 :" + drink2.getCost());
System.out.println(" 描述 :" + drink2.getDes());
drink2 = new Milk(drink2);
System.out.println(" 价格 :" + drink2.getCost());
System.out.println(" 描述 :" + drink2.getDes());
drink2 = new Water(drink2);
System.out.println(" 价格 :" + drink2.getCost());
System.out.println(" 描述 :" + drink2.getDes());
}
}
高度抽象类 被扩展类的抽象类 Person.java
package com.godv.decorator.design;
public abstract class Person {
public abstract void dressed();
}
PersonCloth.java 继承扩展类的抽象类 保留扩展类抽象类的引用 并且在实现方法中调用引用的方法
package com.godv.decorator.design;
public class PersonCloth extends Person {
private Person p;
public PersonCloth(Person p) {
super();
this.p = p;
}
@Override
public void dressed() {
// 调用穿衣
p.dressed();
}
}
NiceCloth.java 扩展类
package com.godv.decorator.design;
public class NiceCloth extends PersonCloth {
public NiceCloth(Person p) {
super(p);
}
//扩展方法
private void dressShirt() {
System.out.println("穿短袖");
}
private void dressJean() {
System.out.println("穿裤子");
}
@Override
public void dressed() {
// TODO Auto-generated method stub
super.dressed();
dressShirt();
dressJean();
}
}
BadCloth.java 扩展类
package com.godv.decorator.design;
public class BadCloth extends PersonCloth {
public BadCloth(Person p) {
super(p);
}
//扩展方法
private void dressNothing() {
System.out.println("没穿");
}
private void dressJean() {
System.out.println("穿裤子");
}
@Override
public void dressed() {
// TODO Auto-generated method stub
super.dressed();
dressNothing();
}
}
Boy.java 被扩展类
package com.godv.decorator.design;
public class Boy extends Person{
@Override
public void dressed() {
// TODO Auto-generated method stub
System.out.println("穿内衣内裤");
}
}
Client.java
package com.godv.decorator.design;
public class Client {
public static void main(String[] args) {
Boy boy = new Boy();
NiceCloth niceCloth = new NiceCloth(boy);
niceCloth.dressed();
BadCloth badCloth = new BadCloth(boy);
badCloth.dressed();
}
}