Machine-translated by Google. Questions are authored in English.
Top

Discussion

Which design pattern ensures a class has only one instance and provides a global point of access to it ?

  • A.Factory Pattern
  • B.Singleton Pattern
  • C.Observer Pattern
  • D.Strategy Pattern

Answer: B

The pattern is the Singleton Pattern.

A singleton makes its constructor private, keeps its single instance in a static field, and exposes it through a static method such as getInstance(). This ensures one instance and gives a global point of access to it, which is useful for things like a configuration object or a logger.

The Factory pattern creates objects without exposing the concrete class. Observer lets objects subscribe to another object's changes. Strategy swaps interchangeable algorithms at run time. None of them limits a class to one instance.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.