Sorry if someone asked this already, but I did not found a question with this specific scenario:
I have an entity with an status and a type. For each status and for each type, I should show the user a different page, and this page is some kind complex to create so it is created with the builder pattern. But, in some cases, these pages could be the same. And with some status, I do not need to check the type.
Some examples that could occur:
I thought about implementing a factory (with an switch case for each status) that will create and return the result of an abstract factory (with a factory for each type). But, I will need some abstract classes to solve the "same page" problem in between these factories.
Do I really need this complex structure?
You want to display page based on different status with Type. So your internal page calling will change based on your context. So, I will suggest you implement this behavior with Strategy Design Pattern.
Below code implementation in C# as per my understanding of your problem statement:
public interface ISomeOperation
{
string DisplayPage(int status);
}
public class Type : ISomeOperation
{
public string DisplayPage(int status)
{
if (status == 1)
return "1";
return string.Empty;
}
}
public class TypeA : ISomeOperation
{
public string DisplayPage(int status)
{
if (status == 2)
return "2A";
if (status == 3)
return "3A";
if (status == 4)
return "4A";
return string.Empty;
}
}
public class TypeB: ISomeOperation
{
public string DisplayPage(int status)
{
if (status == 2)
return "2B";
if (status == 3)
return "3B";
if (status == 4)
return "4B";
return string.Empty;
}
}
public class TypeC : ISomeOperation
{
public string DisplayPage(int status)
{
if (status == 2)
return "2C";
if (status == 3)
return "3C";
if (status == 4)
return "4C";
return string.Empty;
}
}
public class OperationContext
{
private readonly ISomeOperation _iSomeOperation;
public OperationContext(ISomeOperation someOperation)
{
_iSomeOperation = someOperation;
}
public string DisplayPageResult(int status)
{
return _iSomeOperation.DisplayPage(status);
}
}
Driver Code:
class Program
{
static void Main(string[] args)
{
var operationContext = new OperationContext(new Type());
operationContext.DisplayPageResult(1);
operationContext = new OperationContext(new TypeB());
operationContext.DisplayPageResult(2);
operationContext.DisplayPageResult(3);
operationContext.DisplayPageResult(4);
operationContext = new OperationContext(new TypeC());
operationContext.DisplayPageResult(2);
operationContext.DisplayPageResult(3);
operationContext.DisplayPageResult(4);
}
}