Method pattern and method coupling

Disclaimer: I am going to write in Python and in the context of web development with Django, but this question is not language nor framework specific.

Let’s say I have a PizzaManager class that serves as a utils tool to select pizzas in a Restaurant menu. The pizza manager uses data the restaurant has to perform its different actions:

pizza_manager = PizzaManager(restaurant)

The PizzaManager acts as a filter on a specified list of pizzas. The manager deals with different lists of pizzas at a time in different scenarios. So the same pizza manager may be working with all_pizzas at a certain stage and with pepperoni_pizzas at another stage. Since the source pizzas is a changing variable, we decided to pass it in via method chaining, rather than using a traditional setter:

pizza_manager.with_pizzas(all_pizzas)
pizza_manager.with_pizzas(pepperoni_pizzas)
pizza_manager.with_pizzas(pizzas_of_the_year)

Essentially, the with_pizzas method works as follow:

def with_pizzas(self, pizzas):
    self.pizzas = pizzas
    return self

As I mentioned, the main job of the manager is to apply filters to the passed in pizzas. For example, let’s say we define a cheap_pizzas method that selects the cheap pizzas from the self.pizzas list:

def cheap_pizzas():
    self.filtered_pizzas = [pizza for pizza in self.pizzas if pizza.price <= self.threshold_price]
    return self

all_pizzas = restaurant.pizza_set.all()
pizza_manager.with_pizzas(all_pizzas ).cheap_pizzas()

We can keep filtering by chaining another method:

pizza_manager.with_pizzas(all_pizzas).cheap_pizzas().top_pizzas()

Finally, to retrieve the list of filtered pizzas we just run the get() method:

def get(self):
    return self.pizzas

best_pizzas = pizza_manager.with_pizzas(restaurant.pizza_set.all()).cheap_pizzas().top_pizzas().get()

There is one caveat though: you cannot run any of the filtering methods unless you have passed in the pizza list first. This can seem obvious, since the manager cannot operate on a non-existing list of pizzas, but this can be a pitfall when programming and you can make a mistake somewhat easily. You cannot do this:

# we are going to get a TypeError here since self.pizzas is None at this point
PizzaManager(restaurant).cheap_pizzas()

So basically my question is how to deal with this situation. How can I properly deal with method coupling while having chained methods at the same time? How can I make it clear, from a design point of view, that you must specify list of pizzas the manager is currently going to act upon before runnin any of the filtering methods?

Note that just passing the list of pizzas in the constructor is not a valid option, I don’t want to instantiate a single manager every time I want to use a different list of pizzas.

A common suggestion is to never create an object with invalid state. Pass the list of pizzas to the managers constructor and the problem goes away.

If your requirements are too complex for that to be possible, then the Builder pattern can help: use another object that contains the methods for adding pizzas, which then had a build method that copies them to the manager object and returns it, so your client code would look something like this:

pizza_manager_builder()
     .with_pizzas(some_pizza_list)
     .with_pizzas (more)
     .build()
     .top_selling(5)
     .cheapest ()

The implementation would then be something like:

class pizza_manager_builder:
    pizza_list = []
    def with_pizzas (self, pizzas):
       self.pizza_list.extend(pizzas)
       return self
    def build (self):
       assert len(self.pizza_list) > 0, "must set up new pizza list before invoking build"
       result = pizza_manager(self.pizza_list)
       self.pizza_list = []
       return result

class pizza_manager:
    def __init__(self, pizza_list):
       self.pizza_list = pizza_list
    def top_selling (self, count):
       ...
    def cheapest (self):
       ...

8

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật