The display module of a computer should be flexible, so we can change it easily, or we can change it dynamically (at runtime). We can say the display module is like a strategy, and a client can change the strategy at any time. Technically, the display is a function or contract, but it can have multiple implementations, so it's better to create this contract as an interface, and every implementation will provide its own display mechanism so at runtime, the Computer can call it.
So our Java solution looks like the following:
interfacedisplayModule
{
publicvoiddisplay();
}
publicclassMonitorimplementsdisplayModule
{
publicvoiddisplay()
{
System.out.println(“DisplaythroughMonitor”);
}
}
publicclassProjectorimplementsdisplayModule
{
publicvoiddisplay()
{
System.out.println(“Displaythroughprojector”);
}
}
publicclassComputer
{
displayModuledm;// programming through interface as variable part
No comments:
Post a Comment