Static classes and Singleton differences

Source: Internet
Author: User

Recently, when looking at the Android network access class found that some people use static classes to encapsulate, some people use a single case to encapsulate, suddenly found that both can be, which is the best use of it? The answer is not absolute, both of which are possible. Like abstract classes and interfaces, in fact, in many cases both ways can achieve the goal, is to see the individual understanding (abstract class and interface differences look at my other article interface and abstract class differences). Here are some views of the individual.

Static class is like the process-oriented approach and the same, is a tool class, rarely rely on other objects or resources, but in object-oriented thinking, it is necessary to use the class, rather than the process-oriented era of direct call method, so with object-oriented encapsulation of the characteristics of the method to use static class to encapsulate the methods. The singleton is a class that can be instantiated, generally with other objects or resources have a dependency on the singleton, because he can instantiate, at the same time can release resources, and so on, because in the object-oriented thinking, the instantiation is that the characteristics of each object is not the same, need to initialize, the need to occupy resources, the release is exhausted.

On the other hand, static classes are non-thread-safe, and the singleton is generally thread-safe (why is it generally?). You can refer to the article http://www.cnblogs.com/xudong-bupt/p/3433643.html your own thinking).

Static classes and Singleton differences

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.