I am working on a software assignment where the design is component based. The components have ports which provide interfaces.

My professor argues that the Port class which is exposed by each component should be a Singleton, as the port must be the only way to interact with the component. As I see it, multiple instances don't interfere with the requirement that the port must be the single interaction point.

Is as singleton the right way to implement a Port to the component?

To give an example: The assignment is a model of an airplane and an airport. The components are different parts of an airplane and the airport. As it is a group assignment, different students have to implement different components. This is the UML diagram of the different components of the airport: Airport Components

The interfaces are implemented in a class named Port, which is an inner class to the actual implementation. The outer class has a public field port and is a singleton. The different components interact by loading the Jar-File and accessing the Port instance via reflection.

  • 1
    What, exactly, do you mean by "port"? A component is defined by the interface(s) that it provides to the public (to other components) and by interfaces that it requires from components that it interacts with. These interfaces could be any form of API - RESTful APIs, XML exchanges over a service bus, database schemas, methods on one or more classes.
    – Thomas Owens
    Commented Jan 30, 2018 at 15:12
  • I would think that an single interface is all that is required. Whether the implementation of the interface is shared is irrelevant. Unless you are working within the constraints of a much more limited component system, the singleton requirement is only a constraint imposed by your professor. My suggestion? Comply with what your professor asked you to do for the assignment, learn what you can learn, but keep an open mind in the future. Commented Jan 30, 2018 at 15:12
  • What if you have two of the same kind of component?
    – Erik Eidt
    Commented Jan 30, 2018 at 15:14
  • You might want to include en example diagram as reference: uml-diagrams.org/component-diagrams.html Commented Jan 30, 2018 at 15:15
  • @ErikEidt, Really depends on what the component does. In most real-world cases, it doesn't (and shouldn't) matter which instance you use. That's how the cloud can spin up resources to handle surge traffic. Multiple instances of the same service spun up to distribute load. Commented Jan 30, 2018 at 15:17

2 Answers 2


Can the port of a UML component only be a singleton?

According to https://www.uml-diagrams.org/port.html UML does have notation to indicate multiple instances of a port, as well as multiple interfaces to a port.

Multiplicity of a port (if any) is shown after the port name in square brackets. Both name and multiplicity of port are optional.

UML Port with multiplicity - Library Services class has 1 to 6 searchPort ports.

Library Services class has 1 to 6 searchPort ports.

In UML diagrams, you will most likely see the majority of diagrams with one port. From a logical perspective, this is correct. From an implementation perspective, it shouldn't matter.

Logically, you are connecting to one instance of a component that exposes functionality on the port. In real life there may be multiple instances of the same component and you only care about the interaction with one of them.

  • What you mean in the last sentence, is that there is one port for each instance, isn't it ?
    – Christophe
    Commented Jan 30, 2018 at 22:51
  • The multiplicity of ports in the diagram is per instance of the component. They are not, to my knowledge, global constraints. Commented Jan 31, 2018 at 13:29
  • yes, I fully agree ! This is the right answer ! An uml port is not necessarily an ip port, nor a service queue.
    – Christophe
    Commented Jan 31, 2018 at 13:31
  • In the example with Library search, it's not 100% certain why there are 6 ports, but we can assume it's for availability. Does the client accessing the service have to address it as searchPort[1] or searchPort[2], or does the LibraryServices balance the access? In UML it seems outside elements are aware of these names and would be the ones to decide. Six named instances (using their array index) is not very different from six different ports. Gotta love UML for generating heat. Commented Feb 1, 2018 at 15:51

Singelton as a concept

Yes, since the port is a limited resource there should be only one object at runtime (a singleton) interacting with it. It should handle concurrent access of other components to that port.

So your application should create only one instance of the "PortHandler".

Singelton as Code pattern

Often the logical singelton as described above is realized in code using the Singelton Pattern (private constructor and static accessor method)

This should be avoided.

The reason is that this static access makes your code tight coupled and therefore hard to reuse and in consequence hard to test. Also this "code supported singeltons" are merely a global variables which are known being evil almost since the beginning of programming...

e.g.: if you want to unittest your code interacting with the "PortHandler" you might want to replace the real "PortHandler" with a test double which you can programmatically configure to produce well defined data for your tested code.

If your Code under test gets the "PortHandler" via the static accessor method of the Singelton Pattern it is hard to replace.

If you pass the "PortHandler" in via dependency injection (using a DI framework by any chance) replacing it for testing is easy...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.