I have to do an UML class diagram for a project in which I implement an interface with abstract methods for RL algorithms. I implement several algorithms that implement an interface but implement additional methods depending on the algorithm. How can I capture that in an UML diagram? I have thought in including a box with the generic name "Algorithm" and the attributes and methods
"Algorithm specific attributes/methods", would that be correct in the UML framework?
A class should not inherit an interface but realize (aka implements) it. Graphically, you'll use the same notation than inheritance but with a dashed line:
Your case is a classic case of interface realization, as you'll find for example in a strategy pattern. The fact that you add other operations (aka methods) is completely normal and does not require anything special.
If you're using abstract classes instead of interfaces, then you may use inheritance:
Related
I'm creating a student information system which will have two access levels namely admin and manager. The Admin class can perform all functions of Manager class along with some additional functions. But literally speaking an Admin is not a manager. So how should I implement this relationship? Is it okay to do Admin extends Manager or is there any other way to implement this? (I'll be using java to implement the system)
Well, UML provides 3 mechanisms to depict (some) similarity between classes.
Generalization
Interfaces
Substitution
Let's look at them closer:
Generalization
When two classes are in generalization relationship, one of them (subclass, specialized or child class) is a kind of the other (superclass, general or parent class). Child class has all attributes and methods of its parents (inherits them) and can have some additional attributes or methods and can handle some methods in a different matter.
Generalization is depicted with a solid line and an arrow whose head is an empty triangle. Head is pointing the more general class
Interfaces
Actually interfaces are not a direct relationship between two classes that are in some way similar. But they help to show it to some degree.
Interface is a specific kind of a class that has a collection of methods. Interface is not directly instantiated, but other classes can either realize the interface or require an interface.
If a class realizes an interface (or provides it in other words) then it has to have all the methods of an interface, however interface in no way enforces a method of implementation.
So we may have two (or more) classes that realize the same interface and then both those classes will be able to perform the same functions, however it might be a totally different way.
We show interface realization by a dashed line and an arrow in a form of empty triangle, pointing the interface.
The class that requires the interface is the class interacting with those classes providing the interface.
To show interface usage you use a dashed line with an open arrow and stereotype <<use>> (well, technically it is not a stereotype).
Substitution
Class substitution is used to shown that one class can step into the role of the other class but it is not a kind of that class. A substituting class has to have all methods of the class it substitutes but it might have different internal representation.
This relationship is used when two or more classes can perform similar roles but they are not of the same kind.
Substitution relationship is shown as a dashed line with an open arrowhead pointing the class that can be substituted and a stereotype <<substitute>>
Your case is substitution, where Admin can substitute Manager, however you can combine substitution with interfaces to make it clearer.
Also always make sure that the recipient of your documentation will understand the element models you're going to use.
Just make a parent class with the methods shared by both Admin and Manager.
Class diagram is a static representation of a system that we are going to develop.Classes can be categorized as
Boundary classes
Control classes
Entity classes
So when we are drawing class diagram Is it not necessary to draw boundary classes in the class diagram?
It depends on what you are trying to show.
Entity-Control-Boundary (ECB) is a variation of the Model View Controller Pattern.
If your application does not follow the EBC pattern there is no need to mark classes as Entity, Boundary or Control.
You may also use Class Diagramms to show the relationship between some of the classes, like how the Mediator Pattern may be implemented.
From Wikipeda: Mediator Pattern UML Diagram
There is no Entity, Control or Boundary.
So in the answer is - in general there is no need to draw Entities, Boundaries or Control elements in UML Class Diagrams, only if your classes implement something like the ECB Pattern.
Among my two processes' functionality, there is a common function to merge files. I need not going to insist any of the processes to have some methods as interface does. And, also the two processes are independent. So, is it fine I just go with an Abstract class and have the implementation in that abstract class itself? Also I do not need any abstract method.
Inheritance is used when there is IS-A relation between subclass and the base class. I don't think it is the case here. You didn't specify the language, but from your profile I guess you use Java. So if you use an Abstract Class you won't be able to inherit from other, more appropriate class in the future.
Instead of inheritance you can use composition. Which means that you create a regular file merging class which has this method to merge files. And in classes where you want to have this functionality you just instantiate this new file merging class. It lets you inherit from other class in the future.
If you want to inform the world that those classes can merge files (to use polymorphism), and you use Java 8 you can create default method inside an interface and implement this interface without override this default method. But I think composition will be better in this case.
With reference to UML diagrams, what is an interface? and can someone explain in more simpler words. I cant understand anything from googling it.
An interface is like a template design for a class that contains no data or implemetnation; only definitions for methods, properties etc. These are abstract and cannot be instantiated but can be inherited from at which point all specified methods etc must be implemented by the concrete class inheriting the interface.
An interface is a design item describing a behaviour.
Classes implementing the interface will/must behave according to its definition.
Interfaces are used to promote loose coupling and the base of many IoC patterns (Inversion of Control)
A Interface is just a description of a class nothing concrete.
You use it to create a new class "with the same description" without knowing the concrete implementation.
In one word: it's a contract. Every class that implements this contract (interface) will have to implement the methods defined on it.
Whenever I create an abstract class I tend to create an interface to go along with it and have other code refer to the interface and not the abstract class. Usually when I don't create an interface to start with I regret it (such as having to override all implimented methods to stub the class for unit testing or later down the line new classes don't need any of the implimentation and override everything also finding themselves unable to extend any other class).
At first I tried to distinguish when to use an interface and when to use an abstract class by considering is-a vs able-to but I still would end up suffering later down the line for not making an interface to start with.
So the question is when is it a good idea to only have an abstract class and no interface at all?
When you wish to "give" some base class functionality to derived classes but when this functionality is not sufficient to instantiate a usable class, then go for abstract classes.
When you wish that some classes completely implement a set of methods (a public contract), then it is a convenient to define such contract with interfaces and enforce them onto classes by making them inherit this interface.
In short:
With abstract classes you give some common base functionality to derived classes. No further actions are necessary unless abstract class has some stubs (which have to be implemented down there).
With interfaces you require derived classes to implement a set of functions and you do not pass along any implementation.
So the question is when is it a good idea to only have an abstract class and no interface at all?
When you do not wish to enforce any public contract (a set of methods/properties defined by an interface).
Also when you do not plan to use certain coding techniques like casting object to an interface type (run-time polymorphism) or limit allowed input (some method argument will only accept object of types which implement certain interfaces).
Well, the main case it is useful to have only an abstract class without any interface is to mark a certain type. It is useful to be able to check if an object "is-a" something. These interface "mark" an objet to be of a certain type. Depending on the language you use, different design patterns apply ...
These sort of abstract classes exist in java. You can also use them in C++ with RTTI.
my2c