How to allow a user of the class X to choose an implementation of the interface the class X is using?-Collection of common programming errors

Your solution (the public IPriorityQueue property) is commonly seen when using “Dependency Injection” (or “Inversion of Control”) frameworks and is known as “setter injection”.

Another option would be “Constructor Injection”, where the interface’s implementation is provided as a constructor parameter.

There are many of these frameworks to choose from: .Net has Unity, Spring.Net, StructureMap Castle Windsor and others. Java has Spring and probably others as well.

These frameworks allow the user to specify the implementation he wants to use in an XML configuration file, or by providing a configuration at runtime to the Dependency Injection container.

When objects instances are created using such a framework, it takes care of creating/retrieving the dependency and providing them to the dependent object.