从另一种不同种类的类中创建一个实例。又一个“这是一种模式,如果是,名称是什么”?
快速示例:您正在使用ORM并将电子邮件队列存储在数据库中。然后在某个时候,您必须使用第三方库发送批量电子邮件。
你的目标(为简单起见PHP这里无关紧要,公共属性):
class Mail
{
/**
* @var string
*/
public $senderText;
/**
* @var My\App\Entity\User
*/
public $user;
/**
* @var \Doctrine\ORM\ArrayCollection
*/
public $attachments;
}
...而你的第三者 “邮件” 的目标是完全不同的。例如,您可能需要执行以下操作:
$mail = /* hydrated */;
$user = $mail->getUser();
$mailer = new ThirdPartyMailer();
// Fill message properties
$message = $mailer->createMessage();
$fullName = sprintf('%s %s', $user->getFirst(), $user->getLast());
$message->addFrom(array($fullName => $user->getEmail()))
// Create and add attachments
foreach($mail->getAttachments() as $attachment)
{
$message->attach($mailer->createAttachment($attachment->getFullPath()));
}
这是一种模式吗?类似目标转换器,像一个类,负责创建/从另一个转换一个实例...
这不是一个真正的代码问题;这是一个术语问题,应该在[程序员](http://programmers.stackexchange.com)上。投票迁移。 – 2012-08-09 02:04:54
@KenWhite,好的,对不起。我也在投票。编辑。不能投票迁移... – gremo 2012-08-09 02:06:32