Your SlideShare is downloading. ×
  • Like
Strategy pattern
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Now you can save presentations on your phone or tablet

Available for both IPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Strategy pattern

  • 494 views
Published

 

Published in Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
494
On SlideShare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
4
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Class report CLASS ASSIGNMENT- 1 STRATEGY PATTERN War Planning:Introduction: At first, we have to see what happens when a war is being undertaken incertain circumstances, i.e. what features we have to focus on.Actually, the war is called by the king of a certain region. He commands thecommander of the battalion under whom a huge number of soldiers areworking all the time.Now, let’s see the entire war through technical perspectives. Before, goingthrough the scenario, we have to know what a client code is. Page 1 of 12
  • 2. Class reportClient Code:The part of the code that controls the behavior of the other classes is knownas the client code of those classes. As an instance,War {Main () {Commander cm = new commander ();Soldier s1 = new soldier ();Soldier s2 = new soldier ();Soldier s3 = new soldier ();cm. AddSoldier(s1);cm. AddSoldier(s2);cm. AddSoldier(s3);cm. StartWar();}}So, we can see that this client code Main () can control both commanders andsoldiers. So, the Main() function here plays the role of the King.Now, let’s see what can be in the commander part : Page 2 of 12
  • 3. Class reportCommander:Name:-Command:-Command();Soldierlist;AddSoldier(soldier s);Soldierlist.add (s ) {StartWar () {S1.Changemode (“Aggression”);S2.Changemode (“Defensive”);S3.Changemode (“Aggression”);…….……..……..………}And, in the soldier part:Soldier:Name:-Mode:-Function:-Fight (); Page 3 of 12
  • 4. Class reportChangemode ();…..…..……Now, let’s see what we mostly do in such cases:Soldier{Changemode (String m){Mode=m; //mode changeFight ();}Fight (){//according to mode, fight now.If(mode=”Aggressive”){……………………//about 1000 line code Page 4 of 12
  • 5. Class report………….}Else if (mode=”Defensive”){………….………….//about 1000 line code again…………..}}Problems:Now, let’s see what the problems are that we may face in solving in the abovementioned way:At first, If we add some more fighting modes like neutral, some only carryingfoods & weapons etc then we have to add all such things in the same classsoldier. Hence, the soldier class will eventually become huge which is notsuitable for handling a good code.Then, If we separate the modes into distinguished classes, then anotherproblem will arise, ambiguity. The modes will be described in three different functions by threedifferent programmers who contributed in coding the codes of three differentclasses namely soldier, aggressive & defensive respectively. Page 5 of 12
  • 6. Class reportThen, If we keep the mode as string/integer , then for every change indifferent fighting modes, we have to change in soldier class also which is notexpected at all.Solution:Let’s troubleshoot the problems mentioned above:For solving the first problem, We have to separate the fighting modes intoclasses & then all possible changes in modes will be done in the specificclasses. So, The coder of soldier will not be disturbed for any change in thefighting mode.Class Aggressive{}Class Defensive{}Class Mode3{}Class ……..……..……..………In this way, we can add different modes as much as possible wheneverneeded. Page 6 of 12
  • 7. Class reportNow, let’s troubleshoot the second problem.For troubleshooting that,We have to fix a specific function & every class should be made bound to usethat specific function. For this we can declare an interface & every class shouldimplement that interface.Interface Ifight {Fight();}Class Aggressive implements Ifight {Fight () {Print (“I am fighting in aggressive mode”) ;}}Class Defensive implements Ifight{Fight () {Print (“I am fighting in defensive mode”) ;}Class Mode3 implements Ifight{Fight () {Print (“I am fighting in neutral mode”) ;}Class Mode4 implements Ifight{……………………………………… Page 7 of 12
  • 8. Class reportSo, every class here is bound to use the function fight().So, three differentcoders coding three different classes do not have to depend on each other.Now,another question may come…..Why are we using an interface, not anabstract class?The very answer is;In an abstract class there can be an abstract method, a defined methodand variables as well.But,here we only have to declare a function,that’sall.So, we have no need to declare an abstract class,only an interface isenough,i.e. minimal. For this very reason, we have used an interfaceinstead of using an abstract class.Now,let’s fix the third problem.If we keep modes as string/integer then such problems will arise often. So, itis better to use the modes as object. Only then, such problems will neverhappen in near future.Like, in the commander class we can use the modes as objects.Commander Class:S1.Changemode (new Aggressive())…..…….…….S1.Changemode (new Defensive())…….. Page 8 of 12
  • 9. Class report……..………S1.Changemode (new Mode3())………………………This use of modes as object has made the code more flexible & well-maintained.Main Code Sample:Hence, the main code should have the following structure:War Class:War {Main () {Commander cm = new commander ();Soldier s1 = new soldier ();Soldier s2 = new soldier ();Soldier s3 = new soldier ();cm. AddSoldier(s1);cm. AddSoldier(s2);cm. AddSoldier(s3);cm. StartWar(); Page 9 of 12
  • 10. Class report}StartWar () {S1.Changemode (“Aggression”);S2.Changemode (“Defensive”);S3.Changemode (“Aggression”);…….……..……..}}Soldier Class:Soldier{Name:Soldier () {}Changemode (Ifight fm) {FightingMode = fm;FightingMode.fight ();}Interface Ifight{fight ();} Page 10 of 12
  • 11. Class reportAggressive Class:Class Aggressive implements Ifight {Fight () {Print (“I am fighting in aggressive mode”) ;}}Defensive Class:Class Defensive implements Ifight{Fight () {Print (“I am fighting in defensive mode”) ;}}Mode3 Class:Class Mode3 implements Ifight{Fight () {Print (“I am fighting in neutral mode”) ;}}Mode4 Class:……………… Page 11 of 12
  • 12. Class report………Commander Class:S1.ChangeMode (new Aggressive())………………………S1.Changemode (new Defensive())……..……..………S1.Changemode (new Mode3())………………In this way, we can easily diminish all the problems that we have faced earlier.This way of solving a problem is known as “Strategy Pattern”.……………………………………………………………………………..X………………………………………………………………………………… Page 12 of 12