0

Say I have an interface Interface and a concrete class ConcreteClass that implements Interface. Now consider a third class MyClass. If instances of MyClass hold a reference to ConcreteClass:

Interface ref = new ConcreteClass();

then should I associate MyClass with Interface or ConcreteClass in UML class diagram?

Thanks

Wain
  • 118,658
  • 15
  • 128
  • 151
user798275
  • 441
  • 6
  • 14

5 Answers5

0

That depends on what the public interface of MyClass defines.

If the public interface makes an Interface available, then you should link to that on the diagram. This would be the usual approach as the Interface is the general type and specifies the contract. Unless you have a reason to limit to ConcreteClass, don't.

If the public interface makes a ConcreteClass available, then you should link to that on the diagram.

The fact that at runtime a variable of type Interface actually holds an instance of ConcreteClass is beside the point. The diagram represents the relationships.

Wain
  • 118,658
  • 15
  • 128
  • 151
  • I'm not sure if I understand what you mean. Let me clarify: My Class does not implement Interface. The only relationship btw these classes is that ConcreteClass implements Interface. – user798275 Mar 04 '14 at 17:17
  • And my class has an association with interface. – Wain Mar 04 '14 at 19:14
0

There are 2 ways to present the ref variable of MyClass: You can present it as attribute or as association. Then there are two alternative notations for the Interface interface: Square with the interface stereotype or circle. This makes 2*2=4 alternatives.

  1. Show ref as association and use square interface notation. alt 1

Here you can't show the initial value that ref takes. That's because you can't show default values in associations.

  1. Show ref as association but use the circle notation for the Interface. alt 2

As it was with the previous alternative, again here you can't show the initial value.

  1. Show ref as attribute and use square interface notation. alt 3

Here you can show the default value, because you can do that for attributes. The relationship between MyClass and Interface is presented as a dependency. The same happens for the dependency between MyClass and ConcreteClass. Note that this dependency (MyClass depends on ConcreteClass) can be presented also in the alternatives 1 and 2, you can add a dependency arrow (dashed) pointing from MyClass to ConcreteClass.

  1. Show ref as attribute and use circle interface notation. alt 4

Again here you can show the default value.

If we count also the alternatives derived from presenting or not the dependencies, then there are at least 6 ways to present the same thing. Now the question is which to chose. It depends on what do you want to visualize with the diagram and for whom the diagram is intended. In this case if the initialization of ref is the message, then you should use an alternative that presents it. If it's less important, then you might prefer a diagram that shows ref as association.

In a real problem you have more elements, so it makes much more alternatives. It's always up to you to decide what to present and how.

EDIT: Some references to help you understand the notation of interface implementation.

According to wikipedia:

A realization is a relationship between classes, interfaces, components, and packages that connects a client element with a supplier element. A realization relationship between classes and interfaces and between components and interfaces shows that the class realizes the operations offered by the interface.

You can find some quick reference examples and a lot of information at uml-diagrams.org.

This excellent answer Explanation of the UML arrows will help you with more examples.

Here you can also find some more info on realization.

Community
  • 1
  • 1
nakosspy
  • 3,904
  • 1
  • 26
  • 31
  • generalization connection is a solid line, according to UML standard. – Gangnus Mar 05 '14 at 09:53
  • Attribute is to be shown as association, too. They are NOT two variants of solution for one task. – Gangnus Mar 05 '14 at 09:54
  • "Here you can't show the initial value that ref takes. That's because you can't show default values in associations." NO. Here you can't do it because interface can't take ANY value. NEVER. – Gangnus Mar 05 '14 at 09:56
  • Slashed line means dependency, not association. Association with attribute is to be shown in dotted format. Hey, you are absolutely lost in the subject, why are you trying to explain in so complex way? You want him to get lost, too? – Gangnus Mar 05 '14 at 10:00
  • 1. The relationship is realization not generalization. 2. Attribute canbe presented as attribute or as association. 3. It's not interface that takes initial value, it's the `ref` variable which is defined as interface but is initialized with the default value `new ConcreteClass()`. 4. `MyClass` does not have any association with `ConcreteClass`, but it depends on it because of the 'ref=new ConcreteClass()` code. – nakosspy Mar 05 '14 at 10:10
  • 4. "MyClass does not have any association with ConcreteClass"- Why? That was the question.... If you are deriving a class from Interface or abstract class and using that interface instead of concrete class, it is the standard polymorphism and needn't be shown as any dependency. – Gangnus Mar 05 '14 at 10:16
  • Edited with some more references. – nakosspy Mar 05 '14 at 16:23
0

Solely with the Interface. The point is that you want the behavior of the interface. Whatever the implementation is of that interface is for the picture of no importance. MyClass has a relation with the interface, not with the implementation of the interface.

This principle is called Design By Interface. In the answer given by nakosspy is it his first picture. But it would even be better to leave the implementation of ConcreteClass out of the picture. The implementation is of no importance at that conceptual level. If there is a variable pointing to an interface, then is it obvious to the educated reader that there should be a concrete implementation as well.

If you would make a reference to the ConcreteClass then would you have to change the diagram everytime you change the implementation of the interface. That is not what you want. It is bad coding practice and bad uml practice.

It is good coding practice to separate the declaration of the relationship between MyClass and the Interface and the practical implementation of the Interface. By example:

Interface ref = new ConcreteClass();

should never happen in the class MyClass.

You should have something like this instead:

class MyClass
  Interface ref;

  setRef(){
    ref = InterfaceImplementation();
  }
 }

This way can you change the implementation of Interface without changing one line of code in MyClass. Altough this might look much ado when you write one class, think of it when you are managing hundreds of classes.

Loek Bergman
  • 2,192
  • 20
  • 18
0

So: it depends.

It's equally legal to associate MyClass with ConcreteClass or Interface. You won't find the answer to your question in the UML spec. Why? Because the answer lies in your problem domain, not the modelling language.

Consider two contrived examples to illustrate the point.

Example 1: Association between Classes

Substitute:

  • ICanBark for Interface
  • Dog for ConcreteClass
  • Trainer for MyClass

Let's assume the association we want to capture is Trains, i.e.

  • Each Trainer trains many Dogs
  • Each Dog is trained by at most one Trainer

In this case the association exists because of the 'Dogginess', not the 'Barkiness'. So it properly exists between the two classes.

Example 2: Association between Class and Interface

Substitute:

  • ILogger for Interface
  • FileLogger for ConcreteClass
  • Application for MyClass

In this case the relationship is about the 'Logginess', not the 'Fileness'. Application shouldn't care how the interface is implemented; it just wants a way to log messages. So the Association exists between the Class and the Interface

Summary As is nearly always the case with Associations, the key to solving the problem lies in the problem domain itself - not the modelling language.

hth.

sfinnie
  • 9,854
  • 1
  • 38
  • 44
0

You can define reference to concrete class as:

  • Attribute typed as Interface (or ConcreteClass) defined in MyClass, or
  • Association between MyClass and Interface (or ConcreteClass).

no more options are avialable

Vladimir
  • 2,066
  • 15
  • 13