Exforsys

Online Training

Diff b/w factory and startegy

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


Go Back   Exforsys > Testing > Software Patterns

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 06-11-2005, 07:50 AM
Radde
Guest
 
Posts: n/a
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....

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 06-19-2005, 11:36 AM
Gerry
Guest
 
Posts: n/a
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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 06-29-2005, 11:01 AM
pven
Guest
 
Posts: n/a
Re: Diff b/w factory and startegy

Hi,

Yes, Strategy is a behavioural pattern.

It has closer resemblance to Builder design pattern.

Pven

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 10:50 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0
Copyright 2004 - 2007 Exforsys Inc. All rights reserved.