私は見通しとオファーを含む簡単なプロジェクトに取り組んでいます。このプロジェクトは、Prospectオブジェクトを使用してリストの電子メールアドレスを管理し、オファーオブジェクトを使用してキャンペーンを管理するサードパーティのメーリングリストプロバイダと統合されます。メーリングリストのマネージャークラスをより疎結合にするにはどうすればよいですか?
私の懸念事項の1つは、メーリングリストのプロバイダ(MailChimpなど)がサービスの提供を停止するか、今後利用規約を変更することがあるということです。自分のソフトウェアをプロバイダに依存させるのではなく、別のメーリングリストプロバイダを使用する別のクラスを使用して再実装できる汎用インタフェースに依存させたいと考えています。そうすれば、そのようなことが起こったら、私は単に新しいクラスを作成し、古いクラスの代わりにインスタンス化するだけです。
これは実装が簡単なようです。以下に含まれる抽象クラスは、ProspectオブジェクトまたはOfferオブジェクトのいずれかを取る抽象メソッドを定義し、それらにジェネリックメーリングリスト関連の関数を行い、必要に応じてtrue/falseまたは整数値を返します。このインターフェイスは、私のアプリケーションのニーズを十分に満たす必要があります。
<?php
/**
* MailingList file.
*
* Contains the class definition for the abstract class Monty_MailingList.
* @author Lewis Bassett <lewis[email protected]>
* @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 <[email protected]>
* @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);
}
ここでジレンマがあります。
将来、私のアプリケーションとメーリングリストプロバイダとの間で渡されるデータは変更される可能性がありますが、どこでもインターフェイスを変更し続ける必要はありません。オブジェクトをメソッドに渡すことで、どこにでもインターフェイスを変更することなく、新しいプロパティを使用できるようにメソッドを変更できます。これは非常に柔軟な解決策のようです。
しかし
私は必ずしもプロスペクトまたはオファークラスを使用しない場合があります他のプロジェクトでこのクラスを使用したいと思います。上記のインタフェースは、クラスの観点から、クラスが渡されているオブジェクトに依存しているという点で、緊密に結合されているようです。
オブジェクトにメソッドを渡す柔軟性をどのように保つかもしれないのですが、他のプロジェクトでも簡単に再利用できるクラスはありますか?
これまでにお読みいただきありがとうございます。私は常に自分のスキルを向上させるために探しています。私はこれをより良くする方法についてのあなたの洞察に非常に感謝しています。
私はPersonクラスとEmailMessageクラスを作成し、それらをPersonとEmailMessageを使用していたメソッドに引数として渡します。そこで、自分自身をパラメータとして渡したMonty_Prospectクラス内で、新しいPersonクラスをインスタンス化し、その代わりに渡しますか? –