Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

What is a strategy pattern? Implement it.

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


Please Help Members By Posting Answers For Below Questions

Tell us what is json data, and what is one way that .net developers can work with json?

984


Which dll is required to translate xml to sql in internet information server (iis)?

1012


Explain the different types of proxy patterns?

1224


If a dataset contains 100 rows, how to fetch rows between 5 and 15 only?

936


What is interface and abstract class in .net?

1027


How boxing and unboxing occures in memory?

1178


How to get the number after decimal point in .net?

1005


If any body working in Infosys, please can u give me the referense to that company because i hered that their is openings for freshers with referal..

1959


Difference between value type & reference types ?

1071


What method do you use to explicitly kill a users session? How?

1015


What is managed code execution?

1107


Please explain what garbage collection is and how it works. Provide a code example of how you can enforce garbage collection in .net?

972


Explain how does assembly versioning work?

1016


What is an exe and a dll?

1181


How to debug failed assembly binds?

1037