Home > Software design >  Data object and editor object design pattern
Data object and editor object design pattern

Time:08-11

Not sure how to ask this question but I'm looking for a clean way to solve a problem for a project. I'm trying create a class library that will allow someone to create a randomized character for a game and use the data for a web app. I have a CharacterModel class that has all the needed data publicly exposed, however I need a way to edit this information without making the setters publicly available. Also, if I have multiple characters in the future, how do I get the editor to effect specific objects? Is there a design pattern for this type of problem?

CodePudding user response:

Make the setters public. If someone is using your library to create instances of CharacterModel and they plan to use it in a web app, they will know how to wrap access to the data in a safe way.

They will most likely use your library to create instances of your class, then store them serialized in a data store. When they send the data to a user of their app to look at and interact with, they'll either expose it through a web api of some kind to be accessed by a front-end SPA client or they will use a framework to render the data as HTML (e.g. ASP.NET Core w/ razor pages or mvc).

Whenever the user of their app requests changes to the object they will pass requests from the webpage or from the SPA client back to the server (either controller endpoints or through form posts) and they will use an auth system, character model identifiers and other code to determine what changes the user can apply before they save the changes to the data store (usually a database or a document/nosql system).

In other words, in relation to your question: "...if I have multiple characters in the future, how do I get the editor to effect specific objects?"

...you'll let the consumer of your library figure that out when they are building an application with it.

  • Related