簡單工廠模式和工廠方法模式在Python中的實現

簡單工廠模式



class Book(object):

    def __init__(self,name, price):
        self.name = name
        self.price = price

    def read(self):
        print("Book name is {name}, Book's price is {price}".format(name=self.name,price=self.price))


class JavaBook(Book):

    def __init__(self,name,price,lang="java"):
        super(JavaBook,self).__init__(name,price)
        self.lang = lang

    def read(self):
        super(JavaBook,self).read()
        print("Book is written by {lang}".format(lang=self.lang))


class PythonBook(Book):

    def __init__(self,name,price,lang="python"):
        super(PythonBook,self).__init__(name,price)
        self.lang = lang

    def read(self):
        super(PythonBook,self).read()
        print("Book is written by {lang}".format(lang=self.lang))


class BookSimpleFactory(object):

    def __init__(self):
        pass

    def getBook(self,name, price, lang=""):
        if lang == "python":
            return PythonBook(name, price)
        elif lang == "java":
            return JavaBook(name,price)
        else:
            return Book(name,price)


if __name__ == "__main__":

    bookfactory = BookSimpleFactory()

    book1 = bookfactory.getBook("11-python", "25", "python")

    book1.read()

    book2 = bookfactory.getBook("12-python", "99", "python")

    book2.read()

    book3 = bookfactory.getBook("21-java", "46", "java")

    book3.read()

    book4 = bookfactory.getBook("31-english", "199")

    book4.read()

 執行一下

C:\Python27\python.exe E:/python-codes/factory-pattern/FactoryPatternSample.py
Book name is 11-python, Book's price is 25
Book is written by python
Book name is 12-python, Book's price is 99
Book is written by python
Book name is 21-java, Book's price is 46
Book is written by java
Book name is 31-english, Book's price is 199

Process finished with exit code 0

 

 

工廠方法模式

 



import abc
import random


class Book(object):

    def __init__(self,name, price):
        self.name = name
        self.price = price
        self.publisher = None

    def read(self):
        print("Book name is {name}, Book's price is {price}".format(name=self.name,price=self.price))
        if self.publisher:
            print("Book: {name} is published by {publisher} ".format(name = self.name, publisher = self.publisher))

    def setpublisher(self, publisher):
        self.publisher = publisher


class JavaBook(Book):

    def __init__(self,name,price,lang="java"):
        super(JavaBook,self).__init__(name,price)
        self.lang = lang

    def read(self):
        super(JavaBook,self).read()
        print("Book:{name} is written by {lang}".format(name = self.name, lang=self.lang))


class PythonBook(Book):

    def __init__(self,name,price,lang="python"):
        super(PythonBook,self).__init__(name,price)
        self.lang = lang

    def read(self):
        super(PythonBook,self).read()
        print("Book is written by {lang}".format(lang=self.lang))


class AbstractBookFactory(object):
    def __init__(self):
        pass

    @abc.abstractmethod
    def getBook(self, lang):
        pass


class IndustryBookFactory(AbstractBookFactory):
    def __init__(self):
        super(IndustryBookFactory, self).__init__()

    def getBook(self, lang):
        if lang == "python":
            book = PythonBook("Python-book-%s" % (int(10*(random.random()))), int(100*random.random()))
        elif lang == "java":
            book = JavaBook("Java-book-%s" % (int(10*(random.random()))), int(100*random.random()))
        else:
            book = Book("Another-book-%s" % (int(10*(random.random()))), int(100*random.random()))
        book.setpublisher("IndustryPubish")
        return book


class PeopleBookFactory(AbstractBookFactory):
    def __init__(self):
        super(PeopleBookFactory, self).__init__()

    def getBook(self, lang):
        if lang == "python":
            book = PythonBook("Python-book-%s" % (int(100*(random.random()))), int(100*random.random()))
        elif lang == "java":
            book = JavaBook("Java-book-%s" % (int(100*(random.random()))), int(100*random.random()))
        else:
            book = Book("Another-book-%s" % (int(100*(random.random()))), int(100*random.random()))
        book.setpublisher("PeoplePubish")
        return book


if __name__ == "__main__":

    peoplebookfact = PeopleBookFactory()
    book1 = peoplebookfact.getBook("python")
    book1.read()
    print("-"*20)
    book2 = peoplebookfact.getBook("java")
    book2.read()
    print("-"*20)
    book3 = peoplebookfact.getBook("hahah")
    book3.read()
    print("-"*20)

    industrybookfactory = IndustryBookFactory()
    book11 = industrybookfactory.getBook("python")
    book11.read()
    print("-"*20)
    book21 = industrybookfactory.getBook("java")
    book21.read()
    print("-"*20)
    book31 = industrybookfactory.getBook("hahah")
    book31.read()
    print("-"*20)

執行一下:

C:\Python27\python.exe D:/代碼/python-DP/FactoryMethodSample.py
Book name is Python-book-99, Book's price is 3
Book: Python-book-99 is published by PeoplePubish 
Book is written by python
--------------------
Book name is Java-book-46, Book's price is 81
Book: Java-book-46 is published by PeoplePubish 
Book:Java-book-46 is written by java
--------------------
Book name is Another-book-4, Book's price is 43
Book: Another-book-4 is published by PeoplePubish 
--------------------
Book name is Python-book-2, Book's price is 5
Book: Python-book-2 is published by IndustryPubish 
Book is written by python
--------------------
Book name is Java-book-4, Book's price is 92
Book: Java-book-4 is published by IndustryPubish 
Book:Java-book-4 is written by java
--------------------
Book name is Another-book-2, Book's price is 96
Book: Another-book-2 is published by IndustryPubish 
--------------------

Process finished with exit code 0

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章