Grant Crofton
Grant Crofton

Reputation: 9099

Name of pattern where you create an instance to call static methods

Usually done to make legacy code testable. For example, there might be a load of static calls like

int importantNumber = DataAccess.LoadValue();

and I create a class which can be instantiated to call these, which is normally behind an interface, like

public int LoadValue(){
    return DataAccess.LoadValue();
}

Then I can use DI or whatever and replace the original call with

int importantNumber = _dataAccessInstance.LoadValue();

Is there a name for this pattern? I was thinking 'Adapter', but it seems more specific than that.

Upvotes: 2

Views: 181

Answers (2)

Tim Barrass
Tim Barrass

Reputation: 4939

Proxy.

It looks like one of the wrappers -- proxy, adapter or decorator. Decorator doesn't really fit as you're not adding any value; adapter fits if you're mapping from one interface to another; I think proxy is the answer, as you're using it to mediate access to the toolkit.

Could be facade if you're simplifying access to a subset of tools from a very large library of code.

Upvotes: 3

Tom Carter
Tom Carter

Reputation: 2976

It's a proxy.

An adapter changes the interface of a class to make it easier to use with other clases

A facade is the creation of a single interface to make interacting with the interfaces of several objects easier. It changes the level of abstraction.

A decorator does not change the interface but adds extra functionality

A proxy acts as a surrogate to another object with the same interface and same end functionality

Upvotes: 3

Related Questions