亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在Java中避免使用instanceof

在Java中避免使用instanceof

梵蒂岡之花 2019-08-26 14:16:30
在Java中避免使用instanceof具有一系列“instanceof”操作被認為是“代碼味道”。標準答案是“使用多態”。在這種情況下我該怎么做?基類有許多子類; 沒有一個在我的控制之下。類似的情況是Java類Integer,Double,BigDecimal等。if (obj instanceof Integer) {NumberStuff.handle((Integer)obj);}else if (obj instanceof BigDecimal) {BigDecimalStuff.handle((BigDecimal)obj);}else if (obj instanceof Double) {DoubleStuff.handle((Double)obj);}我確實可以控制NumberStuff等等。我不想在幾行代碼中使用多行代碼。(有時我將一個HashMap映射到一個IntegerStuff的實例,將BigDecimal.class映射到一個BigDecimalStuff的實例等等。但是今天我想要一些更簡單的東西。)我想要像這樣簡單的東西:public static handle(Integer num) { ... }public static handle(BigDecimal num) { ... }但是Java不會那樣工作。我想在格式化時使用靜態方法。我正在格式化的東西是復合的,其中Thing1可以包含一個數組Thing2s和Thing2可以包含一個Thing1s數組。當我實現這樣的格式化程序時,我遇到了問題:class Thing1Formatter {  private static Thing2Formatter thing2Formatter = new Thing2Formatter();  public format(Thing thing) {      thing2Formatter.format(thing.innerThing2);  }}class Thing2Formatter {  private static Thing1Formatter thing1Formatter = new Thing1Formatter();  public format(Thing2 thing) {      thing1Formatter.format(thing.innerThing1);  }}是的,我知道HashMap和更多代碼也可以修復它。但相比之下,“instanceof”似乎更具可讀性和可維護性。有什么簡單但不臭嗎?注釋已添加5/10/2010:事實證明,將來可能會添加新的子類,而我現有的代碼必須優雅地處理它們。在這種情況下,類上的HashMap不起作用,因為找不到類。一系列if語句,從最具體的開始到以最一般的結尾,可能是最好的:if (obj instanceof SubClass1) {    // Handle all the methods and properties of SubClass1} else if (obj instanceof SubClass2) {    // Handle all the methods and properties of SubClass2} else if (obj instanceof Interface3) {    // Unknown class but it implements Interface3    // so handle those methods and properties} else if (obj instanceof Interface4) {    // likewise.  May want to also handle case of    // object that implements both interfaces.} else {    // New (unknown) subclass; do what I can with the base class}
查看完整描述

3 回答

?
肥皂起泡泡

TA貢獻1829條經驗 獲得超6個贊

您可能對Steve Yegge的亞馬遜博客中的這篇文章感興趣:“當多態失敗時”。基本上他正在解決這樣的情況,當多態性導致比它解決的更多麻煩時。

問題是,要使用多態,你必須使“處理”邏輯成為每個“切換”類的一部分 - 即在這種情況下為整數等。顯然這不切實際。有時甚至在邏輯上它都不是放置代碼的正確位置。他建議將'instanceof'方法視為幾種邪惡中較小的一種。

與您被迫編寫臭代碼的所有情況一樣,請將其保持在一個方法(或最多一個類)中,以便氣味不會泄漏。


查看完整回答
反對 回復 2019-08-26
?
12345678_0001

TA貢獻1802條經驗 獲得超5個贊

正如評論中所強調的那樣,訪客模式將是一個不錯的選擇。但是如果沒有對目標/接受者/被訪者的直接控制,則無法實現該模式。這里是訪問者模式可能仍然可以在這里使用的一種方式,即使你沒有使用包裝器直接控制子類(以Integer為例):

public class IntegerWrapper {
    private Integer integer;
    public IntegerWrapper(Integer anInteger){
        integer = anInteger;
    }
    //Access the integer directly such as
    public Integer getInteger() { return integer; }
    //or method passthrough...
    public int intValue() { return integer.intValue(); }
    //then implement your visitor:
    public void accept(NumericVisitor visitor) {
        visitor.visit(this);
    }}

當然,包裝最終類可能被認為是它自己的氣味,但也許它很適合你的子類。就我個人而言,我認為instanceof這里的味道不是很糟糕,特別是如果只限于一種方法,我會很樂意使用它(可能超過我自己的建議)。正如你所說,它具有可讀性,類型安全性和可維護性。一如既往,保持簡單。


查看完整回答
反對 回復 2019-08-26
  • 3 回答
  • 0 關注
  • 659 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號