Difference between Java implementation and UML specification wrt interfaces and abstract classes

Donbhupi

Various similar-looking questions exist but I could find none that responds to my query.The possibility of having the specified aggregation relationship while implementing an interface is an essential part of my question, which is not treated in other similar questions or their solutions.

My question concerns implementation of Design Patterns in Java. For the ease of explanation, I'm considering the Command design pattern as shown in this diagram.Class Diagram

Since this UML representation requires that "Invoker" should be an interface and that it should also have an N-ary aggregation relationship with the "Command" interface, it's not possible to implement it if I define Invoker as a Java interface because I can't declare non-static non-final attributes in a Java interface and I do need to define a non-final Collection of "Command" objects to establish that aggregation relationship between "Invoker" and "Command".

So, I went ahead and implemented Invoker as an abstract class, which lets me have the aggregation and also provides abstraction to the implementations of "Invoker" but I'm wondering if it is a good design practice because UML does have a stereotype <<abstract>> but the UML class diagram for this pattern explicitly specifies using an interface rathern than an abstract class. I've also done the same for "Subject" interface of "Observer" pattern implementation in Java for a similar reason.

Please let me know if there's a way to keep "Invoker" as an interface in Java and still achieve the aggregation relationship with the specified multiplicity. If what I did is the best way to do it, please let me know if it may have some adverse effects on the structure of a program I build using the pattern this way.

Adding below the class diagram of a short Java implementation of Command Pattern that I put up for enhanced clarity of the question. I've configured "Controller" here as the "Invoker" interface that can be implemented in different ways, e.g. "InfraredRemote Controller" in this diagram. But the Design Pattern requirement of aggregation relationship between "Invoker" and "Command" interfaces made me configure "Controller" as an abstract class because I could not find any way to achieve the required multiplicity and relationship when I configured "Controller" as an interface. Java Implementation

Donbhupi

Thanks to a good brainstorming session with @zapl and some more research on the web, I've found the answer through this article that makes reference to this page of a very interesting book.

“Program to an interface”, really means “Program to a supertype”. – Head First, Design Patterns

Apparently, the use of word 'Interface' in UML specifications of Design Patterns is more generic and only denotes abstraction, viz. supertype, which can be achieved in the Java implementation by using either an Interface or an abstract class. Although the stereotypes for <<abstract>> classes exist in UML, they are rarely used to describe Design Patterns.

And as it's not possible to achieve the n-ary multiplicity between Java interfaces, the UML "Invoker" interface in the example I used in my question needs to be implemented using a Java interface that is implemented by an abstract class and by extending that abstract class with concrete Invoker subclasses. This would maintain malleability of the code while preserving the abstraction as shown in the implementation below: Java implementation

Here the client "Implementer" uses the Java interface which is implemented by a Java abstract class to provide the multiplicity with "Command" interface and the access to that multiplicity to the concrete Invokers; these two together represent the "Invoker" interface in the UML specification of the Command Design Pattern.

I'm leaving my answer open for comments as of now, expecting comments on something I may have missed. If I don't get any, I'll accept my answer in, say, two days.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

What are the differences between abstract classes and interfaces in Java 8?

From Dev

Preferences of abstract classes over interfaces in Java 8

From Dev

JAR Manifest file - Difference between Specification and Implementation

From Dev

Using abstract classes \ interfaces

From Dev

uml component diagram difference between assembly and interfaces with dependency

From Dev

python abstract base classes, difference between mixin & abstract method

From Dev

Is there a difference between Swift 2.0 protocol extensions and Java/C# abstract classes?

From Dev

Java 8 -- interfaces with default methods vs abstract classes

From Dev

Interfaces and abstract classes in F#

From Dev

Interfaces and abstract classes in F#

From Java

The difference between the Runnable and Callable interfaces in Java

From Dev

What's the difference between public interfaces and published interfaces in Java?

From Dev

ZeroC ICE : what's the difference between interfaces and classes?

From Dev

What would be the correct implementation while deciding over interfaces vs abstract classes?

From Dev

What is the difference between these two c# abstract classes?

From Dev

Interfaces and abstract classes for model classes (MVVM)

From Dev

Association between Classes and Interfaces

From Dev

Casting between Interfaces and Classes

From Dev

What are the differences between dynamic polymorphism, abstract classes and interfaces from an OOP perspective as reflected in C#?

From Dev

UML - Relation between Java classes when not defined in a field

From Dev

UML definition - Generalization, aggregation and abstract classes

From Dev

Java UML Diagrams to Classes

From Dev

SOLID Principles : interfaces vs. abstract classes

From Dev

Interfaces vs. Abstract Classes or polymorphism in general

From Dev

Multiple Inheritance from interfaces (abstract classes)

From Dev

Interfaces vs. Abstract Classes or polymorphism in general

From Dev

Proper usage of abstract classes or interfaces in the following situation

From Dev

Pass objects in a standardized way to interfaces/abstract classes

From Java

interfaces without implementation in Java

Related Related

  1. 1

    What are the differences between abstract classes and interfaces in Java 8?

  2. 2

    Preferences of abstract classes over interfaces in Java 8

  3. 3

    JAR Manifest file - Difference between Specification and Implementation

  4. 4

    Using abstract classes \ interfaces

  5. 5

    uml component diagram difference between assembly and interfaces with dependency

  6. 6

    python abstract base classes, difference between mixin & abstract method

  7. 7

    Is there a difference between Swift 2.0 protocol extensions and Java/C# abstract classes?

  8. 8

    Java 8 -- interfaces with default methods vs abstract classes

  9. 9

    Interfaces and abstract classes in F#

  10. 10

    Interfaces and abstract classes in F#

  11. 11

    The difference between the Runnable and Callable interfaces in Java

  12. 12

    What's the difference between public interfaces and published interfaces in Java?

  13. 13

    ZeroC ICE : what's the difference between interfaces and classes?

  14. 14

    What would be the correct implementation while deciding over interfaces vs abstract classes?

  15. 15

    What is the difference between these two c# abstract classes?

  16. 16

    Interfaces and abstract classes for model classes (MVVM)

  17. 17

    Association between Classes and Interfaces

  18. 18

    Casting between Interfaces and Classes

  19. 19

    What are the differences between dynamic polymorphism, abstract classes and interfaces from an OOP perspective as reflected in C#?

  20. 20

    UML - Relation between Java classes when not defined in a field

  21. 21

    UML definition - Generalization, aggregation and abstract classes

  22. 22

    Java UML Diagrams to Classes

  23. 23

    SOLID Principles : interfaces vs. abstract classes

  24. 24

    Interfaces vs. Abstract Classes or polymorphism in general

  25. 25

    Multiple Inheritance from interfaces (abstract classes)

  26. 26

    Interfaces vs. Abstract Classes or polymorphism in general

  27. 27

    Proper usage of abstract classes or interfaces in the following situation

  28. 28

    Pass objects in a standardized way to interfaces/abstract classes

  29. 29

    interfaces without implementation in Java

HotTag

Archive