Package conditions (encapsulate Conditional)

Source: Internet
Author: User

encapsulation is the abstraction of related methods or properties into an object. The meaning of encapsulation:
    1. External hidden internal implementation, the interface is unchanged, the internal implementation of free modification.
    2. Only the data and methods that are needed are returned.
    3. Provides a way to prevent data from being modified.
    4. Better code reuse.
In order to improve the readability and maintainability of the code, we can encapsulate the conditions when the code contains a lot of conditional judgments. There are two kinds of encapsulation methods: first, no parameter condition judgment, encapsulation is the pre-reconstruction code of the property.
 Public classRemotecontrol {Private string[] Functions {Get;Set; } Private stringName {Get;Set; } Private intCreatedyear {Get;Set; }  Public stringPerformcoolfunction (stringbuttonpressed) {            //determine if we are controlling some extra function//That requires special conditions            if(Functions.length >1&& Name = ="RCA"&& createdyear > DateTime.Now.Year-2)                return "dosomething"; }    }

Post-Refactoring Code

 Public classRemotecontrol {Private string[] Functions {Get;Set; } Private stringName {Get;Set; } Private intCreatedyear {Get;Set; } Private BOOLhasextrafunctions {Get{returnFunctions.length >1&& Name = ="RCA"&& createdyear > DateTime.Now.Year-2; } }         Public stringPerformcoolfunction (stringbuttonpressed) {            //determine if we are controlling some extra function//That requires special conditions            if(hasextrafunctions)return "dosomething"; }    }
Second, the parameters of the conditional judgment, encapsulation as a method of reconstruction before the code
  Public classRemotecontrol {Private string[] Functions {Get;Set; } Private intCreatedyear {Get;Set; }  Public stringPerformcoolfunction (stringbuttonpressed) {            //determine if we are controlling some extra function//That requires special conditions            if(Functions.length >1&& buttonpressed=="RCA"&& createdyear > DateTime.Now.Year-2)                return "dosomething"; }    }

Post-Refactoring Code

  Public classRemotecontrol {Private string[] Functions {Get;Set; } Private stringName {Get;Set; } Private intCreatedyear {Get;Set; } Private BOOLHasextrafunctions (stringbuttonpressed) {Get{returnFunctions.length >1&& buttonpressed=="RCA"&& createdyear > DateTime.Now.Year-2; } }         Public stringPerformcoolfunction (stringbuttonpressed) {            //determine if we are controlling some extra function//That requires special conditions            if(Hasextrafunctions (buttonpressed))return "dosomething"; }    }

Package conditions (encapsulate Conditional)

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.