-1

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()
Dave Schweisguth
  • 36,475
  • 10
  • 98
  • 121
Huang Kai
  • 31
  • 1
  • 3
  • 1
    Your indentation is wrong. `return C = 50` You can't assign in a return statement (which wouldn't do anything anyway; C is local to the function, but the function is returning). Why are you calling `.execute()` on the return value of `cost()`, which is presumably an int? Also this isn't the strategy pattern; it's just polymorphism. – Colonel Thirty Two Jul 01 '15 at 02:19
  • And specifying `metaclass=ABCMeta` won't work either, at least in 2.x - where did you see this stuff? Python isn't super well suited to these OO patterns, so you kind of have to contort it a bit (hence the weird ABC/mixin stuff) to get it to play along. And why are you declaring lower-case classes? And why are car and bus both declared inside transporter? – a p Jul 01 '15 at 02:24

1 Answers1

1

I'm no design pattern expert, but I think this is closer to what you want:

from abc import ABCMeta, abstractmethod
from operator import methodcaller

class Transportation(object):
    def __init__(self):
        # instantiate concrete transporter with lowest cost
        self.transporter = min((Car, Bus), key=methodcaller('cost'))()

    def go_for_ride(self):
        self.transporter.travel()

class Transporter(metaclass=ABCMeta):
    @abstractmethod
    def cost(self):
        pass

    @abstractmethod
    def travel(self):
        pass

class Car(Transporter):
    @classmethod
    def cost(cls):
        return 50

    def travel(self):
        print('riding in a car')

class Bus(Transporter):
    @classmethod
    def cost(cls):
        return 20

    def travel(self):
        print('riding in a bus')

if __name__ == "__main__" :
    transportation = Transportation()
    transportation.go_for_ride()  # -> riding in a bus
martineau
  • 119,623
  • 25
  • 170
  • 301