Class design (static class and fee static class exactly how to choose)

Source: Internet
Author: User
Because some frameworks are designed to often see
Db::get (' Mall ')->where (' AAA ');
Why can't you do this?
Db::get (' Mall '):: Where (' aaa ');

Other problems are that objects produced through a factory or a static container are generally cost-static.
For example, a log class, I have seen static also seen in the non-static design. A little choice.

Don't tell me what type of tool is best static, no new crap.

Reply content:

Because some frameworks are designed to often see
Db::get (' Mall ')->where (' AAA ');
Why can't you do this?
Db::get (' Mall '):: Where (' aaa ');

Other problems are that objects produced through a factory or a static container are generally cost-static.
For example, a log class, I have seen static also seen in the non-static design. A little choice.

Don't tell me what type of tool is best static, no new crap.

First you need to understand the difference between static and non-static.

Static classes that do not need to be instantiated for use. This can be a bit of a performance boost.

Static properties, which are shared by all instances, have only one static property, regardless of the number of instances.

Of course, non-static classes are not very different if they are used in a single case.

So how do you choose static and non-static? There are different ways to use each person's understanding of an object-oriented approach.

such as logs. If there is only one log object in a system, then using a static class is undoubtedly the best, whether it is a call method, performance, data storage is more convenient. At this time this log object, you can think of it as a physical object can also be used as an abstract object.

But if the log is refined into user behavior log, database operation log, server status log and so on, then the log is an abstract object, it needs to have entities to better use. The use of non-static at this time, the new out will be a little better. Because they all belong to the same log object, but they have different data, methods. Function.

So, my personal understanding is that if a class is a complete functional module, does not depend on other modules, and does not need to be extended to complete the function of this module, then it is best to write static.
If a class is just an abstract object, depends on other modules, or needs to be extended, or requires inheritance, and so on, then it is better to use non-static

    • In fact, is not in line with object-oriented design principles, in fact, how to write All right

    • Second, the static class is not easy to be recycled, such as the PHP script language, the life cycle of the script is generally the end of the execution of the script, the memory occupied by the script is theoretically recycled, so the use of static classes theoretically there is no memory recovery problem. But for Java and so on, static class flooding, it is not conducive to the recovery of memory

  • 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.