簡體   English   中英

強制執行靜態方法所需的設計模式

[英]Design pattern needed for enforcing static methods

好的,我想以一個我基本上想做的事為例來開始我的問題,盡管它不是這樣工作的。

我想要一個接口IDog ,它強制其實現具有某些方法。 我還希望超類AbstractDog implements IDog ,以便為所有Dog類提供基本屬性和方法。 然后我想讓子類像Poodle extends AbstractDog 我的問題是static方法-我基本上希望AbstractDog每個子類都有一個不同的static方法,但是我希望能夠從IDog強制執行此方法。

所以我幼稚的(錯誤的)實現是:

public interface IDog {

    String getName(); // every dog instance should be able to call name
    static String getDescription(); // every dog class should be able to get its description

}

public abstract class AbstractDog implements IDog {

    private String name; // every dog instance will have this

    public AbstractDog(String name) {
        this.name = name;
    }

    @Override
    public String getName() {
        return this.name; // every dog instance can call this
    }

}

public class Poodle extends AbstractDog {

    private static String description = "It's a poodle!"; // all Poodles have the same description

    public Poodle(String name) {
        super(name);
    }

    @Override // from IDog
    public static String getDescription() {
        return description;
    } 

}

現在,正如我所說,這是不正確的,因為AbstractDog類將需要static abstract方法getDescription()IDog需要其方法的實現,並且不能被覆蓋。

我想知道是否有一種與我的問題匹配的設計模式:強制執行一組類(可能或應該具有一個中間超類)來實現(不同!)靜態方法。

我發現了一種可能的方法,但我不確定它是否有用或足夠,那就是使用enum DogType ,然后只使用具有DogType屬性的class Dog

public enum DogType {

    Poodle("This is a poodle."), Havanese("This is a Havanese.)";

    private String description;

    private DogType(String description) {
        this.description = description;
    }

    public String getDescription() {
        return this.description;
    }
}

public class Dog {

    private String name;
    private DogType dogType;

    public Dog(String name, DogType dogType) {
        this.name = name;
        this.dogType = dogType;
    }

    public String getName() {
        return this.name;
    }

    public String getDescription {
        return this.dogType.getDescription();
    }
}

但是,這種“變通方法”使我的最初想法失去了能力:我現在不能僅對一個狗類添加其他功能,例如實例方法void prance() ,只能由Poodle訪問。

關於類似問題的許多主題都引用了Factory模式,但是我不確定它是否適合我的問題,因為我不一定需要構造方法。 我認為隨着狗比賽的增加,我的代碼將變得非常混亂。 也許我只是不了解如何正確使用Factory。

接口是強制行為。 類用於指定屬性。 靜態方法被隱藏。 它們不會被子類覆蓋。 因此,如果子類中有靜態方法,但是對象引用是超類型類,則將調用超類中的靜態方法。 這是類方法隱藏,是通過靜態方法發生的。

我想知道是否有一種與我的問題匹配的設計模式:強制執行一組類(可能或應該具有一個中間超類)來實現(不同!)靜態方法。

抱歉。 靜態方法和繼承不是並行的。

我現在不能僅對一個狗類添加其他功能,例如實例方法void prance(),該方法只能由Poodle訪問。

您可以使用方法void prance()引入接口Prancable

public interface Prancable{
      void prance();
}
public class Poodle extends Dog implements Prancable{
      @Override
      public void prance(){
           System.out.println("My Poodle can prance.");
      }
}

您可以按這種方式進行操作,從而為不同的犬種增加行為。

這是一種代碼味道,可能有更好的方法來做到這一點。

如果靜態方法將始終為該類的所有對象返回相同的內容,則應將其設為常規的get方法。

@Override \\ from IDog
public String getDescription() {
    return "This is a poodle";
} 

如果可以更改靜態變量,則創建一個新的對象,該對象將保持該類范圍的狀態並將其提供給構造函數中的每個類。

例如

// StringState is a new class that holds a string and has a set and get method
StringState desc = new StringState("original description");
IDog dog1 = new Poodle(desc);
IDog dog2 = new Poodle(desc);
// prints original description
System.out.Println(dog1.getDescription());
System.out.Println(dog2.getDescription());

desc.set("New description");
// prints new description, since both objects share the same
// StringState,changing it here changes it in all of them.
System.out.Println(dog1.getDescription());
System.out.Println(dog2.getDescription());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM