PHP中的AbstractFactory没有方法重载

时间:2015-01-23 20:04:23

标签: php design-patterns abstract-factory

情况

我目前有4种类型的用户,我们预测未来至少会有3种用户。现在他们是:

  • 管理员(店铺管理员组)
  • 员工(店铺经理)
  • 员工(店员)
  • 客户

在不久的将来,我将不得不让两位员工同时成为客户。我也有支持中心和记者。

问题

创作。创建。创建。我并不担心访问控制,权限等...我现在的代码可以在这方面做奇迹。我的问题只是关于创造。似乎抽象工厂对我来说可能是一个,但事实是所有那些“抽象教程”教学设计模式使用书籍和汽车只是没有帮助我把它带到我的情况。要么我的设计模式错误,要么我不理解。

我的尝试

在UserFactory类中,我们可以看到问题的根源:abstract public function signUp();。这是不好的做法,甚至导致PHP 5.4+上的严格标准错误不尊重方法签名。在Java中,我会有方法重载来解决这个问题。在PHP中,方法重载的工作方式不同,不允许我以这种方式工作。

<?php

abstract class UserFactory {

    const ADMIN = 'AdminRecord';
    const MANAGER = 'ManagerRecord';
    const SALESMAN = 'SalesmanRecord';
    const CUSTOMER = 'CustomerRecord';

    public static function manufacture($type) {
        return new $type;
    }

    protected $accountController;
    protected $emailController;
    protected $toolMailer;

    function __construct() {
        $this->accountController = new AccountController();
        $this->emailController = new EmailController();
        $this->toolMailer = new ToolMailer();
    }

    abstract public function signUp();
}

这是我的第一个用例:创建一个新的管理员。

class AdminRecord extends UserFactory {

    protected $accountCompanyController;

    function __construct() {
        parent::__construct();
        $this->accountCompanyController = new AccountCompanyController();
    }

    public function signUp($name, $email, $password, $companyId, $access) {
        $accountId = $this->accountController->add($name, $password);
        $this->emailController->add($email, $accountId);
        $this->accountCompanyController->add($accountId, $companyId, $access);

        $this->toolMailer->adminWelcome($name, $email, $password);
    }

}

这里我创建了一个新的抽象类,因为我的两个用例属于同一个实体(Salesman和Managers都是具有不同访问级别的Staff)。

abstract class StaffRecord extends UserFactory {

    protected $staffController;

    function __construct() {
        parent::__construct();
        $this->staffController = new staffController();
    }

}

此处,SignUp的签名与管理员相同,后者排除了使用func_num_args()func_get_args()的权限。等等,但在Java中,您将无法使用Method Overload来解决此问题。是的,但在Java中,我可以用int $shopId替换Shop shop,用int $companyId替换Company company

class ManagerRecord extends StaffRecord {

    public function signUp($name, $email, $password, $shopId, $access) {
        $accountId = $this->accountController->add($name, $password);
        $this->emailController->add($email, $accountId);
        $this->staffController->add($accountId, $shopId, $access);
        $this->toolMailer->managerWelcome($name, $email, $password);
    }

}

此处SignUp方法与之前见过的两种情况都不同。

class SalesmanRecord extends StaffRecord {

    public function signUp($name, $email, $password, $cpf, $shopId, $access) {
        $accountId = $this->accountController->addSeller($name, $password, $cpf);
        $this->emailController->add($email, $accountId);
        $this->staffController->add($accountId, $shopId, $access);
        $this->toolMailer->salesmanWelcome($name, $email, $password);
    }

}

这里SignUp方法比以前更加不同。

class CustomerRecord extends UserFactory {

    protected $customerController;

    function __construct() {
        parent::__construct();
        $this->customerController = customerController();
    }

    public function signUp($name, $email, $password, $cpf, $phone, $birthday, $gender) {
        $accountId = $this->accountController->addCustomer($name, $password, $cpf, $phone, $birthday, $gender);
        $this->emailController->add($email, $accountId);
        $this->toolMailer->customerWelcome($name, $email, $password);
    }

}

2 个答案:

答案 0 :(得分:3)

这是我的实施:

我使用Interface来使signUp函数接受每种用户类型的不同类型的参数;

创建的界面:

namespace main;
interface UserInterface { }

您可以添加每个类需要实现的方法。现在,只使用它作为signUp()的类型提示对象;

通过在signUp(User $ user)上使用类型提示,它将解决您在注册时传递的不同类型签名的问题。它可以是用户类型admin,manager,salesman和customer。每个{User} Record都扩展并实现了抽象工厂,但实现方式不同。

