How to write beautiful code (4)

Source: Internet
Author: User

(The idea of this article is basically from the classic book restructuring)

Http://www.cnblogs.com/ceys/archive/2012/03/09/2388356.html previous

 

3. Simplified conditional expressions and function calls

 

Conditional statements should be as simple as possible, and independent functions should be used to represent them respectively.

If the conditional expression selects different behavior based on the object type,Place each branch of the conditional expression into the override function in a subclass, and declare the original function as an abstract function.

Separate query and modify Functions. In a multi-threaded system, you usually need to complete the check before assigning values. At this time, the query is still modified separately. A new function is created, and independent query and modification functions are called and declared as synchronized. If they are not declared as synchronized, they should be visible at the package level or private level.

Replace parameters with functions and objects whenever possible. For example, common value range parameters can be replaced by objects:

 
Class daterange {daterange (date start, date end) {_ start = start; _ end = end;} date getstart () {return _ start;} date getend () {return _ end;} private final date _ start; private final date _ end ;}

If you do not just perform simple construction operations when creating an object, replace the constructor with a factory function.In this way, the type code can be replaced by a factory function in the process of deriving subclass. For example:

 
Static employee create (string name) {try {return (employee) class. forname (name ). newinstance ();} catch (exception e) {Throw new illegalargumentexception ("unable to instantiate" + name );}}

When the caller can perform a pre-check, do not use exceptions, but make condition judgments.

 

Iv. Handling general relationships

 

When two subclasses have the same fields, functions that produce identical results, or constructors with almost identical ontology, move them to the superclass.

When several customers use the same subset of the class interface, or the interfaces of the two classes are partially the same,Extract the same subset to an independent interface. Classes interact with each other in several ways: All responsibility zones of a certain type are used; only a specific subset of a class responsibility zone is used; cooperation with all classes that assist in processing certain requests is required. In the latter two cases, separation of actually used responsibilities is often meaningful. You can use interfaces in Java to meet the above requirements. For example, the timesheet class indicates the schedule for an employee to work for the customer, from which the customer's payable fees can be calculated. to calculate the fee, timesheet needs to know whether the employee level has special skills:

Double Charge (employee EMP, int days) {int base-EMP. getrate () * daysif (EMP. hasspecialskill () return base * 1.05; else return base ;}

 

We can define an interface for employee skill level:

 
Interface billable {public int getrate (): Public Boolean hasspecialskill ();} class employee implements billable... double Charge (billable EMP, int days) {int base = EMP. getrate () * days; if (EMP. hasspecialskill () return base * 1.05; else return base ;}

If some sub-classes, some of the corresponding functions perform similar operations in the same order, put these operations into independent functions respectively, and keep them all with the same signature, so the original functions become the same. Then, move the original function to the superclass.

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.