Answer Posted / 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 View All Answers
Explain what is an application domain?
What is singlecall activation mode used for in .net?
Can "this" be used within a static method?
What is interface and abstract class in .net?
Describe the use of following com+ services jit activation, queued components, object pooling.?
Tell us what is json data, and what is one way that .net developers can work with json?
Explain what are the deferred execution and the immediate execution in linq?
Why is .net used?
Can you give an example of when it would be appropriate to use a web service as opposed to a non-serviced .NET component
Write Code for DataSet,Datareader,and by deleting the button gridview should be empty?
Difference between class and interface in .net?
What is the concept of inheritance and how it works in .net?
Describe the Managed Execution Process
How to prepare parametrized (with more than one parameters) crystal report.pls tell me the code procedure, if any body can?
What's wrong with a line like this? Datetime.parse(mystring);