v Definition The Strategy pattern involves separating an algorithm from its host class and putting it in a separate class. When there are multiple strategies available for an algorithm, for a given problem it is always better to separate them in to different objects. If the algorithms are all kept in the one class, the class will be become a big messy conditional statements. The Strategy pattern enables a client to choose which algorithm to use from a family of algorithms and gives it a simple way to access it. Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from the clients that use it. Note: 1. Strategy is like Template Method except that strategy consist details whereas template provides skeleton for the algorithm. 2. State is like Strategy except in its objective. 3. Strategy lets us change the internal object. Decorator lets us change the covering, underlying object remains same. v Design Let us say that us are implementing text hashing for the system. During requirements decision is to implement the MD5 hashing, during development added with SHA1 for another requirement, and it kept continued. Finally decision is to implement with couple of options and based on the client strategy it can be changed. v UML Diagram Please refer to the attached doc for the images v Code public abstract class HashingStrategy { public abstract void HashingInterface(string text); } public class MD5Hashing : HashingStrategy { public override void HashingInterface(string text) { MD5 md5 = new MD5CryptoServiceProvider(); Byte[] bytes; bytes = ASCIIEncoding.Default.GetBytes(text); Byte[] encodedBytes; encodedBytes = md5.ComputeHash(bytes); string A = BitConverter.ToString(encodedBytes); } } public class HashingContext { private HashingStrategy _hashingStrategy; public HashingContext(HashingStrategy hashingStrategy) { _hashingStrategy = hashingStrategy; } public void HashPassword(string text) { _hashingStrategy.HashingInterface(text); } } public class SHA1Hashing : HashingStrategy { public override void HashingInterface(string text) { SHA1 sha1 = new SHA1CryptoServiceProvider(); Byte[] bytes; bytes = ASCIIEncoding.Default.GetBytes(text); Byte[] encodedBytes; encodedBytes = sha1.ComputeHash(bytes); string A = BitConverter.ToString(encodedBytes); } } public class SHA256Hashing : HashingStrategy { public override void HashingInterface(string text) { SHA256 sha256 = new SHA256CryptoServiceProvider(); Byte[] bytes; bytes = ASCIIEncoding.Default.GetBytes(text); Byte[] encodedBytes; encodedBytes = sha256.ComputeHash(bytes); string A = BitConverter.ToString(encodedBytes); } } public class SHA384Hashing : HashingStrategy { public override void HashingInterface(string text) { SHA384 sha384 = new SHA384CryptoServiceProvider(); Byte[] bytes; bytes = ASCIIEncoding.Default.GetBytes(text); Byte[] encodedBytes; encodedBytes = sha384.ComputeHash(bytes); string A = BitConverter.ToString(encodedBytes); } } Client HashingContext context; context = new HashingContext(new SHA1Hashing()); context.HashPassword("Chinna Sushma"); context = new HashingContext(new SHA384Hashing()); h.HashPassword("Chinna Lohetha"); Hope this helps Chinna Download SampleDownload source files -53 kb |