这里就是我想在我的程序来实现:这是抽象工厂模式的合法使用吗?
- 程序应该打开一个压缩文件,其中包含许多数据文件
- 数据文件的格式可以zip文件之间的差异(如CSV ,制表符分隔的,甚至可能是某种二进制文件这就需要解码)
- 然而,一个zip文件中的所有数据文件是相同类型
我一直在阅读“设计模式”的伽马等人,并一直在看抽象工厂模式试图解决这个问题。
理想情况下,我想为Zip文件创建一个类,它可以读取其中任何类型的数据文件。我想我会有两个类 - FileTypeA和FileTypeB,它可以处理不同格式的数据(尽管未来可能会有更多)。我想告诉我的ZipFile类在读取数据时使用哪种类型的文件。
到目前为止,这是我想出了:
<?php
/**
* An abstract factory used for creating data files of any type
*/
abstract class DataFileFactory{
abstract function createFile($id);
}
/**
* A factory for creating and setting up a data file of type 'A'
*/
class FileAFactory extends DataFileFactory{
public function createFile($id){
$file = new FileA();
$file->setSampleId($id);
return $file;
}
}
/**
* A factory for creating and setting up a data file of type 'B'
*/
class FileBFactory extends DataFileFactory{
public function createFile($id){
$file = new FileB();
$file->setSampleId($id);
return $file;
}
}
/**
* An abstract class which defines some functionality of a data file
*/
abstract class DataFile{
abstract function readData();
abstract function setSampleId();
}
/**
* Concrete class that processes a data file of type 'A'
*/
class FileA extends DataFile{
public function readData(){
echo "Reading data from a file A<br/>";
}
public function setSampleId(){
echo "Setting sample id of a file A<br/>";
}
}
/**
* Concrete class that processes a data file of type 'B'
*/
class FileB extends DataFile{
public function readData(){
echo "Reading data from a file B<br/>";
}
public function setSampleId(){
echo "Setting sample id of a file B<br/>";
}
}
/**
* Concrete class that reads a zip file and reads each file within the zip
*/
class ZipFile{
private $files = array("file1.txt","file2.txt","file3.txt","file4.txt");//this would be an array read from the zip file
private $sampleId = 1;//this would be derived from some other function
/**
* Read all the files in a zip archive.
* $factory can be an instance of any class that extends DataFileFactory, and is used for creating each file
*/
public function readFiles(DataFileFactory $factory){
foreach($this->files as $fileName){//loop through each file in the zip
$file = $factory->createFile($this->sampleId);//use the factory to create the desired file
$file->readData();//now read the data from the file!
echo "object created of type: ".get_class($file)."<hr/>";
}
}
}
/***********************************************************************************************
* IMPLEMENTATION
***********************************************************************************************/
$zip = new ZipFile();//create a new zip file
$factory = new FileAFactory();//instantiate a new factory, depending on which type of file you want to create
$zip->readFiles($factory);//read the files, passing the correct factory object to it
谁能告诉我: (A)这是否是实现什么我找的一个很好的方式,或者是有一些更简单的方法呢? (B)这实际上是抽象工厂模式,还是我完全误解了?
在此先感谢!
刚刚在这里切线,但你有没有考虑使用特征来处理你的一些“复制和粘贴”方法?在你的例子中:readData()和setSampleId()。 –
感谢您的建议 - 但是,我使用的是PHP v5.3.5,并且只有v5.4.0的特性!任何其他建议/答案将受到欢迎! – user1578653
是的,我还有另一个建议...改为5.4^_ ^。但是,如果我有一些有用的补充,我会:) –