Main Content
What is a factory? Archive - Originally posted on "The Horse's Mouth" - 2010-04-26 17:02:41 - Graham Ellis
A Factory is somewhere that takes raw material, and converts into objects of some value. And so it is in Object Oriented Programming, where a "factory method" takes a chunk of raw material and makes an object out of it. It may differ from a constructor in that we may not know what type of object we're actually going to make when we call it!
Let's see an example (in PHP). Here's some source code which takes each of the lines from an incoming data file and passes them in turn into a factory method. The factory return an object each time it's called, which is stored into an array called options.
$southbound = file("southbound.txt");
$options = array();
foreach ($southbound as $lyne) {
array_push($options,pubtrans::myfactory($lyne));
}
From that application code, you can't tell what types of objects have been set up, nor can you tell how the code has decided which to set up - it's all encapsulated (hidden) within the factory.
Here's the code within the pubtrans class / namespace:
static function myfactory($lyne) {
list($dep_t,$capa,$run_t,$dest,$wotsit) = explode(" ",trim($lyne));
if ($wotsit == "train") {
return (new train($dep_t,$capa,$run_t,$dest));
} else {
return (new bus($dep_t,$capa,$run_t,$dest));
}
}
... and from that you can see that we're making either a train or a bus (they are probably classes that are subclasses of pubtrans, but not necessarily so).
The complete example is online - [source code] and [running example] . The data is [here] .
Some other articles
Q908 - Object Orientation: Design Patterns Separating detailed data code from the main application - Ruby example Easy data to object mapping (csv and Python) Singleton design pattern - examples and uses Thin application, thick objects - keep you main code simple. Example in Ruby How healthy are the stars of stage and screen? Java Utility class - flexible replacement for array. Also cacheing in objects and multiple catch clauses example. Designing a base class and subclasses, and their extension, in C++ How to avoid too many recalculations within an object Object factories in C++, Python, PHP and Perl Java - factory method, encapsulation, hashmap example Learning to program - what are algorithms and design patterns? Using object orientation for non-physical objects Perl design patterns example Spike solution, refactored and reusable, Python - Example Caching Design Patterns Reading files, and using factories to create vectors of objects from the data in C++ Learning C++ - a design pattern for your first class Design Patterns - what are they? Why use them? What is a factory method and why use one? - Example in Ruby This article Looking for a practical standards course Object Relation Mapping (ORM) North, Norther and Northest - PHP 5 Objects Q907 - Object Orientation: Design Techniques Associative objects - one object within another. Spike solution, refactoring into encapsulated object methods - good design practise The spirit of Java - delegating to classes Test driven development, and class design, from first principles (using C++) Using object orientation for non-physical objects Teaching OO - how to avoid lots of window switching early on Storing your intermediate data - what format should you you choose? Inheritance, Composition and Associated objects - when to use which - Python example From Structured to Object Oriented Programming. Rooms ready for guests - each time, every time, thanks to good system design When you should use Object Orientation even in a short program - Python example Spike solutions and refactoring - a Python example Why you should use objects even for short data manipulation programs in Ruby Designing your application - using UML techniques Your PHP website - how to factor and refactor to reduce growing pains Ruby - a training example that puts many language elements together to demonstrate the whole Object Oriented Programming for Structured Programmers - conversion training Comments in and on Perl - a case for extreme OO programming What is a factory method and why use one? - Example in Ruby Turning an exercise into the real thing with extreme programming Should Python classes each be in their own file? Program for reliability and efficiency - do not duplicate, but rather share and re-use Relationships between Java classes - inheritance, packaging and others The Light bulb moment when people see how Object Orientation works in real use Containment, Associative Objects, Inheritance, packages and modules This article The Multiple Inheritance Conundrum, interfaces and mixins Plan your application before you start Simples Object Oriented programming - a practical design example Planning! Designing a heirarcy of classes - getting inheritance right When should I use OO techniques? Teaching Object Oriented Java with Students and Ice Cream Object Oriented Tcl Object Oriented Programming in Perl - Course Object Relation Mapping (ORM) What are factory and singleton classes? Maintainable code - some positive advice Build on what you already have with OO Comparison of Object Oriented Philosophy - Python, Java, C++, Perl The Fag Packet Design Methodology Think about your design even if you don't use full UML Design - one name, one action Introduction to Object Oriented Programming Tapping in on resources OO - real benefits H108 - Objects in PHP Associative objects - one object within another. Caching results in an object for efficiency - avoiding re-calculation Singleton design pattern - examples and uses Changing what operators do on objects - a comparison across different programming languages Object factories in C++, Python, PHP and Perl Learning about Object Orientation in PHP - a new set of examples stdClass in PHP - using an object rather than an associative array Objects in PHP - Revision Caching Design Patterns Copying, duplicating, cloning an object in PHP Autoload in PHP How do classes relate to each other? Associated Classes Design Patterns - what are they? Why use them? Designing your application - using UML techniques Computer Graphics in PHP - World (incoming data) to Pixel (screen) conversion Catchable fatal error in PHP ... How to catch, and alternative solutions such as JSON Private and Public - and things between Getting the OO design write - with PHP a example Does copying a variable duplicate the contents? PHP - Object Oriented Design in use This article The Multiple Inheritance Conundrum, interfaces and mixins Static class members in PHP - a documented example Object Oriented Programming in PHP Shipping a test harness with your class in PHP Serialization - storing and reloading objects Abstract classes, Interfaces, PHP and Java Object Oriented programming - a practical design example PHP4 v PHP5 - Object Model Difference Cleaning up redundant objects When should I use OO techniques? PHP - getclass v instanceof Introduction to Object Oriented Programming Sorting objects in PHP Calling base class constructors Accounts in PHP - an OO demo OO PHP demonstration - comparing objects and more What are factory and singleton classes? Object Oriented Model - a summary of changes from PHP4 to PHP5 Cue the music, I'm happy. Build on what you already have with OO Planning a hotel refurb - an example of a Gant chart in PHP Think about your design even if you don't use full UML North, Norther and Northest - PHP 5 Objects Don't repeat code - use loops or functions Should I use structured or object oriented? PHP5 lets you say no PHP v Java Object Oriented Programming in PHP