I encountered with the following code in the work. The MyItemCoordinator
should receive MyItem
objects, process them, add them to MyItemList
collection and then pass them to the listeners.
My questions are:
- When I saw
NewMyItem
method I thought thatMyItemCoordinator
is a factory but when I sawNewMyItem
's implementation I found out that it creates, sorts, calling to listeners and it's notstatic
. SoMyItemCoordinator
is not a factory andNewMyItem
method does way more things that it should to do according to its name, right? If it's not a factory so why
NewMyItem
method receives so many params, creates an object, adds to the collection? Why not to do:example:
var coordinator = new MyItemCoordinator(); var myItem = new MyItem(...PARAMS..) coordinator.AddMyItemToList(myItem)
Inside
AddMyItemToList
we can sort and call the listeners. This is more clear way to do, isn't it?So, is this implementation correct? Doesn't it have something wrong?
public class MyItemCoordinator { ... public IList<MyItem> MyItemList = new List<MyItem>(); public void NewMyItem(int id, ...15_MORE_PARAMS) { MyItem myItem = new MyItem(id,...ONLY_5_PARAMS_WITHIN_15_ARE_USED); AddMyItemToList(myItem); SortMyItemList(); if (MyItemDetectedHandler != null) MyItemDetectedHandler(this); } public void AddMyItemToList(MyItem myItem) { //makes some checking and adds myItem to MyItemList } ... }
MyItemList
allows direct manipulation of the internal state, bypassingAddMyItemToList
etc. \$\endgroup\$