Use the design mode to solidify your C # Program (2)

Source: Internet
Author: User

Use the design mode to solidify your C # Program (2)

Design Patterns: Solidify Your C # Application Architecture with Design Patterns Chinese edition (Part 1)

Author: Samir Bajaj
Translator: glory
After implementing all three algorithms in this way, I can design a client program in a way that is not coupled with the Implementation Details of any particular algorithm. The customer holds an interface reference and does not need to know any details about the specific implementation of this interface. See table 4 code.

Table 4

Class Primality
{
Private Strategy strategy;
Public Primality (Strategy s)
{
Strategy = s;
}
Public bool Test (int n)
{
Return strategy. IsPrime (n );
}
}

Finally, I created an instance of the Primality class and initialized it based on the user input with the corresponding algorithm. The Test method of the Primality class calls the IsPrime method of the corresponding algorithm object (the object implementing The Strategy interface.
Constructing an algorithm family in this way has many advantages, but the biggest advantage is that the customer program has no coupling relationship with the implementation details of a specific algorithm. This improves scalability-you can develop other algorithms and insert them seamlessly as long as they comply with basic interface specifications. In this way, the algorithm can be dynamically transformed. In addition, the strategy Mode avoids the possibility of confusing the client program code due to the use of conditional statements. Do you understand the meaning of this sentence? J]

The following is a complete example of the strategy Mode.

C # example:
Using System;
Interface Strategy
{
Bool IsPrime (int n );
}
Class Miller: Strategy
{
Public bool IsPrime (int n)
{
Bool result = false;
File: //Use the Miller method to test whether n is a prime number. If yes, update the result value.
Console. WriteLine ("Using Millers algorithm ");
Return result;
}
}
Class Fermat: Strategy
{
Public bool IsPrime (int n)
{
Bool result = false;
File: //Use the Fermat method to test whether n is a prime number. If yes, update the result value.
Console. WriteLine ("Using Fermats algorithm ");
Return result;
}
}
Class Mersenne: Strategy
{
Public bool IsPrime (int n)
{
Bool result = false;
File: //Use Mersenne to test whether n is a prime number. If yes, update the result value.
Console. WriteLine ("Using Mersennes algorithm ");
Return result;
}
}
Class Primality
{
Private Strategy strategy;

Public Primality (Strategy s)
{
Strategy = s;
}
Public bool Test (int n)
{
Return strategy. IsPrime (n );
}
}
Class Application
{
Public static void Main ()
{
Console. Write ("Number to be tested :");
String input = Console. ReadLine ();
Int n = Int32.Parse (input );
Console. Write ("Desired algorithm performance: lo, medium, hi? ");
Input = Console. ReadLine ();
Char ch = char. Parse (input );
Primality prime = null;
Switch (ch)
{
Case l:
Case L:
Prime = new Primality (new Miller ());
Break;
Case m:
Case M:
Prime = new Primality (new Fermat ());
Break;
Case h:
Case H:
Prime = new Primality (new Mersenne ());
Break;
}
If (prime! = Null)
{
Bool result = prime. Test (n );
}
Else
Console. WriteLine ("Bad Choice! ");
}
}
/* The following is the output result of a test:
Number to be tested: 1
Desired algorithm performance: lo, medium, hi? M
Using Fermats algorithm
*/
C ++ example:
# Include "stdafx. h ";
# Include
Class Strategy
{
Public:
Virtual bool IsPrime (int n) = 0;
};
Class Miller: public Strategy
{
Public:
Bool IsPrime (int n)
{
Bool result = false;
File: //Use the Miller method to test whether n is a prime number. If yes, update the result value.
Cout <"Using Millers algorithm ";
Return result;
}
};
Class Fermat: public Strategy
{
Public:
Bool IsPrime (int n)
{
Bool result = false;
File: //Use the Fermat method to test whether n is a prime number. If yes, update the result value.
Cout <"Using Fermats algorithm ";
Return result;
}
};
Class Mersenne: public Strategy
{
Public:
Bool IsPrime (int n)
{
Bool result = false;
File: //Use Mersenne to test whether n is a prime number. If yes, update the result value.
Cout <"Using Mersennes algorithm ";
Return result;
}
};
Class Primality
{
Private:
Strategy * strategy;
Public:
Primality (Strategy * s)
{
Strategy = s;
}
~ Primality ()
{
If (strategy! = NULL)
{
Delete strategy;
Strategy = NULL;
}
}
Bool Test (int n)
{
Return strategy-> IsPrime (n );
}
};
Int _ tmain (int argc, _ TCHAR * argv [])
{
Cout <"Number to be tested :";
Int n;
Cin> n;
Cout <"Desired algorithm performance: lo, medium, hi? ";
Char ch;
Cin> ch;
Primality * prime = NULL;
Switch (ch)
{
Case l:
Case L:
Prime = new Primality (new Miller ());
Break;
Case m:
Case M:
Prime = new Primality (new Fermat ());
Break;
Case h:
Case H:
Prime = new Primality (new Mersenne ());
Break;
}
If (prime! = NULL)
{
Bool result = prime-> Test (n );
Delete prime;
Prime = NULL;
File: // ThisEr also demonstrated a bad design. You cannot release Miller, Fermat, or Mersenne objects. Do you know why?
}
Else
Cout <"Bad Choice! ";
Return 0;
}
/* The following is the output result of a test:
Number to be tested: 1
Desired algorithm performance: lo, medium, hi? M
Using Fermats algorithm
*/
]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.