OO Design: Algorithm changing State (Information Flow Question)

I have a class Context, which contains an instance of an abstract class State, which has multiple concrete subclasses (this is an implementation of the State Pattern).

Each State subclass executes a number of algorithms, which are themselves classes, and according to the outcome of an algorithm the state in the Context class might change.


My question is how should the algorithm make this state change in the Context class?

1) A simple solution would be to pass the Context object to the algorithm, but I don’t like that the algorithm know anything about the current State / Context.

2) I could write algorithm-specific wrapper classes of the Context class, where each wrapper has only one public function, namely to set the state of the Context according to the algorithm outcome.

3) Pass a function of Context which does a specific State change as function pointer to the algorithm and execute it when the state of Context should be changed.

While 1) is in my opinion the least favorable option, 2) would introduce a lot of new classes and I am not sure to what extent 3) is doable (in C++) and how Object Oriented this is.

What is the textbook way of dealing with this situation?

If the logic for choosing the next state is a part of the Algorithm classes, then clearly they must be able to communicate the specific state to change to to the Context class.

If the concept of states to change to is useless without the ‘context’ object (hard to tell), passing a reference to the context object should be fine – the algorithms and the context depend on each other and theres nothing you can do about it, so better make it as explicit as possible.

If algorithms dont really care if the state changes happen or not or what state is transitioned to (e.g. the logic for this resides entirely in the particular State subclass), you could pass a functor to the algorithm to be executed at the correct time, and the State subclass then implements the desired behavior (switching to a specific state).
You can do this using either an std::function, or if possible, by passing a lambda as a template parameter to a method of the algorithm class (which should be more performant since it can be inlined – std::function may even do a heap allocation).

So in a State subclass you might have something like:

void State::ExecuteAlgorithms(Context& context)
{
    algorithm.Execute([&context](){context.switchToState(*something*)});
}

Where algorithm.Execute looks like:

template<typename CallbackT>
void Algorithm::Execute(CallbackT switchStateCallback)
{
    *code*
    switchStateCallback();
}

Of course you might want to add more than one callback, or pass parameters to the callback, to allow for more complex logic for choosing the next state.

You can replace the lambda with a class (which is how its implemented internally anyways) if you dont have them available, or use std::function instead of a template if the algorithm isn’t just a single method or if you dont want templates.

2

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