/******* 第一个版本 *********/
class GoodService
{
public function paginate(Request $request, $perPage = 10)
{
$good = new GoodModel();
$query = $good->query();
if ($request->name) {
$query->where('name', $request->name);
}
$paginate = $query->paginate($perPage);
foreach ($paginate as &$row)
{
$row = $this->initRow($row);
}
return $row;
}
public function initRow($row)
{
$orderService = new OrderService();
$row->sum = 0;
if ($row->id) {
$row->sum = $orderService->sumByGoodsId($row->od);
}
return $row;
}
public function get(Request $request)
{
$good = new GoodModel();
$query = $good->query();
if ($request->name) {
$query->where('name', $request->name);
}
$data = $query->get();
foreach ($data as &$row) {
$row = $this->initRow($row);
}
return $row;
}
}
class GoodModel
{
public function query()
{
return $this;
}
public function where($file, $value)
{
return $this;
}
public function paginate()
{
return [];
}
public function get()
{
return [];
}
}
class OrderModel
{
public function query()
{
return $this;
}
public function where($file, $value)
{
return $this;
}
public function sum()
{
return 10;
}
}
class OrderService
{
public function sumByGoodsId($goodId)
{
$orderModel = new OrderModel();
return $orderModel->query()->where('good_id', $goodId)->sum();
}
}
class Export
{
protected $goodService;
public function __construct(GoodService $goodService)
{
$this->goodService = $goodService;
}
public function export()
{
$data = $this->goodService->get();
foreach ($data as $row) {
$data = $this->goodService->initRow($row);
}
return '导出';
}
}
class Request {
public function __get($name)
{
return$_POST[$name] ?? '';
}
}
$service = new GoodService();
$service->paginate(new Request(), 10);
$export = new Export($service);
$export->export();
/*********************第二个版本*********************/
/**中介者模式,使GoodService orderService Export 相互之间 解耦,不直接new,而是通过中介者Mediator去拿这些客户端传入的new对象
* Class AbstarctMediator
*/
class AbstarctMediator
{
protected $mediator;
public function __construct(Mediator $mediator)
{
$this->mediator;
}
/**
* @return Mediator
*/
public function getMediator()
{
return $this->mediator;
}
}
class GoodService extends AbstarctMediator
{
public function paginate($perPage = 10)
{
$good = $this->getMediator()->getGoodModel();
$request = $this->getMediator()->getRequest();
$query = $good->query();
if ($request->name) {
$query->where('name', $request->name);
}
$paginate = $query->paginate($perPage);
foreach ($paginate as &$row)
{
$row = $this->initRow($row);
}
return $row;
}
public function initRow($row)
{
$row->sum = 0;
if ($row->id) {
$row->sum = $this->getMediator()->sumByGoodId($row->id);
}
return $row;
}
public function get()
{
$good = $this->getMediator()->getGoodModel();
$request = $this->getMediator()->getRequest();
$query = $good->query();
if ($request->name) {
$query->where('name', $request->name);
}
$data = $query->get();
foreach ($data as &$row) {
$row = $this->initRow($row);
}
return $row;
}
}
class GoodModel
{
public function query()
{
return $this;
}
public function where($file, $value)
{
return $this;
}
public function paginate()
{
return [];
}
public function get()
{
return [];
}
}
class OrderModel
{
public function query()
{
return $this;
}
public function where($file, $value)
{
return $this;
}
public function sum()
{
return 10;
}
}
class OrderService extends AbstarctMediator
{
public function sumByGoodsId($goodId)
{
$orderModel = $this->getMediator()->getOrderModel();
return $orderModel->query()->where('good_id', $goodId)->sum();
}
}
class Export extends AbstarctMediator
{
public function export()
{
$data = $this->getMediator()->get->get();
foreach ($data as $row) {
$data = $this->goodService->initRow($row);
}
return '导出';
}
}
class Request {
public function __get($name)
{
return$_POST[$name] ?? '';
}
}
class Mediator
{
/**
* @var GoodService
*/
protected $goodService;
/**
* @var GoodModel
*/
protected $goodModel;
/**
* @var OrderService
*/
protected $orderService;
/**
* @var OrderModel
*/
protected $orderModel;
/**
* @var Request
*/
protected $request;
/**
* @var Export
*/
protected $export;
public function setMedia(
GoodService $goodService,
GoodModel $goodModel,
OrderService $orderService,
OrderModel $orderModel,
Request $request,
Export $export
)
{
$this->goodService = $goodService;
$this->goodModel = $goodModel;
$this->orderService = $orderService;
$this->orderModel = $orderModel;
$this->request = $request;
$this->export = $export;
}
public function getGoodService()
{
return $this->goodService;
}
public function getGoodModel()
{
return $this->goodModel;
}
public function getOrderService()
{
return $this->orderService;
}
public function getOrderModel()
{
return $this->orderModel;
}
public function getRequest()
{
return $this->request;
}
public function sumByGoodId($goodId)
{
return $this->orderService->sumByGoodsId($goodId);
}
public function export()
{
return $this->export->export();
}
}
$mediator = new Mediator();
$orderModel = new OrderModel();
$goodModel = new GoodModel();
$goodService = new GoodService($mediator);
$orderService = new OrderService($mediator);
$export = new Export($mediator);
$request = new Request();
$mediator->setMedia(
$goodService,
$goodModel,
$orderService,
$orderModel,
$request,
$export
);
$goodService->paginate(10);
$export->export();
/***************************第三个版本***************************/
/**外观模式 , 为了客户端能简单调用,则用外观模式,让看起来很简单去调用
* Class Action
*/
class Action
{
/**
* @var Mediator
*/
protected $mediator;
/**
* @var GoodService
*/
protected $goodService;
/**
* @var GoodModel
*/
protected $goodModel;
/**
* @var OrderService
*/
protected $orderService;
/**
* @var OrderModel
*/
protected $orderModel;
/**
* @var Request
*/
protected $request;
/**
* @var Export
*/
protected $export;
public function __construct()
{
$this->mediator = new Mediator();
$this->orderModel = new OrderModel();
$this->goodModel = new GoodModel();
$this->goodService = new GoodService($this->mediator);
$this->orderService = new OrderService($this->mediator);
$this->export = new Export($this->mediator);
$this->request = new Request();
$this->mediator->setMedia(
$this->goodService,
$this->goodModel,
$this->orderService,
$this->orderModel,
$this->request,
$this->export
);
}
public function paginate($perpage = 10)
{
return $this->goodService->paginate($perpage);
}
public function export()
{
return $this->export->export();
}
}
$action = new Action();
//分页显示
$action->paginate(10);
//导出
$action->export();