2012-05-14 45 views
1

請參閱下面的代碼。我看不到我錯在哪裏。我也很困惑!非常感謝您的幫助!無法獲得裝飾模式工作

package code; 

public class Client { 

public static void main(String[] args){ 

    proxyPlane plane = new proxyPlane(new Pilot(18)); 
     plane.flyPlane(); 

     plane = new proxyPlane(new Pilot(25)); 
     plane.flyPlane(); 

     DecoratedPilot decPilot = new Pilot(35); 

     System.out.println("Experienced Pilot of age " + Pilot.Age() + " " + decPilot.getDecotation()); 
    } 

} 


package code; 

public interface DecoratedPilot { 

public String getDecotation(); 
} 


package code; 

public class Decoration extends PilotDecorator { 

public Decoration(Pilot pilot) { 
    super(pilot); 
    // TODO Auto-generated constructor stub 
} 

@Override 
public String getDecotation() { 
    return "Pilot has earned his Commercial Wings"; 
} 
} 


package code; 

public abstract class PilotDecorator implements DecoratedPilot { 

public PilotDecorator(Pilot pilot) 
{ 
    apilot = pilot; 
} 
} 


package code; 

public class Pilot implements DecoratedPilot { 

private static int age; 

public static int Age() { 

    return age; 
} 

public Pilot(int age){ 

    Pilot.age = age; 
} 

public String getDecotation() { 
    // TODO Auto-generated method stub 
    return null; 
} 
} 
+0

你的問題是什麼? – romaintaz

+1

你會得到什麼輸出,以及你期望輸出什麼? – Polygnome

+0

請正確地縮進並格式化代碼,其確實很難像這樣讀取。 – Brady

回答

0

問題是你實際上並沒有裝飾任何東西。也就是說,你不是用裝飾器「包裝」一個Component。

下面是Decorator pattern的示例。

這種模式的一個很好的例子是在書中:Head First Design Patterns。我真的很喜歡這本書,如果你還沒有這本書,我強烈推薦它。

+0

如何讓一個類包裝另一個類? – user1360809

+0

@ user1360809,通過傳遞一個對象給構造函數,就像yanflea在他的回答中所做的那樣。像這樣:Pilot decoratedPilot =新DecoratedPilot(standardPilot); – Brady

3

這裏:


 package code; 

     public class Client { 

      public static void main(String[] args) { 
       // -- A standard pilot 
       Pilot standardPilot = new StandardPilot(35); 
       System.out.println("Pilot : " + standardPilot.getAge() + " - " + standardPilot.getDescription()); 
       // -- A decorated pilot 
       Pilot decoratedPilot = new DecoratedPilot(standardPilot); 
       System.out.println("Pilot : " + decoratedPilot.getAge() + " - " + decoratedPilot.getDescription()); 
      } 
     } 

 package code; 

     public interface Pilot { 
      int getAge(); 
      String getDescription(); 
     } 

package code; 

    public class StandardPilot implements Pilot { 

     private int age; 

     public StandardPilot(int age) { 
      this.age = age; 
     } 

     @Override 
     public int getAge() { 
      return age; 
     } 

     @Override 
     public String getDescription() { 
      return "Standard Pilot"; 
     } 

}


package code; 

public class DecoratedPilot implements Pilot { 

    private Pilot pilot; 

    public DecoratedPilot(Pilot pilot) { 
     // todo : check not null 
     this.pilot = pilot; 
    } 

    @Override 
    public int getAge() { 
     return pilot.getAge(); 
    } 

    @Override 
    public String getDescription() { 
     return "Decorated Pilot"; 
    } 
} 

如果你需要幾個裝飾器,你可以製作DecoratedPilot抽象並從中繼承每個特定的裝飾器。

+2

重要的是要注意,這裏的裝飾方法是getAge()。通常,對於裝飾方法,裝飾器將完成其部分,然後在包含(內部或包裝)對象上調用裝飾方法。 – Brady

+0

當然。感謝精確度。 – Yanflea