Object Management Constructors in several software versions

I am designing a library where I discuss whether to use a constructor to initialize objects in the api model. My thoughts:

  • Using the constructor, I can provide what data is needed to have a valid object in accordance with the model.

  • However, using the constructor, I am curious how to maintain the code, since it is modified to support this requirement without breaking existing code based code.

  • I could avoid using constructors in the design of objects, but I need a library to communicate with the developer, using it, which fields are required in order for the object to fit its model.

How to create an object model that expresses the requirement to initialize certain properties, as well as provide scalability with respect to code maintenance?

dim objV1 as new ObjectOfSomeType(Param1, Param2, Param3) 

VS

 dim objV1 as new ObjectOfSomeType() objV1.Property1 = Param1 objV1.Property2 = Param2 objV1.Property3 = Param3 
+4
source share
4 answers

The minimum required initial state for the object, that is, the data necessary for the deterministic behavior of the object, must be set during construction.

If you doubt the ability of clients using your API to successfully create your objects, you should publish a class containing the Factory method template and encourage and / or restrict the use of this.

+2
source

If for the reliability of the state of an object initialization of some properties is required, then such properties must be initialized by the constructor and made read-only .

This will impose a requirement on the part of the class and its users, which will make the changes more complicated , but you cannot have a cake and eat it .

One possible compromise is to create an abstract base class with protected constructors , while other implementations can infer and define other constructors and change behavior.

+2
source

As you summarize, constructors are the easiest way to ensure that only valid objects are created.

However, using the constructor, I am curious how to maintain the code, since it is modified to support this requirement without breaking existing code in the code base.

I'm not quite sure what you are talking about: the risk that you will later realize that you need additional (or less) parameters to create valid objects? IMHO , which should be mitigated by carefully developing, prototyping, and testing before publishing your library . Public APIs are very difficult to change. In the case of widely used, even broken and obsolete functions should be supported unlimitedly (witness Object.clone() in Java). Regardless of what you (or clients) do to build the object, if the required parameter is missing, this is an error. IMO is better off defining such compilation cases than runtime, and constructors provide the easiest and easiest way to achieve this.

Other options you may consider:

  • using the Factory Method (you still need to have all the necessary parameters inside the create method, but you may be able to use some default values ​​for missing parameters, so as not to violate the client code),
  • using Builder (similar to the above, but it gives you more flexibility when setting up design parameters or providing default values).
0
source

I do not understand design patterns; so take what I propose with salt and maybe see if my answer voted up / down before making a decision on use.

I would suggest just following these guidelines:

  • The arguments that MUST be in your constructor are any data that is not expected to be modified throughout the life cycle of your object. You must NOT provide setters for them.
  • Besides this, you can OPTION ALLOW other variable arguments that are usually set to make the code a little easier for the api user. This is what you know will usually be used immediately after calling the constructor. So, say, for example, if it has the User and nickName variables in it, you most likely want to provide an option for this in the constructor, so the consumer does not always have to call the new User (); and then user.setNickName ("name"); each time they instantiate an object.

It's also worth noting that you should do what I described, defining only one constructor, using optional arguments if you are using C #. http://msdn.microsoft.com/en-us/library/dd264739.aspx

0
source

Source: https://habr.com/ru/post/1347746/


All Articles