Python Tutorial

Showing posts with label design pattern. Show all posts
Showing posts with label design pattern. Show all posts

Friday, November 23, 2012

Design pattern in python : Factory method


Factory method pattern in python.
All source code available on github

class Book:
    def book_category(self):    pass

class PythonBook(Book):
    def book_category(self):
        print "Python book"

class JavaBook(Book):
    def book_category(self):
        print "Java book"

class BookFactory:
    def get_book(self, book_type):
        if book_type=='python':
            return PythonBook()
        elif book_type=='java':
            return JavaBook()
        else:
            return None

bookFactory = BookFactory()
pythonBook = bookFactory.get_book('python')
pythonBook.book_category()

javaBook = bookFactory.get_book('java')
javaBook.book_category()



Output:
Python book
Java book

Design pattern in python: Template method


Template method design pattern in python.
All source code available on github

class MakeMeal:

    def prepare(self):  pass
    def cook(self): pass
    def eat(self):  pass

    def go(self):
        self.prepare()
        self.cook()
        self.eat()

class MakePizza(MakeMeal):

    def prepare(self):
        print "Prepare Pizza"

    def cook(self):
        print "Cook Pizza"

    def eat(self):
        print "Eat Pizza"

class MakeTea(MakeMeal):

    def prepare(self):
        print "Prepare Tea"

    def cook(self):
        print "Cook Tea"

    def eat(self):
        print "Eat Tea"

makePizza = MakePizza()
makePizza.go()

print 25*"+"

makeTea = MakeTea()
makeTea.go()



Output:
Prepare Pizza
Cook Pizza
Eat Pizza
+++++++++++++++++++++++++
Prepare Tea
Cook Tea
Eat Tea

Tuesday, October 9, 2012

Design pattern in python : decorator pattern


Python decorator allow us to modify or inject code into a function and classes. It is very easy to use.
Here something we need to remember, __init__ function exceute when decorator is declare and __call__ execute when function('doSomethingCopy') will call independently. We can also call function('doSomethingCopy') from anywhere of the decorator class('MyDecorator').

 
class MyDecorator(object):
    def __init__(self, f):
        self.doSomethingCopy=f
        print "This line will call on decoration"

    def __call__(self,p,q): # here we pass the function parameter 
        print "This line will work when we call the function 'doSomething' "
        self.doSomethingCopy(p+1,q+1)

@MyDecorator
def doSomething(a,b):
    print "I am doing something"
    print "A ",a," B ",b

doSomething(3,4)


Output:
This line will call on decoration
This line will work when we call the function 'doSomething' 
I am doing something
A  4  B  5

Monday, September 17, 2012

Design pattern in python: Abstract factory pattern



Abstract factory pattern in python.


 
class Shop(object):
    def __init__(self, item_factory = None):
        self.item_factory=item_factory

    def get_item_description(self):
        item=self.item_factory.get_item()
        print "Item model ",self.item_factory.model()
        print "Item name ",item.name()
        print "Item color ",self.item_factory.color()

class Shoes(object):
    def name(self):
        return "Shoes"
    def __str__(self):
        return "Shoes"

class Mobile(object):
    def name(self):
        return "Nokia"
    def __str__(self):
        return "Nokia"

class ShoesFactory(object):
    def get_item(self):
        return Shoes()
    def model(self):
        return "MEN101"
    def color(self):
        return "Black"

class MobileFactory(object):
    def get_item(self):
        return Mobile()
    def model(self):
        return "NOK404"
    def color(self):
        return "Black deep"

shopShoes=Shop(ShoesFactory())
shopShoes.get_item_description()
print "*"*30
shopMobile=Shop(MobileFactory())
shopMobile.get_item_description()



Output:
Item model  MEN101
Item name  Shoes
Item color  Black
******************************
Item model  NOK404
Item name  Nokia
Item color  Black deep

Design pattern in python: Singleton pattern



Python singleton design pattern example:
For implementing Singleton pattern we need to override the "__new__" method of class


class MyClass:
 def __init__(self):
  self.numA = 5
  self.numB = 10
 
 def getNumA(self):
  return self.numA

 def getNumB(self):
  return self.numB

 def setNumA(self, v):
  self.numA = v

 def setNumA(self, v):
  self.numB = v

 def toString(self):
  return "A:",self.numA,"B:",self.numB

_singleton = None

def get_instance(cls = MyClass):
 global _singleton
 if _singleton is None:
  _singleton = cls()
 return _singleton

if __name__=='__main__':
 print "Main output"

 a = get_instance()
 print a.toString()
 a.setNumA(15)
 print a.toString()

 b = get_instance()
 print b.toString()

Output:
Main output
('A:', 5, 'B:', 10)
('A:', 5, 'B:', 15)
('A:', 5, 'B:', 15)