ID:1642872
 
How To Use A Factory

Hi so here I would like to suggest one simple easy way to get around building massive convoluted object trees.

I've given an example that creates an object which you can use to build yourself car objects instead of defining new car objects in your tree.

In the end you will have an extremely clean object tree that is more readable.

A Worked Example

// How you can use it

mob
Login()
var/obj/Car/someCar = CarFactory.get("Corolla")
world << someCar.model
world << someCar.cost

someCar = CarFactory.get("Bently")
world << someCar.model
world << someCar.cost


// Defining what it means to be a car
// As you can see there's only one type of car

obj/Car

var
model
cost
speed


// Defining the class that builds us new types of cars

var/CarFactory/CarFactory = new/CarFactory

CarFactory

parent_type = /Factory

objectType = /obj/Car

get(var/request as text)

var/obj/Car/object = new objectType

switch(request)

if("Bently")
object.model = "Bentley Continental GT"
object.cost = 150000
object.speed = 200

if("Corolla")
object.model = "Toyota Carolla"
object.cost = 2000
object.speed = 90

// Add new car models here

else
object = null

return object


// Defining what it means to be a factory

Factory

var/objectType
proc/get(var/request as text)


Output of this Example

Toyota Carolla
2000
Bentley Continental GT
150000
This is pretty much the design structure I employ for this tutorial: http://www.byond.com/forum/?post=1626900

Factories are damned useful, and a great design structure for avoiding the issue of a cluttered object tree.

Thanks for this, Zec. Nice tutorial.
Also very useful for loading the object data from a dynamic source such as SQL.
In response to Ter13
Yeah, I liked yours and I just started taking a course that teaches this stuff so I thought I'd do my own.
This is nice.