jeudi 5 mars 2015

Python am I missing Design Pattern

I am working on python application, this project started for internal need the the soft is spreading to our departments. So I have time for writing again a lot of parts of my application. I want it becomes really scalabe, for the example:


First level: I have Job classes, these classes contain only variable. The number of class can increase.


Second level: I have "Worker", this class contains the common logic, whatever your job you will earn money at the end of the month. I will probably have only one type.


Third level: I have gender class, these classes contain specific logic. The number of class can increase.


You will find below a demo I did:



import abc

class Worker:
__metaclass__ = abc.ABCMeta
def __init__(self, job):
self.job = job

def get_job(self):
print "My job is : %s" % (self.job.name)


class Scientist:
name = 'scientist'

class Salesman:
name = 'salesman'

class Woman(Worker):
def gender(self):
print "I'm a woman %s\n" %(self.job.name)

class Man(Worker):
def gender(self):
print "I'm a man"

if __name__ == "__main__":
man = Man(Salesman())
woman = Woman(Scientist())

man.gender()
man.get_job()

woman.gender()
woman.get_job()


So this model seems to be scalable, It's very easy to add a new collection of variables and can add a new class if I want a new behavior for gender (like i'm a baby)


But am I doing right? Am I missing Design Patterns? There is a way to be more scalable?


Aucun commentaire:

Enregistrer un commentaire