SlideShare una empresa de Scribd logo
1 de 12
Class report




                  CLASS ASSIGNMENT- 1
                   STRATEGY PATTERN




                    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.




                                                                  Page 1 of 12
Class report



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,




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 :



                                                                   Page 2 of 12
Class report

Commander:
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
Class report

Changemode ();

…..

…..

……




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


                                                  Page 4 of 12
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 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.




                                                                   Page 5 of 12
Class report

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
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.


                                                                    Page 6 of 12
Class report

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”) ;}}

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
Class report

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.




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

……..

                                                                    Page 8 of 12
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
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
Class report



Aggressive 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
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

Más contenido relacionado

Destacado (16)

Apache hadoop & map reduce
Apache hadoop & map reduceApache hadoop & map reduce
Apache hadoop & map reduce
 
R with excel
R with excelR with excel
R with excel
 
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
 
Cloud testing
Cloud testingCloud testing
Cloud testing
 
Observer pattern
Observer patternObserver pattern
Observer pattern
 
Mediator pattern
Mediator patternMediator pattern
Mediator pattern
 
Clustering manual
Clustering manualClustering manual
Clustering manual
 
Parallel searching
Parallel searchingParallel searching
Parallel searching
 
Parallel computing chapter 2
Parallel computing chapter 2Parallel computing chapter 2
Parallel computing chapter 2
 
Parallel computing chapter 3
Parallel computing chapter 3Parallel computing chapter 3
Parallel computing chapter 3
 
Parallel computing(2)
Parallel computing(2)Parallel computing(2)
Parallel computing(2)
 
Report writing(short)
Report writing(short)Report writing(short)
Report writing(short)
 
Bengali optical character recognition system
Bengali optical character recognition systemBengali optical character recognition system
Bengali optical character recognition system
 
Environmental pollution control
Environmental pollution controlEnvironmental pollution control
Environmental pollution control
 
Chatbot Artificial Intelligence
Chatbot Artificial IntelligenceChatbot Artificial Intelligence
Chatbot Artificial Intelligence
 

Similar a Strategy pattern

Missilecommand
MissilecommandMissilecommand
Missilecommand
Susan Gold
 
Chapter 3.4
Chapter 3.4Chapter 3.4
Chapter 3.4
sotlsoc
 

Similar a Strategy pattern (11)

Embedded c
Embedded cEmbedded c
Embedded c
 
Missilecommand
MissilecommandMissilecommand
Missilecommand
 
Chapter 3.4
Chapter 3.4Chapter 3.4
Chapter 3.4
 
Database object, sub query, Join Commands & Lab Assignment
Database object, sub query, Join Commands & Lab AssignmentDatabase object, sub query, Join Commands & Lab Assignment
Database object, sub query, Join Commands & Lab Assignment
 
Chapter-5.ppt
Chapter-5.pptChapter-5.ppt
Chapter-5.ppt
 
Typescript Basics
Typescript BasicsTypescript Basics
Typescript Basics
 
Getting Started - Console Program and Problem Solving
Getting Started - Console Program and Problem SolvingGetting Started - Console Program and Problem Solving
Getting Started - Console Program and Problem Solving
 
SPF Getting Started - Console Program
SPF Getting Started - Console ProgramSPF Getting Started - Console Program
SPF Getting Started - Console Program
 
JAVA LESSON-02.pptx
JAVA LESSON-02.pptxJAVA LESSON-02.pptx
JAVA LESSON-02.pptx
 
Visula C# Programming Lecture 6
Visula C# Programming Lecture 6Visula C# Programming Lecture 6
Visula C# Programming Lecture 6
 
Java Basics 1.pptx
Java Basics 1.pptxJava Basics 1.pptx
Java Basics 1.pptx
 

Más de Md. Mahedi Mahfuj (12)

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
 
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(long)
Report writing(long)Report writing(long)
Report writing(long)
 
Job search_resume
Job search_resumeJob search_resume
Job search_resume
 
Job search_interview
Job search_interviewJob search_interview
Job search_interview
 
R language
R languageR language
R language
 
Big data
Big dataBig data
Big data
 
Cloud testing v1
Cloud testing v1Cloud testing v1
Cloud testing v1
 
Distributed deadlock
Distributed deadlockDistributed deadlock
Distributed deadlock
 

Último

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 

Último (20)

Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 

Strategy pattern

  • 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 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. Page 1 of 12
  • 2. Class report 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, 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 : Page 2 of 12
  • 3. Class report Commander: 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 report Changemode (); ….. ….. …… 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 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 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. Page 5 of 12
  • 6. Class report 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 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. Page 6 of 12
  • 7. Class report 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”) ;}} 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 report 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. 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()) …….. 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 report Aggressive 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