tag:blogger.com,1999:blog-736810000699453506.post3200872352366934967..comments2023-05-11T00:49:36.314-07:00Comments on Ruminations on Computing: Design to InterfacesSteve Rowehttp://www.blogger.com/profile/17905356014908630180noreply@blogger.comBlogger7125tag:blogger.com,1999:blog-736810000699453506.post-38663546058666609522008-01-21T05:28:12.000-08:002008-01-21T05:28:12.000-08:00While I agree that interfaces are indeed useful fo...While I agree that interfaces are indeed useful for data classes, I think your moral is far too simplistic.<br>Interfaces have accessibility implications: to be useful within an accessibility scope, they must be implementable by anyone in that scope. But sometimes you need to limit implementations, or you need to require certain implementations -- perhaps orchestrating event sequences, which you would put in a base class -- without allowing instantiation of the bast type. <br>In those cases, you shouldn't use interface. You are more likely to need, instead, an abstract class, and possibly a few well-considered sealeds.MSDNArchivenoreply@blogger.comtag:blogger.com,1999:blog-736810000699453506.post-41646911335521787592008-01-21T05:57:30.000-08:002008-01-21T05:57:30.000-08:00Good point. There are times when you need to disr...Good point. There are times when you need to disregard this principle. As long as you have a good reason for doing so, you are okay. Principles should be generally but not strictly held to. Thanks for pointing out a place where it is a bad idea. Knowing where not to utilize it can be as important as knowing where to use it.<br>I would point out, however, that "interface" is not coequal with the C# (or Java) notion of interface. An abstract class is still an interface in the sense of this article.SteveRowenoreply@blogger.comtag:blogger.com,1999:blog-736810000699453506.post-36718671457094137512008-01-21T06:37:23.000-08:002008-01-21T06:37:23.000-08:00I'd considered that you were talking abstractl...I'd considered that you were talking abstractly (sic) about interfaces, but I also considered that "interface" has a fairly specific definition in mainstream practice.<br>I know it took me a little while to grok why we had abstract classes at all, when we had interfaces. Figured it was worth saving some newbies the confusion.MSDNArchivenoreply@blogger.comtag:blogger.com,1999:blog-736810000699453506.post-62565539234083799722008-01-21T06:50:17.000-08:002008-01-21T06:50:17.000-08:00I'll bite. Why /do/ we have both abstract cla...I'll bite. Why /do/ we have both abstract classes and interfaces?Mauritsnoreply@blogger.comtag:blogger.com,1999:blog-736810000699453506.post-14650988585218109052008-01-21T07:02:22.000-08:002008-01-21T07:02:22.000-08:00My guess: Because C# doesn't have multiple in...My guess: Because C# doesn't have multiple inheritance. You can inherit from any number of interfaces but just one base class (concrete or abstract). That way you avoid most of the messiness of multiple inheritance but gain many of the benefits.SteveRowenoreply@blogger.comtag:blogger.com,1999:blog-736810000699453506.post-25608902504356291402008-01-25T18:26:34.000-08:002008-01-25T18:26:34.000-08:00Actually, one significant difference is that you c...Actually, one significant difference is that you can version classes -- you can't version interfaces. <br>By version, I of course mean the addition of members. Do that with an abstract class and (assuming no subtypes conflict in name), things still compile. Do that with an interface, and *every* subtype breaks.<br>Kiril in C#-land actually disussed this a few months ago. I'd forgotten that this whole thing had an entry in the Framework Design Guidelines: <br>http://kirillosenkov.blogspot.com/2007/08/choosing-interface-vs-abstract-class.html<br>The FDG entry: http://msdn2.microsoft.com/en-us/library/ms229019.aspxMSDNArchivenoreply@blogger.comtag:blogger.com,1999:blog-736810000699453506.post-3970572054047077912008-02-25T17:17:55.000-08:002008-02-25T17:17:55.000-08:00Object-oriented design and design patterns can see...Object-oriented design and design patterns can seem complex. There are a lot of ideas and cases to consider.Steve Rowe's Bloghttp://blogs.msdn.com/steverowe/archive/2007/11/30/design-principles-to-live-by.aspxnoreply@blogger.com