vendredi 10 juillet 2020

What design pattern to use to make views reusable?

Suppose, I have some view that is configured as following:

func configure(viewModel: HelloViewModel) {
    name.text = viewModel.name 
    surname.text = viewModel.surname 
    checkboxImage.image = viewModel.isMember ? UIImage(named: "member") : UIImage(named: "not-member")
}

HellowViewModel is simple struct, not protocol. Now, I need to use this view but with only following change: checkboxImage's image should be different based on viewModel.isMember (not member or not-member). It means, I should now modify my configure method which has bad design. But, how can I solve my task without modifying too much? Maybe there are design patterns to solve this kind of problems? By myself, I should handle that following way:

Create protocol for this view. It will be accepted in configure method.

protocol HelloPresentable {
    var name: String { get }
    var surname: String { get }
    var image: UIImage { get }
}

My configure method should accept this protocol. And then it will be reusable. But currently it accepts a struct, not a protocol. So, how to handle problem in this case?

Aucun commentaire:

Enregistrer un commentaire