English 中文(简体)
PHP OOP设计——在采用通用接口时将参数限制在特定儿童类别中
原标题:PHP OOP design - limiting parameters to specific child classes while implementing generic interfaces

我常常实施PHP项目,以便从网页上删除等级数据,并将这些数据留给行(基本上,数据结构——考虑删除有数据但无法以结构方式提供数据的政府网站)。 每次我都试图提出一个开放式组织的设计,使我能够实现以下目标:

  • Easily replace current HTML parsing scripts with new ones, in case the original web page changes
  • Allow easy extensions of the data scraped and saved, as these projects are also meant for others to take and build on. My aim is to collect the "base" data, while others might decide to include something extra, change the way it is saved and etc.

So far I am yet to find the solution, but the closest I got it something like this:

我界定了一个数据集装箱的抽象类别,以履行共同的树木交易功能:

abstract class DataContainer {

  protected $parent = NULL;
  protected $children = NULL;   

  public function getParent() {
    return $this->parent;
  }

  public function getChildren() {
    return $this->children;
  }             
}

然后,我有实际的数据集装箱。 简言之,我正在将参加议会会议的数据按“开会时的具体问题”分列。 页: 1

每届会议、开庭和问答数据均从不同的URL中删除。 av弃URL含量的机制,我要说,我需要拆解器,将集装箱和DOmDocument用于实际平价。 因此,我要界定这样一个通用接口:

interface Scraper {
  public function scrapeData(DOMDocument $Dom, DataContainer $DataContainer);   
}

然后,每场会议、坐视和问题都将有自己的报废者,实施接口。 但是,我也想确保他们只能接受他们想要的集装箱。 因此,它希望:

class SessionScraper implements Scraper {
  public function scrapeData(DOMDocument $DOM, SessionContainer $DataContainer) {
  }
}

最后,我将有一个通用的<代码>Factory类别,该类别也实施。 Scraper接口,并仅向相关的报废者分发废品。 与此类似:

public function scrapeData(DOMDocument $DOM, DataContainer $DataContainer) {
  //get the scraper from configuration array
  $class = $this->config[get_class($DataContainer)];
  $craper = new $class();
  $class->scrapeData($DOM, $DataContainer);
}

这是该法典中实际要求的那一类。 同样,我可以处理对非行的储蓄问题——每个数据箱都有其DBSaver级,它将使用DBSaver接口。 也可通过<代码>Factory类别进行所有电话,该类别还将实施项目数据库平均接口。

一切都完美无缺,但问题是,实施接口的班次应准确签字接口。 E.g. 方法SessionScraper:scrapeData不能接受 only <>>>>>> 代码>SessionContainer Object, 它必须接受所有<代码>DataContainer物体。 但这并不意味着!

最后,问题:

  • Is my design wrong and I should be structuring everything in a completely different way? (how?), or:
  • My design is OK, it s just that I need to enforce types within methods with instanceof and similar checks instead of enforcing it via typehinting?

事先感谢所有建议/批评。 我完全高兴的是,如果有必要,有些人将这一守则推倒在头上!

最佳回答

Container springs into the eye. This name is very generic, you might need something more dynamic. I think you have Data and you classify it, so it has a type.

因此,你将“exact接口”硬编码为背式接口,你应积极解决这一问题。

If now each Container would have a type, the Scraper could signal/tell whether or not it is applicable for the type of Container.

报废的具体形式实际上为strategy。 页: 1

You just only need to add some logic/contract between Container and Scraper so that they can talk to each other. This contract you can put inside the interface of both.

如果你想把<条码>处理多个<条码>类型<>的代码/代码>,这也使你能够有一个<条码>。

您的<代码> 载有,也请查阅SPL,请您实施一些接口,使您有可供查阅的器具(和回收器)。 这可能是你再次提到的通用结构,而SPL可提高您的<密码>的可用性。

你们不需要在欧佩组织中硬化一切,你可以保持活力,特别是在平安方案,你通常会暂时解决问题。

This will also allow you to easier replace Scrapers with a new version. As Scrapers now would have a type by definition (as suggested above), you can resolve at runtime which concrete class should do the scraping, e.g. dynamically loading them from a .php file in a nice file-system structure.

仅是我的两点。

问题回答

暂无回答




相关问题
Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

JSON with classes?

Is there a standardized way to store classes in JSON, and then converting them back into classes again from a string? For example, I might have an array of objects of type Questions. I d like to ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Passing another class amongst instances

I was wondering what is the best practice re. passing (another class) amongst two instances of the same class (lets call this Primary ). So, essentially in the constructor for the first, i can ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签