Fork me on GitHub

装饰器模式

package shejimoshi.zhuangshiqi.car;
//car接口
public interface Car {
 public abstract void color();
 public abstract void size();
}

  

package shejimoshi.zhuangshiqi.car.juticar;

import shejimoshi.zhuangshiqi.car.Car;
//具体car(原版车)
public class oldcar implements Car {

	@Override
	public void color() {
		System.out.println(" ");

	}

	@Override
	public void size() {
	  System.out.println(" ");
	}	

}

  

package shejimoshi.zhuangshiqi.car.zhuangshi;

import shejimoshi.zhuangshiqi.car.Car;
//装饰类
public class zhuangshi implements Car {
	protected Car car;

	public zhuangshi(Car car) {
		this.car = car;
	}

	public void color() {
		car.color();

	}
	public void size() {
		car.size();
	}

}

  

package shejimoshi.zhuangshiqi.car.zhuangshi;

import shejimoshi.zhuangshiqi.car.Car;
//具体装饰车BMW(改造后)
public class bmw extends zhuangshi {

	public bmw(Car car) {
		super(car);

	}

	public void color() {
		car.color();
		System.out.println("白色");
	}

	public void size() {
		car.size();
		System.out.println("大空间");
	}
}

  

package shejimoshi.zhuangshiqi.car;

import shejimoshi.zhuangshiqi.car.juticar.oldcar;
import shejimoshi.zhuangshiqi.car.zhuangshi.bmw;
import shejimoshi.zhuangshiqi.car.zhuangshi.zhuangshi;
//测试类
public class testcar {
 public static void main(String[] args) {
	Car car=new oldcar();
	car.color();
	car.size();
	zhuangshi BMW=new bmw(car);
	BMW.color();
	BMW.size();
}
}

  

posted @ 2018-09-05 17:31  雪人奥特曼  阅读(199)  评论(0编辑  收藏  举报