Decoretor
1641 ワード
次のようになります.
この犬は歩くだけだ.そうするとね.私のGWも歩くだけなので、まあ、下を見ればわかります.
結果:
MyDog can move
*********************
MyDog can move
yydog can fly so eat moon!
package com.pattern.decoretor.yzb;
public interface IDog {
void move();
}
この犬は歩くだけだ.そうするとね.私のGWも歩くだけなので、まあ、下を見ればわかります.
package com.pattern.decoretor.yzb;
public class MyDog implements IDog{
public void move() {
System.out.println("MyDog can move");//GW
}
}
package com.pattern.decoretor.yzb;
public class YDog extends DogDecoretor {
public YDog(IDog dog) {
super(dog);
}
public void move() {
super.move();
}
public void eatMoon(){
System.out.println("yydog can fly so eat moon!");// .
}
}
package com.pattern.decoretor.yzb;
public abstract class DogDecoretor implements IDog {
private IDog dog;
public DogDecoretor(IDog dog){
this.dog=dog;
}
public void move() {
dog.move();
eatMoon();
}
public abstract void eatMoon();// .
}
package com.pattern.decoretor.yzb;
public class Test {
public static void main(String [] args){
IDog dog=new MyDog();
dog.move();//
System.out.println("*********************");
YDog y=new YDog(dog);
y.move();//
}
}
結果:
MyDog can move
*********************
MyDog can move
yydog can fly so eat moon!