I have two servers that I will be connecting to from one client. For each server, I will be doing an ftp "put" and a "rm".
Should I build one facade, and have an interface like this:
void putFileOnServer1(String file)
void putFileOnServ开发者_运维百科er2(String file)
void removeFromServer1(String file)
void removeFromServer2(String file)
And, should the facade handle all the establishing of the connections and disconnecting? If so, should it use a factory to do so?
You have two methods, PutFileOnServer and RemoveFromServer. Which server you are putting or removing from should be part of the abstraction.
Do the ftp servers have different interfaces? Or do they all understand the same set of commands you want to utilize already?
If so, then simply create one
FtpServer
class that accepts connection info. And create aFtpClient
class that accepts multiple servers, that you can select by some key for instance. (At least this, to some extent, would probably be something I would do).class FtpClient { public function addServer( FtpServer $server, $key ); public function selectServer( $key ); public function putFileOnServer( $file ); public function removeFileFromServer( $file ); }
If not, and you have a class for each individual implementation already, that differ by their interfaces, for instance like:
class FtpServerFoo { public function selectFile( $file ); public function removeSelectedFile(); } class FtpServerBar { public function removeFile( $file ); }
... you should look into the Adapter Pattern:
abstract class FtpServer { abstract public function putFile( $file ); abstract public function removeFile( $file ); } class FtpServerAdapterFoo extends FtpServer { public function __construct( FtpServerFoo $server ) { } public function removeFile( $file ) { $this->server->selectFile( $file ); $this->server->removeSelectedFile(); } } class FtpServerAdapterBar extends FtpServer { public function __construct( FtpServerBar $server ) { } public function removeFile( $file ) { $this->server->removeFile( $file ); } } $cilent = new FtpClient(); $client->addServer( new FtpServerAdapterFoo( new FtpServerFoo() ), 0 ); $client->addServer( new FtpServerAdapterBar( new FtpServerBar() ), 1 ); $client->selectServer( 0 ); $client->putFileOnServer( $file ); $client->selectServer( 1 ); $client->removeFileFromServer( $someOtherfile );
If you don't have individual classes for differing FTP servers yet, then you can just implement the same interface (or inherit an abstract class) for each ftp server implementation and use the same type of FtpClient class as the one above again.
Not really a facade pattern involved here though.
You typically use facades to decrease complexity among multiple object types. In this case, though, it appears you want to use only one functional "type", a "FTPServer". For the most part, then, you should just have two instances of this type, and this type will have a "put" and "remove" method.
When you add needless function points, you actually increase the complexity of maintenance. For instance, if you need to add a new parameter to your functions (maybe access restrictions or whatever), you not only have to change for each place of use, but now have to add in each facade method. Abstraction should reduce this type of coupling, not increase it.
精彩评论