How to implement the Parcelable class?

Asked

Viewed 131 times

1

I need help when it comes to object orientation.

I need to implement the class Parcelable in the model Produto, but for the sake of organization, I would not like to implement directly in the class Produto and create a new class ProdutoParcelable and implement there and make the class Produto identify that implementation or "become" that implementation.

The detail is that in the possible class ProdutoParcelable I’ll need the class attributes Produto.

public class Produto extends ProdutoParcelable

public class ProdutoParcelable implements Parcelable
//Aqui vou precisar dos atributos de Produto
  • 1

    If in this class you will need the attributes of the child class, your abstraction of the problem is wrong. What would be the goal of dividing into different classes?

  • @Andersoncarloswoss The goal would be simply organization. Leave the Product class cleaner. Thank you.

  • So why not define the attributes of Produto which will be needed in ProdutoParcelable directly in this class?

  • @Andersoncarloswoss I will try! Thank you for your attention!

1 answer

1

In case of your problem, I would use a Product interface, where it would list the attributes of a product, and create a class ProdutoModel and ProdutoParcelable, both implementing Product. So when you were creating a normal product (without Parcelable) you would use Produto produto = new ProdutoModel();. But I don’t suggest you do two classes for it, you are increasing the complexity of understanding your code needlessly and it may be that in the future you tangle with it. Own experience.

On the implementation of Parcelable, access that link that has a full tutorial there.

  • Sometimes it is appropriate to make a serialization DTO distinct from the business object, hence the need of Rafa Yaw. It is also interesting to try to focus on business business, and serialization on serialization. Mixing the two responsibilities can (but not necessarily) leave the code... dirty? , polluted? , ugly? , counterintuitive? As always, it depends on the case

  • 1

    That, I went through a situation like this and I had certain problems, but I understand the premise of Rafa, anyway, As long as it is well documented there will be no problems.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.