I cant seem to grasp the proper concepts of a factory.
Can anyone help me code a simple test? I read some texts over the internet and cant code it the same way. Actually i cant understand the process. Copying code is easy, but i need to learn why this wont work.
class Factory:
def __init__(self):
self.msg = "teste"
def fabricateAnotherObject(self,obj,**kwargs):
return apply(obj,**kwargs)
class testClass:
def __init__(self,nome,salario,endereco):
self.nome = nome
self.salario = salario
self.endereco = endereco
def __str__(self):
return "Nome: " + str(self.nome) + "\nEndereco: " + str(self.endereco) + "\nSalario: " + str(self.salario)
a = Factory()
emp = a.fabricateAnotherObject(testClass,"George",2000,"Three Four Five Avenue")
print str(emp)
Factory Method is a creational design pattern used to create concrete implementations of a common interface. It separates the process of creating an object from the code that depends on the interface of the object. For example, an application requires an object with a specific interface to perform its tasks.
In object-oriented programming, a factory is an object for creating other objects; formally, it is a function or method that returns objects of a varying prototype or class from some method call, which is assumed to be "new".
Yes, Python has design patterns. In fact, design patterns are simply ways to solve problems; they are independent of any programming language. You can implement most design patterns in a wide range of programming languages.
A static factory method is a public static method on the object that returns a new instance of the object. These type of methods share the same benefits as the traditional factory method design pattern. This is especially useful for value objects that don't have a separate interface and implementation class.
Your code is counter-productive (sorry, I must say it).
The sense of a factory is, that you don't have to know the class of your constructed object at the position where you create it.
The reason is, that object creation creates a gap in object oriented abstraction. You must be concrete in creating the object. But sometimes you just want to create an object with some behaviour but somebody else should decide (centrally) what concrete class it is.
For example you must create one kind of object in 100 places. But later you might find out, that you must change the class -- you would have to change all those places.
The factory will eliminate this need by defining one place that you must change.
The simplest factory would be:
def fabricateAnotherObject(self, **kwargs):
return testClass(**kwargs)
Of course, this might be of little help in some situations. So some factories might also load the class names from db or some other configuration. But the most simple solution is a hard-coded construction of the object -- only this method must be changed in our example, when you choose to always call this method.
A somewhat more dynamic solution (without need for a db):
class Factory(object):
def __init__(self, theClass):
self.theClass = theClass
def create(self, **kwargs):
self.theClass(**kwargs)
myFactory = Factory(testClass)
The myFactory instance can be used in different locations for creating the correct instances. The problem is, how to initialize myFactory -- in some special module??
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With