Design Patterns – Creating Objects Hidden from Client

architectural-patternsArchitecturedesigndesign-patternsuml

I am having difficulty understanding which design pattern would be best for the following scenario. An application uses a database. That database can either be the production version (SQL) or a test version (Test). Here's my current design:

enter image description here

The problem with this design is that application has to create Test, which it shouldn't be doing (since it's purely for testing). However, Application must somehow get reference to the database it's going to use, either SQL or Test.

What design pattern could I implement to fool application into creating the Test version of the database? How would this solution look in UML?

I have already done enough research to narrow my choices to Abstract Factory, Builder, or Factory Method (or the combination of any)

Best Answer

When you want a part of your program to instantiate a certain subclass, but hide the instantiation behavior from other client code (your Application class), you should probably go with the Factory Method DP.

That relates to the instantiation part. Regarding the actual behavioral differences between the subclasses, the Strategy DP may be a good solution, as stated by @Paul.