我正在做一个涉及Prospects和Offers的简单项目。该项目将与第三方邮件列表提供商集成,该提供商将使用 Prospect 对象来管理列表中的电子邮件地址,并使用 Offer 对象来管理活动。
我担心的一个问题是,任何邮件列表提供商(例如 MailChimp)都可能决定停止提供他们的服务,或者在未来更改条款。我不想让我的软件依赖于提供者,而是想让它依赖于通用接口,该接口可以使用使用不同邮件列表提供者的不同类重新实现。这样,如果确实发生了这样的事情,我只需编写一个新类并实例化旧类即可。
这似乎很容易实现。我的抽象类,包括在下面,定义了抽象方法,它们采用 Prospect 或 Offer 对象并对它们执行通用邮件列表相关函数,在需要时返回 true/false 或整数值。这个接口应该可以很好地满足我的应用程序的需求。
<?php
/**
* MailingList file.
*
* Contains the class definition for the abstract class Monty_MailingList.
* @author Lewis Bassett <lewis.bassett@bassettprovidentia.com>
* @version 0.1
* @package Monty
*/
/**
* Represents the interface for all MailingList classes.
*
* Adhereing to this interface means that if a MailingList provider
* (e.g., MailChimp) stops a service, a new class can extend this interface and
* be replace the obsolete class with no need to modify any of the client code.
*
* @author Lewis Bassett <lewis.bassett@bassettprovidentia.com>
* @version 0.1
* @package Monty
* @copyright Copyright (c) 2011, Bassett Providentia
*/
abstract class Monty_MailingList
{
/**
* Adds the passed prospect to the mailing list, or returns false if the
* prospect already exists. Throws an error if the prospect could not be
* added for any reason (other than it already existing).
*
* @param Monty_Prospect $prospect The prospect object to be added to the
* mailing list.
* @return bool Whether or not the prospect was added.
*/
abstract public function addProspect(Monty_Prospect $prospect);
/**
* Updates the properties stored on the mailing list of the passed prospect,
* or returns false if no data was updated. If the prospect is not found, a
* they are added to the list. Throws an error if the prospect could not be
* added or updated for any readon.
*
* @param Monty_Prospect $prospect The prospect object whose mailing list
* data is to be updated.
* @return bool Whether or not the prospect was updated.
*/
abstract public function updateProspect(Monty_Prospect $prospect);
/**
* Returns true if the passed prospect object could be found on the mailing
* list.
*
* @param Monty_Prospect $prospect The prospect object to be searched for.
* @return bool Whether or not the prospect was found.
*/
abstract public function findProspect(Monty_Prospect $prospect);
/**
* Deletes the passed prospect from the mailing list, or returns false if
* the passed object is not found on the mailing list.
*
* @param Monty_Prospect $prospect The prospect to be deleted.
* @return bool Whether or not the prospect was deleted.
*/
abstract public function deleteProspect(Monty_Prospect $prospect);
/**
* Creates a campaign for the passed offer object, or returns false if the
* campaign already exists. Throws an error if the campaign could not be
* created for any reason (other than it already existing).
*
* @param Monty_Offer $offer The offer to be created.
* @return bool Whether or not the offer was created.
*/
abstract public function createOffer(Monty_Offer $offer);
/**
* Sends the campaign for the passed offer object, or returns false if the
* campaign could not be sent for a reasonable reason (run out of credit or
* something). If the campaign does not yet exist, it is created. Throws an
* error if the campaign could not be created, or an was not sent for an
* unknown reason.
*
* @param Monty_Offer $offer The offer to be sent.
* @return bool Whether or not the offer was sent.
*/
abstract public function sendOffer(Monty_Offer $offer);
/**
* Returns true if a campaign for the passed offer object could be found on
* the mailing list.
*
* @param Monty_Offer $offer The offer to be searched for,
* @return bool Whether or not the offer was found.
*/
abstract public function findOffer(Monty_Offer $offer);
/**
* Returns the ammount of opens registered for the passed offer. Throws an
* error if a campaign is not found for the passed offer.
*
* @param Monty_Offer $offer The offer in question.
* @return int The ammount of registered opens for that offer.
*/
abstract public function getOfferOpens(Monty_Offer $offer);
/**
* Returns the ammount of clicks registered for the passed offer. Throws an
* error if a campaign is not found for the passed offer.
*
* @param Monty_Offer $offer The offer in question.
* @return int The ammount of registered clicks for that offer.
*/
abstract public function getOfferClicks(Monty_Offer $offer);
/**
* Returns the ammount of bounces registered for the passed offer. Throws an
* error if a campaign is not found for the passed offer.
*
* @param Monty_Offer $offer The offer in question.
* @return int The ammount of registered bounces for that offer.
*/
abstract public function getOfferBounces(Monty_Offer $offer);
/**
* Returns the ammount of unsubscribes registered for the passed offer.
* Throws an error if a campaign is not found for the passed offer.
*
* @param Monty_Offer $offer The offer in question.
* @return int The ammount of registered unsubscribes for that offer.
*/
abstract public function getOfferUnsubscribes(Monty_Offer $offer);
}
困境来了。
将来,在我的应用程序和邮件列表提供程序之间传递的数据可能会发生变化,但是,我不想在任何地方都必须不断更改接口。通过将对象传递给方法,我可以修改方法以在新属性可用时使用它们,而无需在任何地方更改接口。这似乎是一个非常灵活的解决方案。
但
我想在其他项目中使用这个类,这可能不一定使用 Prospect 或 Offer 类。从类的角度来看,上面的接口似乎耦合得太紧了,因为类依赖于传递给它的对象。
有没有人对我如何保持将对象传递给方法的灵活性有任何建议,但有一个我可以轻松地重用于其他项目的类?
非常感谢你读到这里!我一直在寻求提高我的技能,我将非常感谢您对我如何才能做得更好的洞察力。