179k views
3 votes
Suppose you are developing a data visualization class called DataViz. Currently, DataViz directly calls some functions in a statistics library/class called SuperStats. You recently heard about another statistics library/class called UberStats, which possibly has better performance than SuperStats.

Redesign DataViz so that it can be configured at runtime to use either SuperStats or UberStats.
Describe the key aspects of your revised design:

User Erosebe
by
8.1k points

1 Answer

5 votes

Final answer:

To make DataViz configurable at runtime to use SuperStats or UberStats, implement the abstract factory design pattern. Create an abstract class called StatsFactory with two concrete subclasses. Set the desired factory at runtime in DataViz. This design allows for easy switching between the two libraries.

Step-by-step explanation:

To redesign the DataViz class to be configurable at runtime to use either SuperStats or UberStats, you can implement the abstract factory design pattern. This pattern allows you to create families of related objects without specifying their concrete classes.

In this case, you can create an abstract class called StatsFactory, which will have two concrete subclasses: SuperStatsFactory and UberStatsFactory. Each factory will provide implementations for the functions in their respective libraries/classes.

DataViz will have a member variable of type StatsFactory, which will be set to either SuperStatsFactory or UberStatsFactory at runtime based on the desired configuration.

The functions in DataViz can then call the functions from the StatsFactory, without needing to know which specific library/class is being used. This design allows for easy switching between SuperStats and UberStats without modifying the code in DataViz.

User Rob Haupt
by
8.5k points