When cooking various kinds of food particularly meats theres often more than

When cooking various kinds of food particularly meats

This preview shows page 2 - 4 out of 6 pages.

When cooking various kinds of food, particularly meats, there's often more than one way to cook them to safe eating temperatures. For example, you might grill them, bake them, deep-fry them, or broil them, depending on whether you have friends over, how much you want to show off your cooking skills, and how many burns you are willing to suffer. Each of these methods will get the item cooked, just via different processes. These processes, in object-oriented code, can each be their own class.
Image of page 2
In our example, let's pretend that we'll ask the user what method they'd like to use to cook their food, and then implement that method using the Strategy design pattern. First, let's write up the Strategy participant, which for our demo is an abstract class. /// <summary> /// The Strategy abstract class, which defines an interface common to all supported strategy algorithms. /// </summary> abstract class CookStrategy { public abstract void Cook(string food); } Each strategy by which we will cook a food item must implement the method Cook() . Let's implement a few of those strategies now (these are all ConcreteStrategy participants): /// <summary> /// A Concrete Strategy class /// </summary> class Grilling : CookStrategy { public override void Cook(string food) { Console.WriteLine("\nCooking " + food + " by grilling it."); } } /// <summary> /// A Concrete Strategy class /// </summary> class OvenBaking : CookStrategy { public override void Cook(string food) { Console.WriteLine("\nCooking " + food + " by oven baking it."); } } /// <summary> /// A Concrete Strategy class /// </summary> class DeepFrying : CookStrategy { public override void Cook(string food) { Console.WriteLine("\nCooking " + food + " by deep frying it");
Image of page 3
Image of page 4

You've reached the end of your free preview.

Want to read all 6 pages?

  • Summer '17
  • Likeleli

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Ask Expert Tutors You can ask You can ask ( soon) You can ask (will expire )
Answers in as fast as 15 minutes