English 中文(简体)
A. 地名战略模式2
原标题:Strategy pattern in Symfony2

我试图建立简单服务,提供各种页数。 基本概念有:

$somePageType = new PageType(...);
$this->get( page.service )->render($somePagetype);

......设计为Strategy patterns。 页面类型将采用<代码>render 方法和page.service。 问题在于,我想在页数类别中使用理论。 我在此有什么选择? 我不想为这些班子创造服务。 这甚至有可能吗? 能否在不提供服务的情况下让集装箱了解? 也许将来,某些页面类型可能只需要多一点的理论,因此,我还需要铭记这一点。

最佳回答

缩略语 在这种情况下,你可以通过<编码>网页注入受扶养人,你不需要将战略定义为服务。

每项战略都可能取决于不同的目标,因此,我猜测你可以将其编成<代码>。 集装箱 意识到。 这里的例子就是如何做到这一点。

// This is the page.service class
class MyPageService {

    public function render(PageTypeInterface $page_type) {
        $page_type->setContainer($this->container);

        // do stuff
    }
}

// This is the type strategy
class MyStrategyType extends ContainerAware implements PageTypeInterface {
    // you can access the container after MyPageService has injected it.
}

因此,每项战略基本上都将.ContainerA注入该集装箱。


如果你们的所有战略都依赖同样的服务,我就把这些服务而不是整个集装箱注入。

class MyPageService {

    public function render(PageTypeInterface $page_type) {
        $page_type->setService($this->container->get( my_service ));

        // do stuff
    }
}
问题回答

这里的服务完全是你想要的。 能够放弃相关战略的附属条件。 然后将特定战略纳入控制人员(也可能是一个动态因素,在时间选择战略)。

ContainerAware is a really bad practice, it couples the object in question to all of the services in the container. Thus, I d strongly recommend avoiding it.





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签