2014年5月10日土曜日

開発環境

Learning Python (Mark Lutz (著)、Oreilly & Associates Inc)のPART Ⅵ.(Classes and OOP)、CHAPTER 32(Advanced Class Topics)、Test Your Knowledge: Part VI Exercises 7.(Composition)を解いてみる。

その他参考書籍

7.(Composition)

コード(BBEdit)

fastfood.py

#!/usr/bin/env python3
#-*- coding: utf-8

class Lunch:
    def __init__(self):
        self.customer = Customer()
        self.employee = Employee()
    def order(self, foodName):
        self.customer.placeOrder(foodName, self.employee)
    def result(self):
        self.customer.printFood()

class Customer:
    def __init__(self):
        self.food = None
    def placeOrder(self, foodName, employee):
        self.food = employee.takeOrder(foodName)
    def printFood(self):
        print(self.food.name)

class Employee:
    def takeOrder(self, foodName):
        return Food(foodName)

class Food:
    def __init__(self, name):
        self.name = name

if __name__ == '__main__':
    lunch = Lunch()
    lunch.order('burritos')
    lunch.result()

入出力結果(Terminal)

$ ./fastfood.py 
burritos
$

The Employee is the active agent.

fastfood1.py

#!/usr/bin/env python3

class Lunch:
    def __init__(self):        
        self.employee = Employee()
        self.customer = Customer()
    def order(self, foodName):
        self.employee.takeOrder(foodName, self.customer)
    def result(self):
        self.employee.printFood()

class Employee:
    def __init__(self):
        self.food = None
    def takeOrder(self, foodName, customer):
        self.food = customer.placeOrder(foodName)
    def printFood(self):
        print(self.food.name)

class Customer:
    def placeOrder(self, foodName):
        return Food(foodName)

class Food:
    def __init__(self, name):
        self.name = name

if __name__ == '__main__':
    lunch = Lunch()
    lunch.order('burritos')
    lunch.result()

入出力結果(Terminal)

$ ./fastfood1.py 
burritos
$

0 コメント:

コメントを投稿