装饰者模式--用于扩展 类 的方法

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();
	}
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值