1、父类 person 包含唯一公有属性
2、服饰类继承Finery继承共有类 并重写父类方法 添加自己的构造方法,和重写show()方法(装饰模式的核心)
3、//具体服饰类TShirts ,Kuzi, Overcoat继承了 服饰类Finery并且重写show的方法添加自己的功能
使用过程:
public class Decoraror_Model02_plus {
public static void main(String[] args) {
Person jun=new Person("jun");
TShirts Ts=new TShirts();
Kuzi Kz=new Kuzi();
Overcoat Oc=new Overcoat();
//注意顺序 顺序不能返 否则空指针
Ts.Decorate(jun); //穿上Ts
Kz.Decorate(Ts); //穿上Kz
Oc.Decorate(Kz); //穿上Oc
Oc.show();
}
}
package ch6_DecoratorModel;
/**
*
* 具体事项装饰模式 相比上一个去掉Component类
* @author Administrator
*
*/
public class Decoraror_Model02_plus {
public static void main(String[] args) {
Person jun=new Person("jun");
TShirts Ts=new TShirts();
Kuzi Kz=new Kuzi();
Overcoat Oc=new Overcoat();
//注意顺序 顺序不能返 否则空指针
Ts.Decorate(jun); //穿上Ts
Kz.Decorate(Ts); //穿上Kz
Oc.Decorate(Kz); //穿上Oc
Oc.show();
}
}
// 人
class Person{
private String name;
double total;
public Person() {
}
public Person(String name) {
this.name=name;
}
public void show() {
System.out.print(this.name+" ");
}
}
//服饰类
class Finery extends Person{
protected Person component;
//打扮
public void Decorate(Person component) {
this.component=component;
}
public void show() {
if(null!=component) {
component.show();
}
}
}
//具体服饰类
class TShirts extends Finery{
TShirts(){
this.total=30;
}
@Override
public void show() {
// TODO Auto-generated method stub
super.show();
this.total+=component.total;
System.out.println("大体恤30元 一共 "+this.total);
}
}
//裤子
class Kuzi extends Finery{
Kuzi(){
this.total=80;
}
@Override
public void show() {
// TODO Auto-generated method stub
super.show();
this.total+=component.total;
System.out.println("裤子80元 一共" +this.total );
}
}
class Overcoat extends Finery{
Overcoat(){
this.total=100;
}
@Override
public void show() {
// TODO Auto-generated method stub
super.show();
this.total+=component.total;
System.out.println("Overcoat100元 一共" +this.total );
}
}