Factory design Pattern in Python


Hi Friends today I have learned about design patterns.

The Factory Design Pattern concepts are similar to factory concepts. (i.e,) In factory How small individual parts are manufactured and send to the main area like that . In python also the subclasses functions do the function according to the functions of the mainclass.

I have created an example program for the factory method in python : -

Here I have created a Movie factory , which used to create films and send results according to the queries from main class of movies. Have a check at the code I have tried.

MovieFactory code is : -

def Movies(type):
 if type == 5:
  class Predator:
   def director(input):
    print "The movie was directed by John McTiernan"
   def moviereview(input):
    print "The Movie was Good"
  return Predator()
 elif type == 4:
  class JurassicPark:
   def director(input):
    print "The movie was directed by Stefen speilberg"
   def moviereview(input):
    print "The film was Best"
  return JurassicPark()
 elif type == 3:
  class Avatar:
   def director(input):
    print "The movie was directed by James Cameroon"
   def moviereview(self):
    print"The film was good"
  return Avatar()

And the Main Class Movies code is : -

from moviefactory import Movies
a = Movies(5)
b = Movies(4)
c = Movies(3)
input = str(raw_input('''
Do you want to know the director of the film and its review
We have 3 films in our list Predator, Jurassic Park, Avatar
To know the director and film reviw, Press the Movie Name
'''))
if input == 'Predator':
 print a.director(), a.moviereview()
elif input == 'Jurassic Park':
 print b.director(), b.moviereview()
elif input == 'Avatar':
 print c.director(), c.moviereview()
else:
 print "you have misspelled the word"

No comments:

Post a Comment