gpt4 book ai didi

php - 没有方法重载的 PHP 中的抽象工厂

转载 作者:可可西里 更新时间:2023-11-01 13:22:41 27 4
gpt4 key购买 nike

现状

我目前有 4 种类型的用户,我们预计 future 至少会增加 3 种。目前他们是:

  • 管理员(商店管理员组)
  • 员工(店长)
  • 员工(商店销售员)
  • 客户

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

问题

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

我的尝试

在 UserFactory 类中,我们可以看到问题的根源:abstract public function signUp();。这是一种不好的做法,甚至会导致 PHP 5.4+ 上的 Strict Standard 错误不遵守方法签名。在 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);
}

}

在这里我创建了一个新的抽象类,因为我的两个用例属于同一个实体(销售员和经理都是具有不同访问级别的员工)。

abstract class StaffRecord extends UserFactory {

protected $staffController;

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

}

在这里,SignUp 的签名将与 Admin 相同,这排除了使用 func_num_args()func_get_args() 的工作。等等,但是在 Java 中你不能使用方法重载来解决这个问题。没错,但在 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);
}

}

最佳答案

这是我的实现:

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

接口(interface)创建:

namespace main;
interface UserInterface { }

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

通过在 signUp(User $user) 上使用类型提示,它将解决您在注册中传递的不同类型签名的问题。它可以是用户类型管理员、经理、销售员和客户。每个 {User}Record 都扩展并实现了抽象工厂,但在实现上有所不同。

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

AbstractUser.php - 我注意到一个用户的共同属性,所以我创建了一个抽象用户。公共(public)属性(电子邮件、姓名、密码)

<?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。但是这一次,它包含了我们作为 User 构建的 UserInterface.php 接口(interface);

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 实例的对象用户(实现用户界面)。

我假设下一组代码是不言自明的:

用户管理员:

<?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
}
}

用户管理器:

<?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;
}
}

经理记录:

<?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);
}
}

用户销售员:

<?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;
}
}

销售员记录:

<?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);
}
}

用户客户:

<?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;
}
}

客户记录:

<?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);
}
}

我是这样用的;

使用 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

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

希望这能解决您的问题。

谢谢。

关于php - 没有方法重载的 PHP 中的抽象工厂,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28117856/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com