SlideShare ist ein Scribd-Unternehmen logo
1 von 14
Downloaden Sie, um offline zu lesen
Class report




  CLASS ASSIGNMENT- 1
       WAR PLANNING
  (STRATEGY PATTERN)




           Submitted by:

 Md.Mahedi Mahfuj          -BIT 0207



           Submitted to:

          Maeenul Islam

           Lecturer,IIT




Institute of Information Technology
       University of Dhaka


                                       Page 1 of 14
Class report




                     War Planning:


Introduction:
 At first, we have to see what happens when a war is being undertaken in
certain circumstances, i.e. what features we have to focus on.



Actually, the war is called by the king of a certain region. He commands the
commander of the battalion under whom a huge number of soldiers are
working all the time.



Now, let’s see the entire war through technical perspectives. Before, going
through the scenario, we have to know what a client code is.




Client Code:


The part of the code that controls the behavior of the other classes is known
as the client code of those classes. As an instance,




                                                                   Page 2 of 14
Class report



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 and
soldiers. So, the Main() function here plays the role of the King.

Now, let’s see what can be in the commander part :

Commander:
Name:-

Command:-

Command();

Soldierlist;

AddSoldier(soldier s);

Soldierlist.add (s ) {
                                                                  Page 3 of 14
Class report

StartWar () {

S1.Changemode (“Aggression”);

S2.Changemode (“Defensive”);

S3.Changemode (“Aggression”);

…….

……..

……..

………

}

And, in the soldier part:

Soldier:
Name:-

Mode:-

Function:-

Fight ();

Changemode ();

…..

…..

……




                                               Page 4 of 14
Class report



Now, let’s see what we mostly do in such cases:

Soldier{

Changemode (String m)

{

Mode=m;      //mode change

Fight ();

}

Fight ()

{

//according to mode, fight now.

If(mode=”Aggressive”)

{

…………

…………//about 1000 line code

………….

}

Else if (mode=”Defensive”)

{

………….

………….//about 1000 line code again

…………..


                                                  Page 5 of 14
Class report

}}



Problems:
Now, let’s see what the problems are that we may face in solving in the above
mentioned way:

At first,

        If we add some more fighting modes like neutral, some only carrying
foods & weapons etc then we have to add all such things in the same class
soldier. Hence, the soldier class will eventually become huge which is not
suitable for handling a good code.

Then,

       If we separate the modes into distinguished classes, then another
problem will arise, ambiguity.

        The modes will be described in three different functions by three
different programmers who contributed in coding the codes of three different
classes namely soldier, aggressive & defensive respectively.

Then,

        If we keep the mode as string/integer , then for every change in
different fighting modes, we have to change in soldier class also which is not
expected at all.



Solution:
Let’s troubleshoot the problems mentioned above:

For solving the first problem,

                           We have to separate the fighting modes into
classes & then all possible changes in modes will be done in the specific
                                                                    Page 6 of 14
Class report

classes. So, The coder of soldier will not be disturbed for any change in the
fighting mode.

Class Aggressive{

}

Class Defensive{

}

Class Mode3{

}

Class ……..

……..

……..

………

In this way, we can add different modes as much as possible whenever
needed.

Now, let’s troubleshoot the second problem.

For troubleshooting that,

We have to fix a specific function & every class should be made bound to use
that specific function. For this we can declare an interface & every class should
implement that interface.

Interface Ifight {

Fight();}

Class Aggressive implements Ifight {

Fight () {

Print (“I am fighting in aggressive mode”) ;}}

                                                                       Page 7 of 14
Class report

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{

……………

……………

……………

So, every class here is bound to use the function fight().So, three different
coders coding three different classes do not have to depend on each other.

Now,another question may come…..Why are we using an interface, not an
abstract class?

The very answer is;

In an abstract class there can be an abstract method, a defined method
and variables as well.But,here we only have to declare a function,that’s
all.So, we have no need to declare an abstract class,only an interface is
enough,i.e. minimal. For this very reason, we have used an interface
instead of using an abstract class.




                                                                   Page 8 of 14
Class report

Now,let’s fix the third problem.

If we keep modes as string/integer then such problems will arise often. So, it
is better to use the modes as object. Only then, such problems will never
happen 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())

……..

……..

………

S1.Changemode (new Mode3())

………

………

………

This use of modes as object has made the code more flexible & well-
maintained.




                                                                    Page 9 of 14
Class report

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();

}

StartWar () {

S1.Changemode (“Aggression”);

S2.Changemode (“Defensive”);

S3.Changemode (“Aggression”);

…….

……..

……..

}}


                                                            Page 10 of 14
Class report



Soldier Class:

