Everything has an Interface [duplicate]

Possible Duplicate:
Do I need to use an interface when only one class will ever implement it?

I am taking over a project where every single real class is implementing an Interface. The vast majority of these interfaces are implemented by a single class that share a similar name and the exact same methods (ex: MyCar and MyCarImpl). Almost no 2 classes in the project implement more than the interface that shares its name.

I know the general recommendation is to code to an interface rather than an implementation, but isn’t this taking it a bit too far? The system might be more flexible in that it is easier to add a new class that behaves very much like an existing class. However, it is significantly harder to parse through the code and method changes now require 2 edits instead of 1.

Personally, I normally only create interfaces when there is a need for multiple classes to have the same behavior. I subscribe to YAGNI, so I don’t create something unless I see a real need for it. Am I doing it all wrong or is this project going way overboard?

3

This is a straight up violation of the YAGNI principle and I strongly recommend against doing it that way. It is so easy to introduce an interface later when you really need it that you should not even bother creating it up front. I heard this is a quite common pattern among Java folks but don’t ask me why, I have no idea.

However, if this is a team project on which you collaborate, you should follow the established standards, even though they are bad.

2

This is a common with partial applications of SOLID principles to a project, and not necessarily a bad thing.

Coding against interfaces instead of classes is a reasonable way to maintain Dependency Inversion, and is very useful (some say necessary) for creating testable software components.

However, if your system as exactly 1 interface for every class, and vice versa, you’re probably not respecting Interface segregation, and you’re almost certainly violating the guideline of You Ain’t Gonna Need It.

Consider the cost in development time (now and in the future) of creating interfaces for each class, and evaluate whether better interface segregation would benefit your project. These principles and guidelines exist only to enable you to understand the tradeoffs in various designs, and communicate about them effectively. The decision on which conventions to follow, and how strictly, is in your hands.

2

Yes, this project is going way overboard. There are two main reasons why this may happen:

  • The programmer was blindly following some dogma without understanding where it would actually yield benefits and where not
  • The programmer is doing test-driven development and makes heavy use of a mocking framework for the tests, and that mocking framework does not (or did not, either when the project was created or when the programmer learned the technique) support mocking concrete classes.

Having an interface to each class is extreme. You spotted that right.

However, having interfaces implemented by only a single class is not that unusual as you may think. If you are familiar with the Agile principles, they usually say that interfaces belong to the client, which is both logically correct and technically practical. So you may create interfaces to define behavior to the client and compile clients without the real classes available and thus reducing compile times or delivery of modules. Because modules will depend on interfaces, they will be deliverable independently and without a concrete implementation. Than you can just swap implementations as you wish.

If you take this to extreme, you and up with what you see. It’s not good at all, but it would be worse to have no interface at all.

I know the general recommendation is to code to an interface rather than an implementation, but isn’t this taking it a bit too far?

(IMO) yes. If you have an interface with only one implementor, then you don’t really need the interface.

That said, many places will always require 2 implementors. The actual implementation and a mock implementation to allow unit testing of the things that consume it. If your object is just data, or can be used by tests in isolation, great.

The other case where interfaces make sense is as a public interface to an API. That can serve as protection to your consumers even if there is only one implementor (for now).

1

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