My question is related class design. It is really hard to explain everything here so I am taking an example which is similar to my problem.
Requirements: Customer can execute a single action or he can combine multiple actions.
Based on the above requirements, the classes were structured as below:
interface ICustAction
{
void Execute();
}
class CustAction:ICustAction
{
// this class contains a single action to be performed
public void Execute()
{
//should execute customer action
}
}
class CustActionCollction:ICustAction
{
// list of actions to be performed
private List<CustAction> _custActions;
public void Execute()
{
//should execute all the customer action in the list
}
}
class ExecutionEngine
{
public void Execute(ICustAction action)
{
//executes customer action/ customer action collection based on reference
action.Execute();
}
}
New Requirement:
Customer should be able to add CustActionCollection inside CustActionCollection.
Example:
Customer action collection:
customer action 1:
customer action 2:
customer action collection 1: (this action collection might have multiple customer actions)
customer action 3:
Based on these requirements, the updated CustActionCollection class looks like this
class CustActionCollction:ICustAction
{
// list of actions to be performed
private List<ICustAction> _custActions;
public void Execute()
{
//should execute all the customer action in the list
}
}
Now here is the new requirement where I need expert opinion.
I need to check whether a particular action is defined more than once in a customer action collection??
Customer action collection:
customer action 1:
customer action 2:
customer action collection 1: (this action collection might have multiple customer actions)
customer action 3:
SO, the check should be recursive including all child collection.
Question:
Should I add this check method inside the interface or it should be exclusively for the customer action collection class??
A)If the method is defined in the interface then what will be the signature of the method then implementation for both classes?
B)If the method is not in the interface then what will be the implementation of the method?
I’m Sorry, if I could not explain it properly.
If I understand your question it seems that checking for duplicates will only need to happen in the case of a
CustActionCollection. As a result I think you best implementation is to:CustActionequality. (Doesn’t need to be but makes the most sense in my opinion).In the
CustCollection.evaluate()method, generate a master List of allCustActionsand check for duplicates. It will look something like this (this in the classCustActionCollection):