开发者

Is there any implementation of class interfaces for Smalltalk?

开发者 https://www.devze.com 2023-02-14 13:49 出处:网络
In C# classes ca开发者_开发百科n have interfaces that can have multiple implementations.How do you do that in smalltalk?First of all you typically don\'t need interfaces, because if an object implemen

In C# classes ca开发者_开发百科n have interfaces that can have multiple implementations. How do you do that in smalltalk?


First of all you typically don't need interfaces, because if an object implements the same messages as another one it can replace it. In Java and C# you cannot do this unless they are in the same hierarchy, thus you need interfaces.

  • In (all) Smalltalk there are protocols (method categories) that serve the purpose of informally grouping methods together.
  • In Pharo Smalltalk there are Traits. At first they look like interfaces, but they are also able to provide an implementation.


After a discussion today with a coworker of mine it seems to me that the answer is any class could be considered an interface because any class can be passed in a message to any other class.

Any number of classes in smalltalk can respond to the same message, therefore you don't need interfaces as per C# and java.


As Lukas said, most of the times, you do not need them. Mostly because to achieve polymorphism, the only thing you need, is to implement the same message. There is no need to define a common type for them.

On the other hand, sometimes, from MY point of view, you do need interfaces. Mostly when you have a contract to show, or when having a kind of abstract superclass. This is very common when developing frameworks. Take as an example a logger or a serializer. In this case you may want to define the mandatory methods that a serializer should implement. Then you can create an abstract super class, with all the methods implemented this way:

LoggerIterface >> log: anObject
    self shouldBeImplemented


LoggerIterface >> reset
    self shouldBeImplemented

Etc...so, checking this class, you now which methods you have to implement so that the user of this object works well. Notice that #shouldBeImplemented is implemented in Object with something like this (in Pharo Smalltalk):

Object >> shouldBeImplemented
    "Announce that this message should be implemented"

    self error: 'This message should be implemented'

But as you can see, it is just a convention, it is not imposed by the language itself.

Cheers

Mariano


Even when not called "Interfaces", Dolphin Smalltalk (http://www.object-arts.com/) provides a feature named "Protocols" which are first class objects.

Each protocol defines a set of selectors (method names), and you can test whether a class conforms or not to a certain protocol:

conformsToProtocol: protocol
    "Answer whether the receiver conforms to the named <MethodProtocol>."

You end up having a formal/defined set of method names, and you can check whether certain object can be using in the context of a protocol. Also, the class browser will show you the list of protocols to which the selected class is compliant.

And there is a protocol browser, so you can explore each protocol and view system-wide which classes conforms to them.

Summarizing: Interfaces are not necessary in Smalltalk, at least not to implement polymorphism. However certain Smalltalk dialects provide different degrees of support to protocols, which are the analog of Interfaces but for dynamic languages.

0

精彩评论

暂无评论...
验证码 换一张
取 消