php的抽象工厂模式使用场景,php – 这是抽象工厂模式的合法用途吗?

这是我在我的程序中尝试实现的内容:

>程序应该打开一个zip文件,其中包含许多数据文件

> zip文件的数据文件格式可能不同(例如csv,制表符分隔,甚至可能是某种需要解码的二进制文件)

>但是,在zip文件中,所有数据文件的类型都相同

我一直在阅读Gamma等人的“设计模式”,并一直在寻找抽象工厂模式来试图解决这个问题.

理想情况下,我希望Zip文件有一个类,它可以读取其中的任何类型的数据文件.我想我会有两个类 – FileTypeA和FileTypeB,它们可以处理不同格式的数据(尽管将来会有更多).我想告诉我的ZipFile类在读取数据时使用哪种类型的文件.

到目前为止,这是我提出的:

/**

* 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
";

}

public function setSampleId(){

echo "Setting sample id of a file A
";

}

}

/**

* Concrete class that processes a data file of type 'B'

*/

class FileB extends DataFile{

public function readData(){

echo "Reading data from a file B
";

}

public function setSampleId(){

echo "Setting sample id of a file B
";

}

}

/**

* 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)."


";

}

}

}

/***********************************************************************************************

* 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)这实际上是抽象工厂模式,还是我完全被误解了?

提前致谢!

解决方法:

这是一个很好的实现,但如果你使用接口可以稍微调整一下.

一个带有所有虚方法的abtract类它只是一个接口所以不要使用抽象类,使用interfaces.

interface IDataFileFactory{

public function createFile($id);

}

class FileAFactory implements IDataFileFactory

class FileBFactory implements IDataFileFactory

如果您在FileAFactory和FileBFactory方法中找到重复的代码,那么是时候重构您的类并创建继承.

interface IDataFileFactory{

public function createFile($id);

}

abstract class BaseFileFactory implements IDataFileFactory {

//some methods implementation with common features to avoid repeating code

//some abstract methods to be implemented for A and B FileFactories

//absolute abstract base class has no sense because in php you can use interfaces.

//...

}

class FileAFactory extends BaseFileFactory

class FileBFactory extends BaseFileFactory

然后使用throug接口:

public function readFiles(IDataFileFactory $factory){

//create a file using factory

return IDataFile; //return Interface implemented by all DataFile types.

}

您可以使用DataFile基类执行相同的操作,依此类推.

我还建议不要在参数中传递工厂,因为工厂不在上下文中.尽量不要将架构实现与数据和信息处理工作流混合在一起.您可以在可供其他类访问的作用域中创建容器来解析工厂.

例如,容器可以读取配置文件以在应用程序引导程序中创建具体工厂;读取某个值,由用户在用户案例的先前步骤中选择,存储在类实例中或在运行时接受参数以解析工厂.它是关于实现某种简单的依赖关系.

无论如何,这只是我的观点,可能是一个不同意见.

我希望它有所帮助.

标签:php,design-patterns

来源: https://codeday.me/bug/20190612/1227072.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值