您希望根据类型的不同状态显示页面。因此,您的内部页面调用将根据您的上下文进行更改。所以,我建议你用
.
根据我对您的问题陈述的理解,以下代码以C语言实现:
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);
}
}
驾驶员代码:
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);
}
}