[Programming] Where on the Net can I find info about manager and factory classes?

Hi,

Just a quick programming question. I have been coming across terms like “factory” and “manager” classes for data structures. What actually are those? I did numerous searches on the Internet but couldn’t find any help. Anyone has any good links?

Thanks in advance!

They sound like examples of programming. As in:

Our new software package handles all operations of our business, but still has a few bugs. I looked at the code, and it appears the factory class can’t handle country information, which is a problem because we have factories all over the globe. And the manager class can’t handle middle names; only initials.

A class is simply a reusable encapsulated programming resource.

Does that sound like what they may be talking about?

Factories and managers are design patterns.

Design patterns are a concept formally introduced in a famous and highly influential book called Design Patterns: Elements of Reusable Object-Oriented Software by Gamma/Helm/Johnson/Vlissides, popularly referred to as “the gang of four” or “GoF”.

This book set in motion one of the most productive trends in programming of late, the simple but powerful idea that well-designed object-oriented programs tend to use, over and over again, certain patterns of logic. A pattern is not code, so it generally cannot be packaged in a library and reused; a pattern is an idiom you apply to your code. GoF defines it thus:

It’s important to note that most design patterns themselves are not new, and in fact the reason they’re patterns is that they’ve been proven to work really well: It’s just that now we have a good language to describe them. New patterns crop up all the time, because there are so many contexts and problems to solve, and someone always finds a generalized way to express a solution. You will find some sites on the net that obsessively collect patterns much like other people collect guitar chords. Someone also thought up the idea of anti-patterns: patterns that tell you how not to do things, ie. common design pitfalls.

Incidentally, a significant inspiration to the pattern movement was the works of architect Christopher Alexander, who invented a pattern language for building architecture: one of his works includes a large section simply listing different problems their solutions as architectural configurations: essentially where to place the bathroom in a given context. Combining these patterns, you could literally piece together a house, Lego-style.

Your “factory class” refers to the Abstract Factory pattern, which solves the problem of how to abstract a concrete class implementation from the user. Say you have an abstract class called Vehicle, and a VehicleFactory that produces such objects. The factory can produce Car, Train and Tank objects, all of which are concrete implementations of Vehicle – the user does not care. The user merely invokes the factory’s create() (or whatever) method, which returns a Vehicle instance. This leaves you with a single place in which to “plug in” the implementation class, so that when the Jetsons-style future finally comes around, we can change the factory to produce FlyingCars.

A “manager” is not really a design pattern, just a common term for anything that can be said to “manage” objects or data. For example, in a database setting you could have a connection manager that kept a list of open connections and a connection factory which the manager would use to instantiate connections.

A couple of other patterns:

The Visitor pattern is what systems programmers know as a callback – a function or class that you give to entity which invokes it (“calls back”): for example, you want to find all the files in the directory /foo, so you call walk("/foo", visitor), which calls visitor() on every file inside /foo.

The Composite pattern defines is something which acts like one object but really represents a group of objects, where all method calls act on the group as a whole. Here’s a silly example in C++:


#include <vector>
#include <cstdlib>
#include <iostream>

using namespace std;

class ICow
{
  public:
    virtual void moo() = 0;
};

typedef vector<ICow*> CowList;

class Cow : public ICow
{
  public:
    virtual void moo()
    {
      cout << "moo!" << endl;
    }
};

class Herd : public ICow
{
  private:
    CowList m_cows;
  public:
    Herd(CowList cows) :
      m_cows(cows)
    {
    }
    virtual void moo()
    {
      for (CowList::iterator i = m_cows.begin(); i != m_cows.end(); i++)
      {
        ICow* cow = *i;
        cow->moo();
      }
    }
};

int main()
{
  cout << "Here's one cow:" << endl;
  Cow().moo();

  cout << "Here's a whole bunch of cows:" << endl;
  CowList v;
  for (int i = 0; i < 10; i++)
  {
    v.insert(v.end(), new Cow());
  }
  Herd h(v);
  h.moo();
}

C++ is so ugly. Just for fun, here’s the Python equivalent:


class Cow:
  def moo(self):
    print "moo!"

class Herd:
  def __init__(self, cows):
    self.cows = cows
  def moo(self):
    for cow in self.cows:
      cow.moo()

print "Here's one cow:"
Cow().moo()
print "Here's a whole bunch of cows:"
cows = []
for x in range(10):
  cows.append(Cow())
herd = Herd(cows)
herd.moo()

Sorry about the verbosity. Hope this answers your question, though.

Oh, and since you specifically asked about factories, here’s another example.


import time

class CarFactory:
  def createCar(self):
    year = time.localtime()[0]
    if year < 1908:
      return CarriageAndHorses()
    elif year > 2030:
      return HoverCar()
    else:
      return Car()

class CarriageAndHorses:
  def canFly(self):
    print 0

class Car:
  def canFly(self):
    print 0

class HoverCar:
  def canFly(self):
    print 1
    
factory = CarFactory()
car = factory.createCar()
if car.canFly():
  print "It's Blade Runner time!"
else:
  print "Damn, it's not the future yet."

I don’t know about the OP, but I found your answers fascinating. Thanks for the info.

Also, I agree that C++ is exceedingly ugly.

Herd Cows - lmao

Dammit, on preview I see Gentle beat me to it. I’m posting this anyway.

Those are basic object oriented programming design patterns.

Design patterns are the basic, recurring structures that come up in OO programming. When you do it for any length of time, you discover that almost all objects wind up falling into a few different categories. People have taken those categories and standardized them.

The book “Design Patterns” by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides is the best source of information on those, since that’s the gold standard text on the subject. If you’re doing OO programming with anyone else, it’s a good idea to have read that book, since then you’ll have a common language from which to discuss the basic design elements.

For example, a “factory” class is something that builds other classes in situations where you wouldn’t want to directly create a class. The example usally used is writing a program that uses cross-platform widgets. Different platforms create a button differently, but you want your program would treat them the same internally. Otherwise, you’re going to have tons of unsupportable code that do things like "if(windows) {do one thing} else if(MacOS9) {do something different} scattered everywhere. With a factory class, you can put the platform dependent code all in one place, and have your factory class hand the object created back to your program. So instead of trying to create a platform specific button, you can just call “MyButtonFactory.makeNewButton()” and treat the button the same everywhere in your program.

If you don’t want to get the book (if you’re just dabbling, you probably don’t need to shell out the cash) then do a google search on “design patterns”. There’s lots of stuff out there on it.

If you’re feeling really ambitious, look into UML.

I hope this makes sense. I didn’t get very much sleep last night.

Thanks a lot, all!! I think I have a lead as to where to get more info…(Instead of searching for “programming managers” I shall look for “programming design patterns”).

Once again, thanks for the great explanation (and source code!)

LostCause, better yet, buy the book that I and buckgully mentioned. :slight_smile: If you do any sort of object-oriented programming, it will be a rewarding read, though it’s the kind of book you skim through once to grok the principles, then keep as a reference or source of inspiration when facing design problems. The price of the book may be a little hefty, but I’m sure you can find a cheaper used copy at abebooks.com.

Also, for your specific query about factories, this is a better search query.

Google has a directory entry about patterns.

There’s also specific books on design patterns for different programming languages. I own one in .pdf format for Java, and I know there’s also one for C++ too. The book I have isn’t written by the GOF, however, and possibly doesn’t go into as much detail (it details when to use them, their advantages and implements an example).

Design patterns become invaluable when making large projects (and Java’s standard library is a good showcase for a few of them). You possibly know of a few already if you’ve been doing programming, the Singleton is often used in all sorts of applications to allow only one object of a particular class to be instantiated.