I would like to draw a UML sketch to communicate part of a software but the type of relationship between classes does not seem very clear cut to me.
Let's say I want to periodically fetch the recent orders from the database and update a graphical view that has the order details.
This is a highly simplified pseudo code example but my implementation is java
//Order objects contain information about a single order
class Order
{
private String orderNumber;
private String buyerName;
public Order(String number, String name)
{
//initialize fields
}
//public getters
}
now I have a class called Model that has a single static function for fetching orders in a time frame:
class Model
{
private Model()
{}
public static List<Order> getOrders(LocalDateTime from, LocalDateTime until)
{
//...get order objects from database and return
}
}
Model.getOrders()
is accessed by my Controller class which is in charge of fetching Order
objects and updating a view periodically
class Controller implements Runnable
{
private RecentOrdersView target; //view to update
private Controller()
{
}
public Controller getInstance(RecentOrdersView input)
{
//...singleton
//compose the returned object with the view, so it needs which view to update
}
public void Run()
{
//... client code runs this function periodically
List<Order> recentOrders = Model.getOrders(...);//get Orders
for(int i=0; i<recentOrders.size(); i++)
{
target.updateView(recentOrders.get(i).getOrderNumebr(),
recentOrders.get(i).getBuyerName());
}
}
}
As you might have guessed there is a class for my view as follows:
class RecentOrdersView
{
private RecentOrdersView()
{
}
public RecentOrdersView getInstance()
{
//...singleton implementation
}
public void updateView(String orderNumber, String buyerName,...)
{
//...update the view based on the parameters
}
}
so the controller and the view are implemented as singletons, but the model is accessed through public static function. Client code is responsible for putting it together:
class Client
{
public static void main(String[] args)
{
RecentOrderView view = RecentOrdersView.getInstance();
Runnable controller= Controller.getInstance(view);
//schedule controller to run periodically
}
}
Now I want to create a UML diagram for this setup, however I don't know if I should use aggregation or composition between Controller
, Model
and Order
my best guess looks like this:
I have selected composition relationship between Client
and RecentOrdersView
and Controller
, because Client
owns an instance of each. I have also selected an aggregation relationship between RecentOrdersView
and Controller
, because Controller
holds a reference of RecentOrdersView
(even though it does not own it).
where I am confused is the relationships between Order
, Controller
and Model
. Specifics:
Relationship between Model
and Controller
:
I know that Controller
calls a static function in Model
, but since there is no object instances, I am unsure what to do
Relationship between Model
and Order
: I almost want to use composition because Model
instantiates Order
objects, but since this happens in a static function there is no immediate owner, the Order
objects get passed back to Controller
Relationship between Controller
and Order
:
inside Controller
method Run()
is where Order
objects fall out of scope, but Controller
object is not the one that directly instantiated those objects. should I be using composition here?
Thank you for reading this long question. I also highly appreciate advice about the overall design pattern used.