3

This issue is a pure design modeling

I have two packages and there are different classes with same name should embedded to these packages

what's the good design solution if I have same classes in different packages

I have read different solutions based on coding such as:

1-use "import"dependency between packages to avoid redundancy classes 2-create instance of classes in other package, and thus allow to have same name classes in different packages
3-fully qualify one of the class name

Would you suggest which is best solution or tell me other good solutions please?

elay221
  • 61
  • 8

2 Answers2

5

You are allowed to use the same name for classes when they are I different packages. A package is a namespace so the fully qualified names of such classes will be different. Now how you access the class depends on in which package are you at the moment. Whenever you're outside the package containing the class (either directly out through import/access), you have to use fully qualified names to avoid ambiguity.

If the classes are actually the same, you may: - put it the one package where it suits more and simply access it from the other package (standard approach, possible for all public classes) - put it in one of the packages (if it suits there better for some reason) and import it to the other package (through element or package import) - put it in additional package (e.g. Utils) and import it to both packages.

The choice will depend on specific situation.

Ister
  • 5,958
  • 17
  • 26
4

If it is the same class you should define it in one package and "reuse" it in the other.

A complete UML modeling tool should be able to Drag-n-Drop an existing class in another package.

The tool should be able to indicate you are using a class from another package.

enter image description here

Xaelis
  • 1,609
  • 10
  • 17
  • Another alternative would be to show the fully qualified name of the imported package, e.g. `aPackage.MyClass` inside the package `other`. And I don't know if there is an aliasing feature in UML, but it would even be nicer to be able to have `aPackage.MyClass as APackageMyClass`. – clapas Jul 09 '20 at 09:37