QueryFactory

This change adds a new QueryFactory interface and unifies QueryImpl construction.