Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
For 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
completion of example t6 - add factory, comparator, etc
Applying OO design techniques and best practise example from a Well House Consultants training course
More on Applying OO design techniques and best practise [link]

This example is described in the following article(s):
   • Inheritance, Composition and Associated objects - when to use which - Python example - [link]
   • Object factories in C++, Python, PHP and Perl - [link]

Source code: t7 Module: Y116

# We have TABLES of various sorts in a room
# Associated with each table we have a waitress and multiple customers

# We're running a restuarant, seating guests, etc!

# to example t6, we have added some further OO demos
# - a factory method (static call that returns a constructor)
# - a comparator to compare two objects derived from the same base
# - a static method to compare a whole list of similar objects
# - a method to print out an object by overriding the default

class person (object):
        def getname(this):
                return this.name

class customer(person):
        def __init__(this,name,partysize):
                this.name = name
                this.partysize = partysize
        def getsize(this):
                return this.partysize

class waitress(person):
        def __init__(this,name,capacity):
                this.name = name
                this.capacity = capacity
                this.ccc = 0 # current customer count / not yet used

# ---------------------------------------------------

class table(object):
        def __init__(current,material,w,h,wt):
                current.w = w
                current.h = h
                current.place = 925
        def getbums(current):
                side = int(current.w/current.place)
                wide = int(current.h/current.place)
                return 2 * (side + wide)
        def setplace(current,p):
                current.place = p
        def getdoggie(current):
                if current.w < current.h:
                        return current.w/2
                return current.h/2
        def _sng(current,material,wt):
                current.wt = wt
                current.m = material
                current.parties = []
        def getwaitstaff(self):
                return self.wt.getname()
        def seatguests(self,pname):
                so_far = self.getseated() # already at table
                adding = pname.getsize() # wanting to join
                holds = self.getbums() # capacity of table

                if holds >= so_far + adding:
                        return True

                return None

        def getseated(self):
                so_far = 0
                for pty in self.parties:
                        so_far += pty.getsize()
                return so_far

        def findplace(tablist,client):
                for t in tablist:
                        if t.seatguests(client):
                                placed = 1
                        placed = None
                return placed
        findplace = staticmethod(findplace) # Now an oldfashioned way, still works

        @staticmethod # This is the new way - a.k.a. "decorator"
        def myFactory(sourcestring):
                els = sourcestring.split(" ")
                print els
                if els[0] == "Re":
                        server = waitress(els[3],10)
                        # Should really have called method to search for this waitress
                        # and then re-used her!
                        return recttable(els[4],int(els[1]),int(els[2]),server)
                elif els[0] == "Ro":
                        # code to be written
                elif els[0] == "Sq":
                        # code to be written
                        print "oops"
                        return None

        def __str__(self):
                whattype = self.__class__.__name__
                # Above would be better as call to "gettype" on object to return English text
                rz = "A " + whattype + " made of " + self.m + "."
                return rz

        def getmore(this,that):
                s1 = this.getbums()
                s2 = that.getbums()
                if s1 > s2: return this
                return that

        def getmost(these):
                sofar = these[0]
                for maybe in these[1:]:
                        sofar = sofar.getmore(maybe)
                return sofar

class recttable(table):

class squaretable(table):
        def __init__(current,material,w,wt):
                current.w = w
                current.h = w
                current.place = 925

class roundtable(table):
        def __init__(current,material,w,wt):
                current.w = w
                current.h = w
                current.place = 800
        def getbums(current):
                side = int(current.w * 3.14159265 / current.place)
                return side

# ----------------------------------------------

if __name__ == "__main__":

        lead = waitress("Heather",12)
        assist = waitress("Poppy",10)

        cafe = [recttable("Cherry",2100,980,lead),
                squaretable("oak",5000,assist) ]

