Why is the strategy design pattern and the adapter related to each other? It seams to me that the adapter manipulates the result of some method to fullfill the input needs of anotherone. Wheras the strategy precibes behaviour.
Adapter pattern is used after the application components are designed so that we can use them without modifying the source code. This is in contrast to the Bridge pattern, which is used before the components are designed.
The difference simply lies in that they solve different problems: The State pattern deals with what (state or type) an object is (in) -- it encapsulates state-dependent behavior, whereas. the Strategy pattern deals with how an object performs a certain task -- it encapsulates an algorithm.
Adapter and Facade are both wrappers; but they are different kinds of wrappers. The intent of Facade is to produce a simpler interface, and the intent of Adapter is to design to an existing interface.
Adapter changes the interface of an existing object, while Decorator enhances an object without changing its interface.
Adapter patterns basically allows classes to work together that on their own could not due to incompatible interfaces. Adapter converts the interface of one class into something that may be used by another class.
Similar to how if you travel abroad you need to carry a power adapter to be able to use the wall sockets.
Strategy pattern, on the other hand takes a group of algorithms, and makes them interchangeable (by extending from a common interface). So that whatever class that is going to use the strategy can easily interchange it with another strategy from the group.
In other words, Adapter does not add behavior in any way, it just modifies the existing interface to allow some other class to access the existing functionality.
Strategy pattern on the other hand encapsulates different behavior, and allows them to be switched at run time.
An example is always good, lets take a look at a use case for an adapter. Imagine you have are using some package you cannot modify, it contains the following files:
class DatabaseManager
{
private Connection $connection;
public function connect(Connection $connection)
{
$this->connection = $connection->establish();
}
}
class MysqlConnection implements Connection
{
public function establish(){
// connect to mysql...
}
}
Inside your repo you have the following code:
class YourService
{
public function connectDB()
{
$db = new DatabaseManager();
$db->connect(new MysqlConnection());
}
}
It all works fine, however then you get the requirement to change the connection to SqlLiteConnection and add have to import a different package for using it. The imported file looks like this:
class SqlLiteConnection
{
public function prepareConnection(){
// first prepare the connection...
return $preparedConnection;
}
public function executeConnection($preparedConnection){
// then connect ...
$preparedConnection->execute();
}
}
The interface (method names) of SqlLiteConnection is different to MysqlConnection, so you are having trouble using it:
class YourService
{
public function connectDB()
{
$db = new DatabaseManager();
$db->connect(new SqlLiteConnection()); // error! method `establish` does not exist in SqlLiteConnection.
}
}
You are not able to modify the SqlLiteConnection package, so to get it to work you create an adapter:
class SqlLiteAdapter implements Connection
{
public function establish($sqlConnection)
{
$preparedConnection = $sqlConnection->prepareConnection();
$sqlConnection->executeConnection(preparedConnection);
}
}
Now it works!
class YourService
{
public function connectDB()
{
$db = new DatabaseManager();
$db->connect(new SqlLiteAdapter(new SqlLiteConnection())); //works!
}
}
As you can see the adapter was just used as an alias to map establish
to the methods prepareConnection
and executeConnection
. This is the purpose of an adapter, it does not add functionality but just adapts
the interface to your needs. Similar to a travel adapter that allows you to use a US plug in a EU socket.
The strategy pattern on the other hand would have a similar implementation as above but you would not simply map some functions in the Adapter but would add your unique logic for each of your Strategies.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With