Jump to content

Completely Off-Topic


ThePsion5

Recommended Posts

Hi guys,I was wondering if anyone here knew some good active forums on object-oriented programming and or software design patterns; I have a question that's been puzzling me for some time now. I'll post it here just in case anyone's interested:The Factory design pattern is meant to reduce tightly-coupled objects by separating the code that instantiates particular classes from the code that implements them. So instead of having code like this:

$Crawler = new Crawler('http://www.google.com');$Crawler->setStrategy($Something);$Crawler->setErrorHandler($SomethingElse);

You have something like this:

$Crawler = CrawlerFactory::makeCrawler('http://www.google.com', $Something, $SomethingElse);

Therefore you can change what the makeCrawler() method actually creates without having to change the code in the instantiating classes. Here's where my question comes in:By using the factory method, aren't you creating tight coupling between the classes that instantiate the Crawler class and the CrawlerFactory (since the classes using the Crawler class must know about the CrawlerFactory directly)?

Link to comment
Share on other sites

Right...the concept of tight coupling between objects is that two classes are explicitly aware of each other and know about each other's internal implementations...for example, tight coupling would be something like this:

$variable = $Class1->getsomething();$Class2=>internalVar = $variable;

This doesn't sound so bad except when you need to change the $Class2 object to a different class that may not have the same variable name, loose coupling is more like this:

interface IClass1{  function getSomething();}interface IClass2{  function setThatThing($thing);}

so the code is now this:

$var = $Class1->getSomething();$Class2->setThatThing($var);

And you can change out $Class2 for a different type of object as long as it conforms to the interface. Hopefully I didn't just confuse everyone alot. :)

Link to comment
Share on other sites

I understand what you are getting at. In the case of a class that is only used internally in an application there is, IMO, very little difference between the 2 methods (por/con wise).If the class is intended for use in mulitple applications having the factory seperate would be a good idea incase you need to create the object differently you could just have a different factory for the different applications without changing the class.Wow, I hope I wasn't confusing either. I haven't written many classes to be used by multiple applications or to be distributed as a component so I haven't researched the topic much or had to even worry about it.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...