Reputation: 1692
I have an application that consists of a server that can have multiple clients of two types, user clients, and device clients. To do this, I have a vector of the client base class where I will add the new clients as they connect to the server. This is a simplified version of what I have:
class Client
{
protected:
int m_port;
Client(int port): m_port{port}{}
public:
virtual ~Client(){}
}
class User: public Client
{
public:
User(int port): Client{port}{}
virtual ~User(){}
}
class Device: public Client
{
public:
User(int port): Client{port}{}
virtual ~Device(){}
}
class Server
{
public:
Server(int port): m_port{port}{}
~Server()
{
for (std::vector<Client*>::iterator it = m_clients.begin();
it != m_clients.end();
it++){
free(m_clients.pop_back());
}
}
void run()
{
if(m_port == 1){
m_clients.pushback(new User(port));
}else{
m_clients.pushback(new Device(port));
}
}
private:
int m_port;
std::vector<Client*> m_clients;
}
And I have the following UML representation:
My doubt here is if this diagram represents the correct intent. Maybe I'm not that experienced with UML and this is enough, but I think this diagram doesn't clearly depict the fact that the vector in the server will contain User(s) and Device(s) and not Client(s). I don't know if I need to put an extra stereotype label, an extra note, or an extra link or association like this:
So, how would this c++ class relationship be correctly represented in UML?
Upvotes: 1
Views: 1639
Reputation: 32594
I think this diagram doesn't clearly depict the fact that the vector in the server will contain User(s) and Device(s) and not Client(s). I don't know if I need to put an extra stereotype label, an extra note, or an extra link or association
you just have to say the class Client is abstract in your first diagram, you can show that writting its name in italic in the diagram. Do not add associations as you did in the second diagram.
Warning m_client
is private in Server but you used a + rather than a -
You use a composition between Server and Client, are you sure this is is the case and then the clients disappear when the server disappears ? There is nothing in the C++ code doing that. (see OP remark)
Having drawn m_client
as an association it is not necessary to also show it as an attribute.
The clients do not know the server, this is more visible drawing the association non bidirectional (i.e. Server -------> Client
).
The C++ definition uses a vector where the elements are ordered, you can indicate the property is ordered using {ordered}
Because of the C++ definition of run with if(m_port == 1)
a server has client(s) or-exclusive device(s), you can indicate that through a constraint
<<ctor>
and <<vector>>
are not defined in the standard but I have nothing against their use.
Upvotes: 2