Created
October 1, 2018 17:53
-
-
Save sevperez/7b85dedfd2d714c613724cb25c3ad47a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace dip_2 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var bakery = new Restaurant("Bakery", new Oven()); | |
bakery.Cook("cookies"); | |
var crepery = new Restaurant("Crepery", new Stove()); | |
crepery.Cook("crepes"); | |
} | |
} | |
class Restaurant | |
{ | |
public string Name { get; set; } | |
public ICooker Cooker { get; set; } | |
public Restaurant(string name, ICooker cooker) | |
{ | |
this.Name = name; | |
this.Cooker = cooker; | |
} | |
public void Cook(string item) | |
{ | |
this.Cooker.TurnOn(); | |
this.Cooker.Cook(item); | |
this.Cooker.TurnOff(); | |
} | |
} | |
interface ICooker | |
{ | |
bool On { get; set; } | |
void TurnOn(); | |
void TurnOff(); | |
void Cook(string item); | |
} | |
class Oven : ICooker | |
{ | |
public bool On { get; set; } | |
public void TurnOn() | |
{ | |
Console.WriteLine("Turning on the oven!"); | |
this.On = true; | |
} | |
public void TurnOff() | |
{ | |
Console.WriteLine("Turning off the oven!"); | |
this.On = false; | |
} | |
public void Cook(string item) | |
{ | |
if (!this.On) | |
{ | |
Console.WriteLine("Oven not turned on."); | |
} | |
else | |
{ | |
Console.WriteLine("Now baking " + item + "!"); | |
} | |
} | |
} | |
class Stove : ICooker | |
{ | |
public bool On { get; set; } | |
public void TurnOn() | |
{ | |
Console.WriteLine("Turning on the stove!"); | |
this.On = true; | |
} | |
public void TurnOff() | |
{ | |
Console.WriteLine("Turning off the stove!"); | |
this.On = false; | |
} | |
public void Cook(string item) | |
{ | |
if (!this.On) | |
{ | |
Console.WriteLine("Stove not turned on."); | |
} | |
else | |
{ | |
Console.WriteLine("Now frying " + item + "!"); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment