download2565 2019-05-23 08:20
浏览 194

这是Builder模式的正确用法吗?

I have a scenario like this:

The system handles reservations from couple of different sources. All bookings come in the form of a json with differing formats for each source. Some JSON attributes are present in one JSON and not in the other etc.

In order to enter the reservation into the system, I am trying to centralize the Add Reservation logic into one method. To do this, I am trying to create a class called Reservation using the Builder pattern as such:

<?php

namespace App\Business\Classes\Reservation;


use App\Business\Classes\Utils\ReservationUtils;

/**
 * Class Reservation
 * Holds entire reservation details
 * @package App\Business\Classes\Reservation
*/
class Reservation
{
    protected $reference, $otaReference, $hotelCode, $customerId, $guestName, $couponId, $checkin, $checkout, $guests, $customerCountry,
    $commissionRate, $paymentStatus, $commissionOta, $commissionDifference, $rooms = [], $cityTax, $vat, $serviceCharge, $geniusBooker = false,
    $geniusAmount = 0, $taxes, $taxIncluded = false, $supplierAmount, $totalAmount, $specialRequest, $currency, $deviceType, $bookingSource,
    $otaRefId, $status, $source, $remarks;

    public function __construct()
    {
        // generates a reference
        $this->reference = ReservationUtils::referenceGenerator();
    }

     /**
      * @param mixed $otaReference
      * @return Reservation
     */
     public function setOtaReference($otaReference): Reservation
     {
        $this->otaReference = $otaReference;
        return $this;
     }

     /**
      * @param mixed $checkin
      * @return Reservation
     */
     public function setCheckin($checkin): Reservation
     {
        $this->checkin = $checkin;
        return $this;
     }

     /**
      * @param mixed $checkout
      * @return Reservation
     */
     public function setCheckout($checkout): Reservation
     {
        $this->checkout = $checkout;
        return $this;
     }

     /**
      * @param array $rooms
      * @return Reservation
     */
     public function setRooms(array $rooms): Reservation
     {
        $this->rooms = $rooms;
        return $this;
     }
     // and so on....
}

And I would use it like this:

$data= json_decode($result->jsonData);
$reservation = (new Reservation())
                  ->setOtaReference($data->otaReference)
                  ->setCheckin($data->checkin)
                  ->setCheckout($data->checkout); //...etc

This is probably not the textbook usage of the builder pattern in PHP or even Java but is there anything wrong with it?

Thanks

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 对于相关问题的求解与代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
    • ¥15 保护模式-系统加载-段寄存器
    • ¥15 电脑桌面设定一个区域禁止鼠标操作
    • ¥15 求NPF226060磁芯的详细资料
    • ¥15 使用R语言marginaleffects包进行边际效应图绘制