Main Content
Object factories in C++, Python, PHP and Perl Archive - Originally posted on "The Horse's Mouth" - 2014-12-19 21:27:13 - Graham Ellis
Raw materials are converted into a manufactured product in a factory or by a factory procedure, and the sort of product you can make depends on both the raw material that's supplied and on the capability of the factory.
And so it is with Object Oriented Computer Programming - supply raw material to something that's known as a factory method - that's not a reserved word, by the way, just a convention, and the factory method will return you a object it has manufactured. Since no object exist at the start of the process, your factory method needs to be a class or static method (the naming convention depends on which language you're using).
During today's course, I wrote an example in C++ of a group of two (very small) classes, both inheriting from the same base class, and I provided a factory within that base class to manufacture new objects of the two different types, depening on the incoming data - strings from a file, where the string contents lead to the decision as to which object was manufactured. Complete example [here] .
Other examples of factory methods manufacturing different object types are [here] in Python , [here] in PHP and [here] in Perl .
Some other articles
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 This article 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 What is a factory? 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 Y112 - Objects - Intermediate Nesting decorators Defining an object that is a modified standard type in Python with in Python - examples of use, and of defining your own context Object and Static methods - what is the difference; example in Python 3 Setting up and tearing down with the Python with keyword Deciding whether to use parameters, conditional statements or subclasses Spike solution, refactoring into encapsulated object methods - good design practise A good example of recursion - a real use in Python Changing what operators do on objects - a comparison across different programming languages This article Python base and inherited classes, test harness and unit testing - new examples Python Properties - how and why Really Simple Class and Inheritance example in Python Inheritance, Composition and Associated objects - when to use which - Python example Backquote, backtic, str and repr in Python - conversion object to string Metaclasses (Python) and Metatables (Lua) Static variables in functions - and better ways using objects A demonstration of how many Python facilities work together A list of special method and attribute names in Python Python - some common questions answered in code examples Defining static methods in Python Should Python classes each be in their own file? The Light bulb moment when people see how Object Orientation works in real use Python decorators - your own, staticmethod and classmethod Mixins example in Python Multiple inheritance in Python - complete working example The Multiple Inheritance Conundrum, interfaces and mixins Methods that run on classes (static methods) in Python How do I set up a constant in Python? TypeError: super() argument 1 must be type, not classobj (Python) Python - fresh examples of all the fundamentals Calling base class constructors Equality, sameness and identity - Python Using a utility method to construct objects of different types - Python Python - formatting objects What are factory and singleton classes? __new__ v __init__ - python constructor alternatives? Practical polymorphism in action Pieces of Python Comparison of Object Oriented Philosophy - Python, Java, C++, Perl Think about your design even if you don't use full UML Class, static and unbound variables Overloading of operators on standard objects in Python Using a Python dictionary as a holder of object attributes P218 - More Objects Changing what operators do on objects - a comparison across different programming languages This article Using object orientation for non-physical objects Perl design patterns example Building an object based on another object in Perl Perl - calls to methods that use => - what do they mean? What do I mean when I add things in Perl? Learning Object Orientation in Perl through bananas and perhaps Moose Making Perl class definitions more conventional and shorter Some more advanced Perl examples from a recent course Different perl examples - some corners I rarely explore Igloos melt in the summer, but houses do not The Multiple Inheritance Conundrum, interfaces and mixins Calculation within objects - early, last minute, or cached? Operator overloading - redefining addition and other Perl tricks Nuclear Physics comes to our web site Calling base class constructors Factory method example - Perl Example of OO in Perl Object Oriented Programming in Perl - Course Perl for Larger Projects - Object Oriented Perl What are factory and singleton classes? -> , >= and => in Perl Comparison of Object Oriented Philosophy - Python, Java, C++, Perl Think about your design even if you don't use full UML NOT Gone phishing Changing @INC - where Perl loads its modules Packages in packages in Perl When to bless a Perl variable Bellringing and Programming and Objects and Perl 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 This article 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 What is a factory? Looking for a practical standards course Object Relation Mapping (ORM) North, Norther and Northest - PHP 5 Objects C233 - OO in C++ - beyond the basics Variables, Pointers and References - C and C++ When do I use the this keyword in C++? Designing a base class and subclasses, and their extension, in C++ Final examples for 2014 - and a look at our 2015 training course options This article Extended and Associated objects - what is the difference - C++ example Associated Classes - using objects of one class within another Destructor methods in C++ - a primer C++ - objects that are based on other objects, saving coding and adding robustness C and C++ - preprocess, compile, load, run - what each step is for Private and Public - and things between C++ - putting the language elements together into a program C++ objects - some short, single file demonstrations C++ - a complete example with polymorphism, and how to split it into project files Objects and Inheritance in C++ - an easy start Complete teaching example - C++, inheritance, polymorphism Calling base class constructors What a lot of files! (C++ / Polymorphism demo) C - structs and unions, C++ classes and polymorphism What are factory and singleton classes? C++ - just beyond the basics. More you can do Comparison of Object Oriented Philosophy - Python, Java, C++, Perl Simple polymorphism example - C++ References and Pointers in C++