What is a strategy pattern? Implement it.



What is a strategy pattern? Implement it...

Answer / guest

Strategy pattern is one of the 23 design patterns
available. The basic concept of strategy pattern is object
composition at runtime. When you have multiple classes each
having similar behaviour but the behaviour could change
based on an external factor or the object itself. For
example take a car, a car can have an engine, a steering
wheel, 4 tyres e.t.c but a remote control car will not have
an engine but will have the other characteristics of a
normal car. For this case let us have the following code:
Interface HasEngine
{
void Engine();
}
Interface Tyres
{
void 4Tyres();
}

class Hummer:HasEngine,Tyres
{
override void Engine()
{
Console.WriteLine("Has Engine");
}
override void 4Tyres()
{
Console.WriteLine("Has 4 tyres");
}
}
class RemoteCar:HasEngine,Tyres
{
override void Engine()
{
Console.WriteLine("Has no Engine");
}
override void 4Tyres()
{
Console.WriteLine("Has 4 tyres");
}
}
class Car
{
HasEngine HE;
Tyres T;
setEngine(HasEngine obj)
{
HE=obj;
}
setTyres(Tyres obj)
{
T=obj;
}
}
class MainClass:Car
{
HasEngine he;
Tyres tyr;
setInstance()
{
he = new Hummer();
tyr = new Hummer();
}
public static void Main()
{
MainClass mc = new Car();
mc.setInstance();
mc.setEngine(he);
mc.setTyres(tyr);
}
}

Is This Answer Correct ?    1 Yes 1 No

Post New Answer

More Dot Net General Interview Questions

i had attended to infosys interview on 17th april 2010...on .net..3+ experience for Technology Analyst .. to my knowledge i did well in technical and hr whether i loose the interview or still processing is taking place..am confused please. what accuatly would be happend?

0 Answers  


How many namespaces are in .net version 1.1?

0 Answers  


differance between checkbox and rediobutton in vb.net?

1 Answers  


How many types of design patterns available in .NET?

0 Answers  


Difference between machine.config and web.config?

0 Answers  






What is the advantage of .net core?

0 Answers  


What is reflection and what is it for?

0 Answers  


How can you assign an rgb color to a system.drawing.color object?

0 Answers  


What is the base class of Button control?

2 Answers  


Explain about the features and elements present in Visual studio.NET IDE?

0 Answers  


What’s different between process and application in .net?

0 Answers  


Give An example of a ctype and directcast.

2 Answers   Wipro,


Categories