How to refactor “nested” view classes to avoid deep method calls?

Lets say I’m displaying a bunch of data (model) using a View class for rendering. However, a lot of the data has sub-data (models) complicated enough to require separate rendering classes.

In my design, a View class has a model which it is rendering, and has many children Views which display the sub-data. In some cases, A View, while containing a model, may not have anything to render and serves more as a wrapper for it children.

However, if you have very complex data and your sub-views have subviews,this design results in deeply nested method calls. Some methods are simply passing information to view classes who may do nothing with it except pass it to their children. This seems inefficient, so I figured there might to be a pattern or something that solves this more elegantly.

4

Looks to me like you’re not really separating the model and the view, while being in a situation where such a separation is exactly what you need.

If “a View class has a model which it is rendering” then there’s really no separation between them, you can just make one class for that. I’m not saying that there’s anything inherently wrong with that, for example the Spring (Web) MVC framework has a ModelAndView class which can contain references to a (composite) model and a view. I’m just thinking that in your case it would be better if you’d externalize the relation between model and view: have your Model classes completely separated from your View classes, and then have some sort of manager that decides what View is used to display what Model at a given time.

Then make each of these as granular as needed to be able to reuse them, i.e. if you have some piece of model and a certain way to display it which is repeated as part of multiple views (but at the same time that piece of Model is also needed to be displayed differently in other views) then make Model and View classes for each of these situations, and have a manager decide with what view that model should go depending on the current context (parent view, request, etc).

2

On the one hand we teach ourselves to use design patterns and common sense design techniques, from which emerges the ideal of designing close to conceptual models, and, on the other hand, we’re concerned when our resulting models become deep. And we shouldn’t be.

If, conceptually, your views are aggregations of other views, then I see no problem with nested structures and the resulting delegation model. I see no problem with views delegating as part of fulfilling their role, as long as their interface is consistent with their purpose and speaks at a consistent level of abstraction.

Let me restate this. If you were to explain your view to colleagues, and the explanation best describes the view in terms of other views, then that is what your code should be like.

In fact, views that delegate may be simply working to realize the Law of Demeter (not something to go fanatic about, but definitely something nice to recognize in your designs).

I should say that I apply the above principles in my user interface code with good results. Event handling can become verbose, but you have EventBus to the rescue.

Assuming you have a sufficient templating language that allows for mixing and matching your templates, you should break out each relevant piece of your model into its own template which can be used to make a composite view.

For instance, you may have a Book model, which in turn has data for Bookmarks, perhaps some Highlights, and maybe PageNotes. Now each of these should have their own template that renders the content in a meaningful way, and this should be achieved by passed in only the relevant data into the template.

Why would one want to take this approach? It allows you to separate your concerns. You model should be used to marshal your data and move it from point A to point B, and your view should simply render it for consumption.

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