This is a discussion on Diff b/w factory and startegy within the Software Patterns forums, part of the Testing category; Hello, Can anyone tell whats the difference between factory design pattern and strategy pattern..I saw couple of examples,,both ...
|
|||||||
| Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Diff b/w factory and startegy
Hello,
Can anyone tell whats the difference between factory design pattern and strategy pattern..I saw couple of examples,,both patterns looks same to me.. Eg: Factory pattern: class shape { public: virtual void draw() = 0; }; class sqaure : public shape { void draw() { } }; class circle : public shape { void draw() { } }; class context { Shape* sh; public: // Factory method Shape* Factory(int i) { case 0: sh = new Square(); break; case 1: sh = new Circle(); break; } }; void main() { context cont; Shape* sh = cont.Factory(0); sh.draw(); Shape* sh = cont.Factory(1); sh.draw(); } // Strategy pattern class NameStrategy { public: virtual void greet() = 0; }; class SayHi : public NameStrategy { public: void greet() { cout << "Hi! How's it going?" << endl; } }; class Ignore : public NameStrategy { public: void greet() { cout << "(Pretend I don't see you)" << endl; } }; class Context { NameStrategy& strategy; public: Context(NameStrategy& strat) : strategy(strat) {} void greet() { strategy.greet(); } }; int main() { SayHi sayhi; Ignore ignore; Context c1(sayhi), c2(ignore), c3(admission); c1.greet(); c2.greet(); } Cheers.... |
|
|||
|
Re: Diff b/w factory and startegy
I'm kind of new to patterns, but here is my take on the difference.
<b>Factory</b>: With the factory pattern, you do not know exactly which type of shape class you need. All you know is that you have some sort of discriminating information to give to the factory and it will determine which class should be returned. A better real world example is below <this is just pseudo-Java code>: public interface IFileSystem { public boolean isEncrypted(); public void write(); public byte[] read(); } public abstract class AbsFilesystem implements IFileSystem { // Must be overidden by children. public abstract boolean isEncrypted(); public void write() { // Implementation goes here } public byte[] read() { // Implementation goes here } } public class StandardFileSystem extends AbsFileSystem { public boolean isEncrypted() { return false; } } public class EncryptedFileSystem extends AbsFileSystem { public boolean isEncrypted() { return true; } public void write() { // Override with encrypted impl } public byte[] read() { // Override with encrypted impl } } public class Data { private boolean encrypted; public boolean isEncrypted() { return this.encrypted; } } public class FileSystemFactory { public static IFileSystem getFileSystem(Data data) { if (data.isEncrypted()) { return new EncryptedFileSystem(); } else { return new StandardFileSystem(); } } } Sorry for the long example, but I hate it when books don't a detailed listing. <b>Strategy</b>: With the strategy pattern, the "infrastucture" is setup to allow for the client to plug in any classes that have the same interface but provide different functionality. The big difference is that you decide what you want to use. Building off of the example code above: public class DataChannel { public static void sendAllFileSystemDataToTape(IFileSystem fs) { // Use fs to send all data to tape fs.write(); } } public class UIConsole { private IFileSystem std = new StandardFileSystem(); private IFileSystem enc = new EncryptedFileSystem(); public void handleTapeWriteRequest(boolean encryptedWrite) { if (encryptedWrite) { DataChannel.sendAllFileSystemDataToTape(this.enc); } else { DataChannel.sendAllFileSystemDataToTape(this.std); } } } Here we let the "operator" dictate how to perform the write rather than a hard coded factory method. Again, sorry for being long winded ... but I'm new to all of this too and can't stand it when someone just breezes over these subtle differences. Hope this helps .... Gerry |