我假设每个用户类型都有相应的/唯一的行为。我添加了名为:AbstractUser.php,UserAdmin.php,UserManager.php,UserSalesman.php和UserCustomer.php的额外类。每个类将包含不同类型的用户和属性,但扩展了一个抽象类用户,这对每个类(电子邮件,名称,密码)都是通用的;

AbstractUser.php - 我注意到用户的常见属性,因此我创建了一个抽象用户。共同属性(电子邮件,姓名,密码)

<?php

namespace main;

abstract class AbstractUser {
    public $email;
    public $name;
    public $password;

    public function __construct($email, $name, $password) {
        $this->email = $email;
        $this->name = $name;
        $this->password = $password;
    }
}

让我们重写你的UserFactory.php。但这一次,它包括我们构建UserInterface.php作为User的界面;

namespace main;

use main\UserInterface as User;

abstract class UserFactory {
    const ADMIN = 'AdminRecord';
    const MANAGER = 'ManagerRecord';
    const SALESMAN = 'SalesmanRecord';
    const CUSTOMER = 'CustomerRecord';

    public static function manufacture($type) {
        return new $type;
    }

    protected $accountController;
    protected $emailController;
    protected $toolMailer;

    function __construct() {
        $this->accountController = new \stdClass();
        $this->emailController = new \stdClass();
        $this->toolMailer = new \stdClass();
    }

    abstract public function signUp(User $user);
}

注意方法signUp();我用创建的接口键入提示,这意味着它只接受具有User实例的对象用户(实现用户界面)。

我认为下一组代码是不言自明的:

UserAdmin:

<?php
namespace main;

use main\AbstractUser;

class UserAdmin extends AbstractUser implements UserInterface {
    public $companyId;
    public $access;

    public function __construct($email, $name, $password, $companyId) {
        parent::__construct($email, $name, $password);
        $this->companyId = $companyId;
        $this->access = UserFactory::ADMIN;
    }
}

AdminRecord:signUp(User $ user)应该只接受UserAdmin.php的实例

<?php

namespace main;

use main\UserFactory;
use main\UserInterface as User;

class AdminRecord extends UserFactory {
    protected $accountCompanyController;

    function __construct() {
        parent::__construct();
        $this->accountCompanyController = new \stdClass(); //new AccountCompanyController();
    }

    public function signUp(User $user) {
        $accountId = $this->accountController->add($user->name, $user->password);
        $this->emailController->add($user->email, $accountId);
        $this->accountCompanyController->add($accountId, $user->companyId, $user->access);
        $this->toolMailer->adminWelcome($user->name, $user->email, $user->password);
    }
}

让我们重写你的抽象StaffRecord.php :(我认为没有变化)

<?php
namespace main;

use main\UserFactory;

abstract class StaffRecord extends UserFactory {
    protected $staffController;

    function __construct() {
        parent::__construct();
        $this->staffController = new \stdClass(); //staffController
    }
}

的UserManager:

<?php

namespace main;

use main\AbstractUser;

class UserManager extends AbstractUser implements UserInterface {
    public $shopId;
    public $access;

    public function __construct($email, $name, $password, $shopId) {
        parent::__construct($email, $name, $password);
        $this->shopId = $shopId;
        $this->access = UserFactory::MANAGER;
    }
}

ManagerRecord:

<?php

namespace main;

use main\StaffRecord;
use main\UserInterface as User;

class ManagerRecord extends StaffRecord {
    public function signUp(User $user) {
      $accountId = $this->accountController->add($user->name, $user->password);
      $this->emailController->add($user->email, $accountId);
      $this->staffController->add($accountId, $user->shopId, $user->access);
      $this->toolMailer->managerWelcome($user->name, $user->email, $user->password);
    }
}

UserSalesman:

<?php

namespace main;

use main\AbstractUser;

class UserSalesman extends AbstractUser implements UserInterface {
    public $cpf;
    public $access;
    public $shopId;

    public function __construct($email, $name, $password, $cpf, $shopId) {
        parent::__construct($email, $name, $password);
        $this->shopId = $shopId;
        $this->cpf = $cpf;
        $this->access = UserFactory::SALESMAN;
    }
}

SalesmanRecord:

<?php

namespace main;

use main\StaffRecord;
use main\UserInterface as User;

class SalesmanRecord extends StaffRecord {
    public function signUp(User $user) {
      $accountId = $this->accountController->addSeller($user->name, $user->password, $user->cpf);
      $this->emailController->add($user->email, $accountId);
      $this->staffController->add($accountId, $user->shopId, $user->access);
      $this->toolMailer->salesmanWelcome($user->name, $user->email, $user->password);
    }
}

