Consider parameter lidating method parameters

Source: Internet
Author: User
Consider parameter lidating method parameters

Parameters of the complete Method

Sometimes it's a good idea to encapsulate multiple parameters to a method into a single object. This may enhance readability and simplify calling code. Consider a method signature like this:

Sometimes it is a good idea to encapsulate multiple parameters in a method. This improves readability and simplifies the call process. Consider a method declaration like this:

Public VoidSetoptions (font F,IntLinespacing,IntLinesperpage,
IntTabsize );

We cocould simplify this signature by rolling the multiple parameters into a single object, like this:

We can simplify this Declaration to wrap these parameters into a single object, like this:

Public VoidSetoptions (options );

The main advantage is flexibility. we don't need to break signatures to add further parameters: we can add additional properties to the parameter object. this means that we don't have to break code in existing callers that aren't interested in the added parameters.

The main advantage is scalability. We do not need to destroy the Declaration to add more parameters: we add parameter attributes to parameter objects. This means we have to destroyCodeAdd parameters for callers.

As Java, unlike C ++, doesn' t offer default parameter values, this can be a good way to enable clients to simplify CILS. let's suppose that all (or most) or the parameters have default values. in C ++ we cocould code the default values in the method signature, enabling callers to omit some of them, like this:

Just as JAVA does not want C ++ and does not provide default parameter values, this is a good way to simplify the client. Let's assume that all (or most) parameters have default values. In C ++, We can encode the default value in the method declaration so that the caller can omit some of them, as shown in the following code:

VoidSomeclass: setoptions (font F,IntLinespacing = 1,IntLinesperpage = 25,
IntTabsize = 4 );

This isn' t possible in Java, But weCanPopulate the object with default values, allowing subclasses to use syntax like this:

This is not possible in Java, but we can use the default value to construct this object, allowing sub-classes to use the Syntax:

Options o =NewOptions ();
O. setlinespacing (2 );
Retriable. setoptions (O );

Here,OptionsObject's constructor sets all fields to default values, so we need modify only to those that vary from the default. if necessary, we can even make the parameter object an interface, to allow more flexible implementation.

Here, the constructor of this options object is set to the default value for all fields, so we need to modify these values different from the default values. If necessary, we can even make the parameter object an interface, allowing more flexibility.

This approach works fig well with constructors. it's indicated when a class has extends constructors, and subclasses may face excessive work just preserving superclass constructor permutations. instead, subclasses can use a subclass of the superclass Constructor's parameter object.

This method works very well in constructor. It implies that when a class has many constructor methods, the subclass can face excessive work and only keep the arrangement of the super class constructor. Instead, the subclass can use a parent class to construct a parameter object.

The command Design Pattern uses this approach: a command is required tively a random lidated set of parameters, which are much easier to work with together than individually.

Command mode: a command effectively merges the parameter set, which is easier to work together than an individual.

The disadvantage of parameter specified lidation is the potential creation of parameter objects, which increases memory usage and the need for garbage collection. objects consume heap space; primitives don't. whether this matters depends on how often the method will be called.

The disadvantage of parameter merging is the potential creation of many objects, increasing the memory utilization and requiring more garbage collection. The object uses heap memory; the original type does not. This problem depends on whether this method is often called.

Note

Note:

Using lidating method parameters in a single object can occasionally cause performance degradation in J2EE applications if the method call is potentially remote (a call on the remote interface of an EJB ), as specified aling and unmarshaling several primitive parameters will always be faster than specified aling and unmarshaling an object. however, this isn't a concern unless the method is invoked particle usually often (which might indicate poor application partitioning-we don't want to make frequent remote CILS if we can avoid it ).

Consolidating method parameters can occasionally cause performance degradation on a single object. In J2EE applications, if the method call is remote (on the EJB remote interface ), as the original parameters for sending and unblocking, it will always be faster than sending and unblocking objects. However, this is not a problem unless the method does not call a lot (it may indicate a poor application partition-we should not do frequent remote calls if we avoid it ).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.