How can you prevent a class from being instatiated, means, how to stop other classes/callers creating a new instace of an existing class?
There are two methods which are straight away
1) Declaring a class static, there by making all the members and properties static
2) Use a private constructor.
[OUT OF SYLLABUS]
The 2nd point is also used to follow Singleton Pattern (more on the pattern), but this is not to prevent creating any instances but to restrict only one instance of that class is created.
There are two methods which are straight away
1) Declaring a class static, there by making all the members and properties static
2) Use a private constructor.
[OUT OF SYLLABUS]
The 2nd point is also used to follow Singleton Pattern (more on the pattern), but this is not to prevent creating any instances but to restrict only one instance of that class is created.
No comments:
Post a Comment