mercredi 23 septembre 2015

Python OOP Factory Pattern

I am still learning OOP Design Pattern and everything I have read so far says that Factory Patterns are commonly used. I am still trying to learn this pattern. For my program I am creating an Abstract Factory called "person" and my factory called "personFactory" should let you create different types of people (Users, Customers, Plumbers, etc...). With my current code I am getting this error:

AttributeError: 'module' object has no attribute 'createUser'

This is my code:

person.py

import abc

class person:
    __metaclass__ = abc.ABCMeta
    fName = ""
    mName = ""
    lName = ""
    address = ""
    city = ""
    state = ""
    zipcode = ""
    email = ""
    phone = ""
    dob = None

    @abc.abstractmethod
    def __init__(self,fName=None,mName=None,lName=None,address=None,city=None,state=None,zipcode=None,email=None,phone=None,dob=None):
        self.fName = fName
        self.mName = mName
        self.lName = lName
        self.address = address
        self.city = city
        self.state = state
        self.zipcode = zipcode
        self.email = email
        self.phone = phone
        self.dob = dob

personFactory.py

from person import person

class personFactory(person):

    def createUser(self):
        uName = ""
        password = ""
        role = ""
        def __init__(self,uName,password,role):
            self.uName = uName
            self.password = password
            self.role = role
        def login(uName,password):
            if(uName == self.uName and password == self.password):
                return "Logged In"
            else:
                return "Did not log in"
        def logout():
            return "Logout"

    def createCustomer(self):
        items = []
        balace = 0
        def __init__(self,balance):
            self.balance = balance
        def AddToCart(item):
            self.items.append(item)
            print("Item added")
        def Order(items):
            totalprice = 0
            for item in items:
                totalprice =+ item.price
            return totalprice
        def Pay(payment):
            self.balance =- payment
            return self.balance

main.py

import personFactory

class main():

    user1 = personFactory.createUser()
    user1.fName = "John"
    user1.lName = "Smith"
    user1.uName = "jSmith"
    user1.password = "Jamestown"

    customer1 = personFactory.createCustomer()
    customer1.fName = "George"
    customer1.lName = "Washington"
    customer1.balance = 100

main()

I'm not sure why I cannot call createUser. What am I doing wrong? Any help would be greatly appreciated!

Aucun commentaire:

Enregistrer un commentaire