How to avoid a singleton in this example?

I have a class Image and another one ImageStore. The Image it’s just a value object containing the package name and the image name. The ImageStore does all the actual work: calculates the real path of the image (based on the package and image name) and loads it from the server.

Now I have a quite big (and deep) hierarchy of nodes (this is a render engine for a game) so for example now I am in a group and I want to insert a new Image. I do:

var image = new Image("packageName", "imageName");

At this moment the Image doesn’t have any size yet, as I don’t want to hard code the image size (in this case this can be a tile, which is not always 64*32 for example) in my game view.

  • when the Image is added to the Group (I suppose the group is already attached to the Renderer) then the Group will be invalidated
  • the Renderer will request a new frame render.
  • it will parse the hierarchy tree of node and when it finds the Image I’ve added before
  • it will ask the ImageStore (or TileImageStore, this one not only stores the width and height but also the bottom center point of the tile. This only when the bottom center of the tile doesn’t match the bottom center point of the real image.) for the ImageData;
  • the ImageStore now loads the package, which also has the info about the width and height;
  • it will apply the new width and height to the image and will set a flag as loaded.

I would like to have somehow the ImageStore detached from the render. To be able to use only the Image and ImageStore without the need of the renderer.

Edit:
I want to do this because I would like to know the size of a image even when the image is not rendered. In order to get the image it has to ask the ImageStore for the size, anchor and imageData. The packages are like this:

package = {image01: {base64: "very very long string", width: 10, height: 20, anchorX: 1, anchorY: 10}}

Only after the ImageStore loads this big file is able to know the image width/height;

5

You just make an ImageStore and pass it into a Renderer you make. There is no reason for singletons here.

11

Without really knowing any further details:

Suggest you rename your existing Image struct, to ImageInfo
And create a new object called SelfLoadingImage, which has an ‘Image’ interface that the Renderer can use to get actual pixel data or whatever.

Then under the hood, how this pixel data is retrieved is a different problem that the Renderer doesn’t care about. You can now, for instance, have a HardcodedImage for testing purposes.

In the case of a SelfLoadingImage, it has a constructor that takes an ImageInfo, and an ImageStore and uses these when needed. But these instances of SelfLoadingImage get added as ‘Image’ objects to your rendering system.

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