Navigation:  Tutorials > Beginners Guide to Smalltalk > Classes and Methods >

Classes as Object Factories

Previous pageReturn to chapter overviewNext page

So far, you've seen how a Smalltalk class is an object that specifies how instances of that class behave. It does this by holding on to all the methods that implement the messages the instance can respond to, and by keeping information about the layout of the instance's data (the instance variables etc.). However, a class object also has an additional useful function; it can act as a "factory" for manufacturing its instances. We've seen this before in Objects and Messages:

rect := Rectangle origin: 0@0 extent: 10@10.


Here we are sending the message #origin:#extent: to the Rectangle class with the expectation that it will answer a new instance of a rectangle. In this way the class is acting like a factory object capable of a manufacturing instances to our specifications and returning them for us to use in any way we see fit. The most common instance creation method is #new which can usually be sent to a class to create a new instance when no additional parameters are required to define it. For example, when we create new shapes to add to a Playground we use something like:

playground add: Triangle new.