Does it make sense to use interfaces if you don’t have polymorphism?

Suppose I have distinct classes that have the same behavior, which can be represented like this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public interface Behavior {
void operationA();
}
public class ImplementerA implements Behavior {
public void operationA() {
}
}
public class ImplementerB implements Behavior {
public void operationA() {
}
}
</code>
<code>public interface Behavior { void operationA(); } public class ImplementerA implements Behavior { public void operationA() { } } public class ImplementerB implements Behavior { public void operationA() { } } </code>
public interface Behavior {
    void operationA();
}

public class ImplementerA implements Behavior {
    public void operationA() {

    }
}

public class ImplementerB implements Behavior {
    public void operationA() {

    }
}

But ImplementerA and ImplementerB are used by distinct clients; ClientA only uses ImplementerA and ClientB only use ImplementerB; other than these, there are no other clients for ImplementerA and ImplementerB.

In this scenario, does the interface Behavior have any purpose? Should it be created because maybe future requirements changes…

8

If you’re using a language where classes must be hard-coded into the code (e.g. Java/C#) then it might make sense to use an interface in case you ever want to change the implementation of Behavior that gets passed to ClientA or ClientB without recompiling. This usually happens when you want to unit test some code by providing mock implementations of Behavior. Without an interface you’d have to change the import statement of the client code so that it references the right implementation and recompile it.

In languages that don’t hard-code references to classes using globally-unique identifiers (which is what fully-qualified names such as com.yoursite.Foo are) this isn’t an issue and there’s no need to make an interface. However languages with this kind of module system are relatively unpopular functional languages (e.g. Standard ML, OCaml) so it likely doesn’t apply to you.

Note, however, that not every class can be implemented as an interface. Consider:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public class LooksAtOthers {
public LooksAtOthers binaryOperation(LooksAtOthers other) {
...
}
}
</code>
<code>public class LooksAtOthers { public LooksAtOthers binaryOperation(LooksAtOthers other) { ... } } </code>
public class LooksAtOthers {
    public LooksAtOthers binaryOperation(LooksAtOthers other) {
        ...
    }
}

The binary operation here looks at the private fields of other. This comes up a lot when implementing data structures; in order to do efficient merges or unions you have to be able to look at the implementation of both operands. This can’t be done from an interface:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public interface CantLook {
CantLook binaryOperation(CantLook other)
}
public class CantLookImpl implements CantLook {
public CantLook binaryOperation(CantLook other) {
// no idea what type CantLook has
}
}
</code>
<code>public interface CantLook { CantLook binaryOperation(CantLook other) } public class CantLookImpl implements CantLook { public CantLook binaryOperation(CantLook other) { // no idea what type CantLook has } } </code>
public interface CantLook {
    CantLook binaryOperation(CantLook other)
}

public class CantLookImpl implements CantLook {
    public CantLook binaryOperation(CantLook other) {
        // no idea what type CantLook has
    }
}

Here CantLook is an interface, so the class implementing CantLook has no idea what private fields other has. As a result if binaryOperation required looking inside other, it can’t be implemented efficiently any more.

Note also that using a class makes debugging somewhat easier. With a class any given client code only interacts with one implementation; it either works or it doesn’t. With an interface there can be any number of implementations and nothing stops someone from making a bad one. If you use an interface and ClientA breaks, you have to look at which implementation of Behavior broke it. Someone might’ve created and passed in a buggy ImplementerC.

So the bottom line is: it depends. Does it bother you to have to change import statements? Are you doing unit testing? Do you need to look at other instances of the class?

7

The main reason to use interfaces is explained in the dependency inversion principle ( the D in SOLID ).

  • High-level modules should not depend on low-level modules. Both should depend on abstractions.
  • Abstractions should not depend on details. Details should depend on abstractions(***).

The goal of this is decoupling the higher level from specific implementations, allowing for more maintainability.

Maintainability’s main issue is compensate the unability of human beings to foresee the future.

Statements like “ClientA only uses ImplementerA and ClientB only use ImplementerB” has proven to not hold true forever.

*** NOTE: In the context of SOLID and OOD, “abstration” means than it cannot be instantiated, like an abstract class or interface.

Something that cannot be instantiated needs to have very little reason to change overtime. That way code that depends on abstractions depend on things that are stable ( the what vs the how ). Code that depends on abstractions have less reason to change that code that depends on concretions.

10

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật