Pattern for loading and handling resources

Many times there is the need to load external resources into the program, may they be graphics, audio samples or text strings.

Is there a patten for handling the loading and the handling of such resources?

For example: should I have a class that loads all the data and then call it everytime I need the data? As in:

GraphicsHandler.instance().loadAllData()
...//and then later:
draw(x,y, GraphicsHandler.instance().getData(WATER_IMAGE))
//or maybe
draw(x,y, GraphicsHandler.instance().WATER_IMAGE)

Or should I assign each resource to the class where it belongs?
As in (for example, in a game):

Graphics g = GraphicsLoader.load(CHAR01);
Character c = new Character(..., g);
...
c.draw();

Generally speaking which of these two is the more robust solution?

GraphicsHandler.instance().getData(WATER_IMAGE)
//or
GraphicsHandler.instance().WATER_IMAGE //a constant reference

I don’t know that either option is going to be particularly robust. In both cases, you’re relying on some kind of singleton or static-state-heavy implementation to manage what amount to global variables. How exactly you access the global variables (whether directly or hidden behind some kind of weakly typed “getData()” call) isn’t going to matter a lot to maintainability in the face of a global variable scheme in general. Your application is going to be highly cross-coupled and interdependent either way.

I would say a decent option with an eye on future flexibility would be to take your getData() concept and put it in an interface. Then inject an instance of that interface into classes that need access to the data (and try to limit that number as much as possible). This way you can mock out or swap the thing that provides that data. The way you have it now, this is impossible — every client of that singleton depends on the entire application state being spun up in order for it to be used, making testing and decoupling difficult, if not impossible. And that state of affairs is the opposite of robust.

It depends on the purpose of your application.

In some games, there is a need for a highly responsive user interface and loading resources while playing could have a negative impact. Secondly, many games run with 100% CPU usage, esp. 3D shooters, so that any data which can be preloaded also becomes preloaded. This lowers the overall system requirements. As can be seen, there are performance reasons for preloading of resources. Finally, resources are often compressed inside one large archive to save disk space. It takes some time to decompress all files for the current level. That is the last reason for preloading.

Is the program a game? Does it need some of these performance-optimization techniques? Otherwise, the closest design pattern for this would be Facade:
http://en.wikipedia.org/wiki/Facade_pattern

Gfx ghost = (Gfx)ResourceLoader.load("/gfx/sprites/ghost");
Sound taunt = (Sound)ResourceLoader.load("/audio/boost/you_play_like_a_girl.voc");
Music levelMusic = (Music)ResourceLoader.load("/audio/music/level1.mp3");

With the solution above, there can be a resource cache and you want to log most frequently loaded resources. After that, you can preload those resources implicitly after your program starts because you know they will be needed. You should not however preload everything. For every level, different resources are needed.

1

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