mardi 30 juin 2015

How do I implement this solution (strategy design pattern) in python? I have some rough code here

I am trying to implement this solution in python using the strategy design pattern. I am totally new to OOP, currently working with my professor on this problem.

Basically the question is to have an abstract class( or rather an interface), and 2 concrete child classes (car and bus), that have the method "find()" that returns the cost of taking the transportation. After they return their respective costs, the abstract parent class will choose the cheaper transportation and output a print statement "I choose bus".

Here is the rough code. Appreciate someone who can help me out! I believe my code is very wrong somewhere.

from abc import ABCMeta, abstractmethod

class transporter(metaclass=ABCMeta):
    'abstract parent class'
        @abstractmethod
        def find(self, cost):
            pass

    class car(transporter):
        def find(self):
            return "Cost of car travel is 50"

        def cost(self):
            return C = 50


    class bus(transporter):
        def find(self):
            return"Cost of bus travel is 20"

        def cost(self):
            return B = 20



    if __name__ == "__main__" :


        find_car = car()
        find_bus = bus()

        find_car.cost().execute()
        find_bus.cost().execute()

Aucun commentaire:

Enregistrer un commentaire