UserCustomer:

<?php

namespace main;

use main\AbstractUser;

class UserCustomer extends AbstractUser implements UserInterface {
    public $cpf;
    public $phone;
    public $birthday;
    public $gender;

    public function __construct($email, $name, $password, $phone, $birthday, $gender) {
        parent::__construct($email, $name, $password);
        $this->phone = $phone;
        $this->birthday = $birthday;
        $this->gender = $gender;
        $this->access = UserFactory::CUSTOMER;
    }
}

的CustomerRecord:

<?php

namespace main;

use main\UserInterface;
use main\UserInterface as User;

class CustomerRecord extends UserFactory {
  protected $customerController;

  function __construct() {
    parent::__construct();
    $this->customerController = new \stdClass(); //customerController
  }

  public function signUp(User $user) {
    $accountId = $this->accountController->addCustomer($user->name, $user->password, $user->cpf, $user->phone, $user->birthday, $user->gender);
    $this->emailController->add($user->email, $accountId);
    $this->toolMailer->customerWelcome($user->name, $user->email, $user->password);
  }
}

这是我使用它的方式;

with loader.php:

<?php

function __autoload($class)
{
    $parts = explode('\\', $class);
    require end($parts) . '.php';
}
  

php main.php

<?php
namespace main;

include_once "loader.php";

use main\AdminRecord;
use main\UserAdmin;
use main\UserFactory;
use main\ManagerRecord;
use main\UserSalesman;
use main\CustomerRecord;


$userAdmin = new UserAdmin('francis@email.com', 'francis', 'test', 1);
$adminRecord = new AdminRecord($userAdmin);

$userManager = new UserManager('francis@email.com', 'francis', 'test', 1);
$managerRecord = new ManagerRecord($userManager);

$salesMan = new UserSalesman('francis@email.com', 'francis',  'test', 2, 1);
$salesmanRecord = new SalesmanRecord($salesMan);

//$email, $name, $password, $phone, $birthday, $gender
$customer = new UserCustomer('francis@email.com', 'francis', 'test', '0988-2293', '01-01-1984', 'Male');
$customerRecord = new CustomerRecord($customer);

print_r($adminRecord);
print_r($userManager);
print_r($salesMan);
print_r($salesmanRecord);
print_r($customer);
print_r($customerRecord);

下载文件:https://www.dropbox.com/sh/ggnplthw9tk1ms6/AACXa6-HyNXfJ_fw2vsLKhkIa?dl=0

我创建的解决方案并不完美,仍然需要重构和改进。

我希望这能解决你的问题。

感谢。

答案 1 :(得分:2)

我认为你错过了工厂的观点。您从工厂生成的类不会扩展工厂,工厂会生成并将对象实例作为相应的类或子类。您可以扩建工厂以建立更具体的工厂,但这是一个不同的主题。

AdminRecordManagerRecord等应该扩展公共基础抽象UserRecord类,而不是UserRecordFactory。工厂只需构建适当的用户记录。

绕过不同签名的最简单方法是传入包含所需属性的选项对象或数组。下面我展示了数组,但您可能需要一个UserSignupConfig类,可以像AdminSignupConfig一样进行特定用途的扩展,而不是通用数组。

abstract class UserRecord {
public static function manufacture($type) {
    return new $type;
}

protected $accountController;
protected $emailController;
protected $toolMailer;

function __construct() {
    $this->accountController = new AccountController();
    $this->emailController = new EmailController();
    $this->toolMailer = new ToolMailer();
}

abstract public function signUp(array $config = array());

 //or via optional object of type UserSignupConfig
// "abstract public function signUp(UserSignupConfig $config = null);"

}

在最基本的示例中,UserRecordFactory可以使用构建器方法来构造UserRecords(或任何子类)。

 //Create the factory
 $userRecordFactory = new UserRecordFactory();

 //Grab me something that extends UserRecord
 $adminRecord = $userRecordFactory->churnOutUserRecord("AdminRecord");

churnOutUserRecord方法可以是一个简单的开关:

public function churnOutUserRecord($type){
      $record = null;
      switch($type){
         case "AdminRecord": $record = new AdminRecord(); break;
         case "ManagerRecord": $record = new ManagerRecord(); break;
          ///...
      }
      return $record;

      // ...Or just "return new $type;" 
      // if you are 100% sure all $types are valid classes

    }  

最后一点:所有这些抽象类的使用并不是我喜欢的次要代码重用方式。相反,我建议尽可能使用接口,但这是一个更深层次的主题。