Soldier{

Name:

Soldier () {

}

Changemode (Ifight fm) {

FightingMode = fm;

FightingMode.fight ();

}

Interface Ifight

{

fight ();

}



Aggressive Class:

Class Aggressive implements Ifight {

Fight () {

Print (“I am fighting in aggressive mode”) ;

}

}




                                                Page 11 of 14
Class report

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:

………

………

………

Commander Class:

S1.ChangeMode (new Aggressive())

………

………

………

S1.Changemode (new Defensive())

……..


                                                Page 12 of 14
Class report

……..

………

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 13 of 14
Class report




               Page 14 of 14

Weitere ähnliche Inhalte

Andere mochten auch

New microsoft office word 97 2003 document
New microsoft office word 97   2003 documentNew microsoft office word 97   2003 document
New microsoft office word 97 2003 documentRajib Paul
 
Mbl annual report_2015
Mbl annual report_2015Mbl annual report_2015
Mbl annual report_2015Rajib Paul
 
презентация глагол To be в past simple (4 кл.)
презентация глагол To be в past simple (4 кл.)презентация глагол To be в past simple (4 кл.)
презентация глагол To be в past simple (4 кл.)KirsanovaNatalia
 
Office of Utilities Regulation - Jamaica
Office of Utilities Regulation - JamaicaOffice of Utilities Regulation - Jamaica
Office of Utilities Regulation - Jamaicagarth2101
 

Andere mochten auch (7)

New microsoft office word 97 2003 document
New microsoft office word 97   2003 documentNew microsoft office word 97   2003 document
New microsoft office word 97 2003 document
 
Mbl annual report_2015
Mbl annual report_2015Mbl annual report_2015
Mbl annual report_2015
 
презентация глагол To be в past simple (4 кл.)
презентация глагол To be в past simple (4 кл.)презентация глагол To be в past simple (4 кл.)
презентация глагол To be в past simple (4 кл.)
 
Office of Utilities Regulation - Jamaica
Office of Utilities Regulation - JamaicaOffice of Utilities Regulation - Jamaica
Office of Utilities Regulation - Jamaica
 
Mediator pattern
Mediator patternMediator pattern
Mediator pattern
 
Observer pattern
Observer patternObserver pattern
Observer pattern
 
Clustering manual
Clustering manualClustering manual
Clustering manual
 

Mehr von Md. Mahedi Mahfuj

Mehr von Md. Mahedi Mahfuj (20)

Bengali optical character recognition system
Bengali optical character recognition systemBengali optical character recognition system
Bengali optical character recognition system
 
Parallel computing chapter 3
Parallel computing chapter 3Parallel computing chapter 3
Parallel computing chapter 3
 
Parallel computing chapter 2
Parallel computing chapter 2Parallel computing chapter 2
Parallel computing chapter 2
 
Parallel computing(2)
Parallel computing(2)Parallel computing(2)
Parallel computing(2)
 
Parallel computing(1)
Parallel computing(1)Parallel computing(1)
Parallel computing(1)
 
Message passing interface
Message passing interfaceMessage passing interface
Message passing interface
 
Advanced computer architecture
Advanced computer architectureAdvanced computer architecture
Advanced computer architecture
 
Parallel searching
Parallel searchingParallel searching
Parallel searching
 
Matrix multiplication graph
Matrix multiplication graphMatrix multiplication graph
Matrix multiplication graph
 
Database management system chapter16
Database management system chapter16Database management system chapter16
Database management system chapter16
 
Database management system chapter15
Database management system chapter15Database management system chapter15
Database management system chapter15
 
Database management system chapter12
Database management system chapter12Database management system chapter12
Database management system chapter12
 
Strategies in job search process
Strategies in job search processStrategies in job search process
Strategies in job search process
 
Report writing(short)
Report writing(short)Report writing(short)
Report writing(short)
 
Report writing(long)
Report writing(long)Report writing(long)
Report writing(long)
 
Job search_interview
Job search_interviewJob search_interview
Job search_interview
 
Apache hadoop & map reduce
Apache hadoop & map reduceApache hadoop & map reduce
Apache hadoop & map reduce
 
Basic and logical implementation of r language
Basic and logical implementation of r language Basic and logical implementation of r language
Basic and logical implementation of r language
 
Map reduce
Map reduceMap reduce
Map reduce
 
R with excel
R with excelR with excel
R with excel
 

Kürzlich hochgeladen

Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 

Kürzlich hochgeladen (20)

Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 

Strategy pattern.pdf

  • 1. Class report CLASS ASSIGNMENT- 1 WAR PLANNING (STRATEGY PATTERN) Submitted by: Md.Mahedi Mahfuj -BIT 0207 Submitted to: Maeenul Islam Lecturer,IIT Institute of Information Technology University of Dhaka Page 1 of 14
  • 2. Class report War Planning: Introduction: At first, we have to see what happens when a war is being undertaken in certain circumstances, i.e. what features we have to focus on. Actually, the war is called by the king of a certain region. He commands the commander of the battalion under whom a huge number of soldiers are working all the time. Now, let’s see the entire war through technical perspectives. Before, going through the scenario, we have to know what a client code is. Client Code: The part of the code that controls the behavior of the other classes is known as the client code of those classes. As an instance, Page 2 of 14
  • 3. Class report 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 and soldiers. So, the Main() function here plays the role of the King. Now, let’s see what can be in the commander part : Commander: Name:- Command:- Command(); Soldierlist; AddSoldier(soldier s); Soldierlist.add (s ) { Page 3 of 14
  • 4. Class report StartWar () { S1.Changemode (“Aggression”); S2.Changemode (“Defensive”); S3.Changemode (“Aggression”); ……. …….. …….. ……… } And, in the soldier part: Soldier: Name:- Mode:- Function:- Fight (); Changemode (); ….. ….. …… Page 4 of 14
  • 5. Class report Now, let’s see what we mostly do in such cases: Soldier{ Changemode (String m) { Mode=m; //mode change Fight (); } Fight () { //according to mode, fight now. If(mode=”Aggressive”) { ………… …………//about 1000 line code …………. } Else if (mode=”Defensive”) { …………. ………….//about 1000 line code again ………….. Page 5 of 14
  • 6. Class report }} Problems: Now, let’s see what the problems are that we may face in solving in the above mentioned way: At first, If we add some more fighting modes like neutral, some only carrying foods & weapons etc then we have to add all such things in the same class soldier. Hence, the soldier class will eventually become huge which is not suitable for handling a good code. Then, If we separate the modes into distinguished classes, then another problem will arise, ambiguity. The modes will be described in three different functions by three different programmers who contributed in coding the codes of three different classes namely soldier, aggressive & defensive respectively. Then, If we keep the mode as string/integer , then for every change in different fighting modes, we have to change in soldier class also which is not expected at all. Solution: Let’s troubleshoot the problems mentioned above: For solving the first problem, We have to separate the fighting modes into classes & then all possible changes in modes will be done in the specific Page 6 of 14
  • 7. Class report classes. So, The coder of soldier will not be disturbed for any change in the fighting mode. Class Aggressive{ } Class Defensive{ } Class Mode3{ } Class …….. …….. …….. ……… In this way, we can add different modes as much as possible whenever needed. Now, let’s troubleshoot the second problem. For troubleshooting that, We have to fix a specific function & every class should be made bound to use that specific function. For this we can declare an interface & every class should implement that interface. Interface Ifight { Fight();} Class Aggressive implements Ifight { Fight () { Print (“I am fighting in aggressive mode”) ;}} Page 7 of 14
  • 8. Class report 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{ …………… …………… …………… So, every class here is bound to use the function fight().So, three different coders coding three different classes do not have to depend on each other. Now,another question may come…..Why are we using an interface, not an abstract class? The very answer is; In an abstract class there can be an abstract method, a defined method and variables as well.But,here we only have to declare a function,that’s all.So, we have no need to declare an abstract class,only an interface is enough,i.e. minimal. For this very reason, we have used an interface instead of using an abstract class. Page 8 of 14
  • 9. Class report Now,let’s fix the third problem. If we keep modes as string/integer then such problems will arise often. So, it is better to use the modes as object. Only then, such problems will never happen 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()) …….. …….. ……… S1.Changemode (new Mode3()) ……… ……… ……… This use of modes as object has made the code more flexible & well- maintained. Page 9 of 14
  • 10. Class report 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(); } StartWar () { S1.Changemode (“Aggression”); S2.Changemode (“Defensive”); S3.Changemode (“Aggression”); ……. …….. …….. }} Page 10 of 14
  • 11. Class report Soldier Class: Soldier{ Name: Soldier () { } Changemode (Ifight fm) { FightingMode = fm; FightingMode.fight (); } Interface Ifight { fight (); } Aggressive Class: Class Aggressive implements Ifight { Fight () { Print (“I am fighting in aggressive mode”) ; } } Page 11 of 14
  • 12. Class report 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: ……… ……… ……… Commander Class: S1.ChangeMode (new Aggressive()) ……… ……… ……… S1.Changemode (new Defensive()) …….. Page 12 of 14
  • 13. Class report …….. ……… 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 13 of 14
  • 14. Class report Page 14 of 14