# line of data that needs converting to object:
        fromfile = "Re 1500 2100 Rachel Teak 2005"

        for tab in cafe:
                ps1 = tab.getbums()
                dog = tab.getdoggie()
                order = tab.getwaitstaff()
                print "We can seat",ps1,"and doggie distance is",dog,"order from",order

        if not table.findplace(cafe,customer("Alex",4)): print "nowhere for Alex"
        if not table.findplace(cafe,customer("Simon",3)): print "nowhere for Simon"
        if not table.findplace(cafe,customer("Bob",2)): print "nowhere for Bob"
        if not table.findplace(cafe,customer("Santa",122)): print "nowhere for Santa"
        if not table.findplace(cafe,customer("Sally",3)): print "nowhere for Sal"

        for tab in cafe:
                ps1 = tab.getbums()
                ps2 = tab.getseated()
                order = tab.getwaitstaff()
                print tab,"We have",ps2,"at a table for",ps1,"looked after by",order

# comparator

        moreseats = cafe[2].getmore(cafe[4])
        print "More seats on",moreseats

        mostseats = table.getmost(cafe)
        print "Most seats on",mostseats

# ------------------------------------------------------

munchkin:opy grahamellis$ python t7
['Re', '1500', '2100', 'Rachel', 'Teak', '2005']
We can seat 6 and doggie distance is 490 order from Heather
We can seat 2 and doggie distance is 375 order from Poppy
We can seat 5 and doggie distance is 750 order from Heather
We can seat 20 and doggie distance is 2500 order from Poppy
We can seat 6 and doggie distance is 750 order from Rachel
nowhere for Santa
A recttable made of Cherry. We have 6 at a table for 6 looked after by Heather
A recttable made of Plastic. We have 0 at a table for 2 looked after by Poppy
A roundtable made of Ash. We have 3 at a table for 5 looked after by Heather
A squaretable made of oak. We have 3 at a table for 20 looked after by Poppy
A recttable made of Teak. We have 0 at a table for 6 looked after by Rachel
More seats on A recttable made of Teak.
Most seats on A squaretable made of oak.
munchkin:opy grahamellis$

Learn about this subject
This module and example are covered on the following public courses:
 * Learning to program in Python
 * Python Programming
 * Intermediate Python
Also available on on site courses for larger groups

Books covering this topic
Yes. We have over 700 books in our library. Books covering Python are listed here and when you've selected a relevant book we'll link you on to Amazon to order.

Other Examples
This example comes from our "Applying OO design techniques and best practise" training module. You'll find a description of the topic and some other closely related examples on the "Applying OO design techniques and best practise" module index page.

Full description of the source code
You can learn more about this example on the training courses listed on this page, on which you'll be given a full set of training notes.

Many other training modules are available for download (for limited use) from our download centre under an Open Training Notes License.

Other resources
• Our Solutions centre provides a number of longer technical articles.
• Our Opentalk forum archive provides a question and answer centre.
The Horse's mouth provides a daily tip or thought.
• Further resources are available via the resources centre.
• All of these resources can be searched through through our search engine
• And there's a global index here.

Purpose of this website
This is a sample program, class demonstration or answer from a training course. It's main purpose is to provide an after-course service to customers who have attended our public private or on site courses, but the examples are made generally available under conditions described below.

Web site author
This web site is written and maintained by Well House Consultants.

Conditions of use
Past attendees on our training courses are welcome to use individual examples in the course of their programming, but must check the examples they use to ensure that they are suitable for their job. Remember that some of our examples show you how not to do things - check in your notes. Well House Consultants take no responsibility for the suitability of these example programs to customer's needs.

This program is copyright Well House Consultants Ltd. You are forbidden from using it for running your own training courses without our prior written permission. See our page on courseware provision for more details.

Any of our images within this code may NOT be reused on a public URL without our prior permission. For Bona Fide personal use, we will often grant you permission provided that you provide a link back. Commercial use on a website will incur a license fee for each image used - details on request.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2022: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/resources/ex.php • PAGE BUILT: Sun Oct 11 14:50:09 2020 • BUILD SYSTEM: JelliaJamb