Access items of a container with a pattern

I’ve two C++ classes with a relation: a container class which has a list of items. For example a library has got many books. The classes are the library and the book.

Now from the external world I have to create, update and delete books. In addition there other classes that are interested to know when an event on a book happens and these (observers) instances can be created at run-time.

Now I’ve got different ways to proceed:

1) Use always the container to do the operation: central point of management but I have to create couples of methods for example setTitle(bookID) that call setTitle() on the book, bad;

2) Use a getBook() on the container to return a reference of the book, allow the modification, at this point I have to use a commit method to notify the container which can notify the observer but in this way the pattern doesn’t force a “commit”, I mean if the external caller doesn’t call commit() I loose 🙂

3) Use a getBook() on the container to return a copy of the book and an update method of the container to overwrite the book, central point of management again but the book if big is a problem, a lot of copies are really bad.

Any tips? Is there any pattern that covers this use case?

2

Interesting question. If the language were C# or Java, you could use reflection, proxies, or delegates in order to learn whether or not an event occurred in the book class. Granted, these solutions are a little heavy handed for such a simple matter, however the problem remains.

C++ doesn’t have reflection, proxies, or delegates. However, what you can do is ensure that objects like Book derive from a common class that keeps track of changes.

Specifically, I’m talking about the Subject/Observer pattern. Book would derive from the Subject class whose sole methods would be:

public:
   bool isChanged() const;
   void resetChanged();

   void addChangeObserver(Observer &observer);
protected:
   void setChanged();

The idea is that Subject can tell you if an object has changed and its changed status can be reset. In addition it lets you keep track of observers through addChangeObserver.

Book would derive from Subject, and Library would initially reset the change status of all added books. It would then have a getBook method and assuming Book responsibly sets the change status, Library could later know it. If you wish to perform an action upon a change event, then it would add itself as an Observer for all books added (at which point Library would also have to derive from Observer).

Again, this is a little overboard, but you gain all the advantages of decoupling at the slight expensive of complicating the code. However, in a large project, you almost have to do it this way since keeping code decoupled is increasingly important as it grows.

I hope that helps.

3

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