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

**URL:** <https://boards.straightdope.com/t/programming-where-on-the-net-can-i-find-info-about-manager-and-factory-classes/199913>\
**Category:** Factual Questions\
**Created:** [September 6, 2003, 11:46am UTC](https://boards.straightdope.com/t/programming-where-on-the-net-can-i-find-info-about-manager-and-factory-classes/199913 "2003-09-06T11:46:23Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Crowbar\_of\_Irony\_3](https://avatars.discourse-cdn.com/v4/letter/c/f08c70/32.png) [@Crowbar\_of\_Irony\_3](https://boards.straightdope.com/u/Crowbar_of_Irony_3)\
**Post date:** [September 6, 2003, 11:46am UTC](https://boards.straightdope.com/t/programming-where-on-the-net-can-i-find-info-about-manager-and-factory-classes/199913/1 "2003-09-06T11:46:23Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![EllisDee](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/ellisdee/32/4531_2.png) [@EllisDee](https://boards.straightdope.com/u/EllisDee)\
**Post date:** [September 6, 2003, 12:25pm UTC](https://boards.straightdope.com/t/programming-where-on-the-net-can-i-find-info-about-manager-and-factory-classes/199913/2 "2003-09-06T12:25:25Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![gentle](https://avatars.discourse-cdn.com/v4/letter/g/eb8c5e/32.png) [@gentle](https://boards.straightdope.com/u/gentle)\
**Post date:** [September 6, 2003, 1:54pm UTC](https://boards.straightdope.com/t/programming-where-on-the-net-can-i-find-info-about-manager-and-factory-classes/199913/3 "2003-09-06T13:54:49Z")

</div>

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](http://isbn.nu/0201633612) 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:

> [@](#):
>
> A design pattern systematically names, motivates, and explains a general design that addresses a recurring design problem in object-oriented systems. It describes the problem, the solution, when to apply the solution, and its consequences. It also gives implementation hints and examples. The solution is a general arrangement of objects and classes that solve the problem. The solution is customized and implemented to solve the problem in a particular context.

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](http://c2.com/cgi/wiki?PortlandPatternRepository) 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](http://www.math.utsa.edu/sphere/salingar/Chris.text.html#COMPUTER), 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++:

```auto

#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:

```auto

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.

---

<div class="post-metadata">

**Author:** ![gentle](https://avatars.discourse-cdn.com/v4/letter/g/eb8c5e/32.png) [@gentle](https://boards.straightdope.com/u/gentle)\
**Post date:** [September 6, 2003, 2:20pm UTC](https://boards.straightdope.com/t/programming-where-on-the-net-can-i-find-info-about-manager-and-factory-classes/199913/4 "2003-09-06T14:20:50Z")

</div>

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

```auto

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

```

---

<div class="post-metadata">

**Author:** ![EllisDee](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/ellisdee/32/4531_2.png) [@EllisDee](https://boards.straightdope.com/u/EllisDee)\
**Post date:** [September 6, 2003, 2:29pm UTC](https://boards.straightdope.com/t/programming-where-on-the-net-can-i-find-info-about-manager-and-factory-classes/199913/5 "2003-09-06T14:29:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![buckgully](https://avatars.discourse-cdn.com/v4/letter/b/eb9ed0/32.png) [@buckgully](https://boards.straightdope.com/u/buckgully)\
**Post date:** [September 6, 2003, 2:37pm UTC](https://boards.straightdope.com/t/programming-where-on-the-net-can-i-find-info-about-manager-and-factory-classes/199913/6 "2003-09-06T14:37:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Crowbar\_of\_Irony\_3](https://avatars.discourse-cdn.com/v4/letter/c/f08c70/32.png) [@Crowbar\_of\_Irony\_3](https://boards.straightdope.com/u/Crowbar_of_Irony_3)\
**Post date:** [September 6, 2003, 3:43pm UTC](https://boards.straightdope.com/t/programming-where-on-the-net-can-i-find-info-about-manager-and-factory-classes/199913/7 "2003-09-06T15:43:07Z")

</div>

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!)

---

<div class="post-metadata">

**Author:** ![gentle](https://avatars.discourse-cdn.com/v4/letter/g/eb8c5e/32.png) [@gentle](https://boards.straightdope.com/u/gentle)\
**Post date:** [September 6, 2003, 4:08pm UTC](https://boards.straightdope.com/t/programming-where-on-the-net-can-i-find-info-about-manager-and-factory-classes/199913/8 "2003-09-06T16:08:49Z")

</div>

**LostCause** , better yet, buy the book that I and **buckgully** mentioned. 🙂 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](http://abebooks.com/).

Also, for your specific query about factories, [this is a better search query](http://www.google.com/search?q=abstract+factory+design+pattern).

Google has a [directory entry about patterns](http://directory.google.com/Top/Computers/Programming/Methodologies/Patterns_and_Anti-Patterns/?il=1).

---

<div class="post-metadata">

**Author:** ![Capt.Ridley\_s\_Shooting\_Party](https://avatars.discourse-cdn.com/v4/letter/c/cc9497/32.png) [@Capt.Ridley\_s\_Shooting\_Party](https://boards.straightdope.com/u/Capt.Ridley_s_Shooting_Party)\
**Post date:** [September 6, 2003, 6:39pm UTC](https://boards.straightdope.com/t/programming-where-on-the-net-can-i-find-info-about-manager-and-factory-classes/199913/9 "2003-09-06T18:39:10Z")

</div>

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.
