Specification code examples of PHP design patterns (28)

Time:2020-11-22

objective

Build a clear business rule specification, in which each rule can be targeted. Each specification class has a method called issatisfiedby, which determines whether a given rule meets the specification and returns true or false.

UML diagram

The official PHP advanced learning exchange community “click” manages and sorts out some materials. Bat and other first-line factories have advanced knowledge systems (related learning materials and written test questions) and are not limited to: distributed architecture, high scalability, high performance, high concurrency, server performance tuning, tp6, laravel, yii2, redis, swote, soft, Kafka, MySQL optimization, shell script, dock Er, micro service, nginx and other knowledge points

code

  • Item.php
price = $price;
    }

    public function getPrice(): float
    {
        return $this->price;
    }
}
  • SpecificationInterface.php
  • OrSpecification.php
specifications = $specifications;
    }

    /**
     *Returns true if a rule meets the criteria, otherwise returns false
     */
    public function isSatisfiedBy(Item $item): bool
    {
        foreach ($this->specifications as $specification) {
            if ($specification->isSatisfiedBy($item)) {
                return true;
            }
        }
        return false;
    }
}
  • PriceSpecification.php
minPrice = $minPrice;
        $this->maxPrice = $maxPrice;
    }

    public function isSatisfiedBy(Item $item): bool
    {
        if ($this->maxPrice !== null && $item->getPrice() > $this->maxPrice) {
            return false;
        }

        if ($this->minPrice !== null && $item->getPrice() < $this->minPrice) {
            return false;
        }

        return true;
    }
}
  • AndSpecification.php
specifications = $specifications;
    }

    /**
     *If a rule does not meet the criteria, it returns false, otherwise it returns true
     */
    public function isSatisfiedBy(Item $item): bool
    {
        foreach ($this->specifications as $specification) {
            if (!$specification->isSatisfiedBy($item)) {
                return false;
            }
        }

        return true;
    }
}
  • NotSpecification.php
specification = $specification;
    }

    public function isSatisfiedBy(Item $item): bool
    {
        return !$this->specification->isSatisfiedBy($item);
    }
}

test

  • Tests/SpecificationTest.php
assertFalse($orSpec->isSatisfiedBy(new Item(100)));
        $this->assertTrue($orSpec->isSatisfiedBy(new Item(51)));
        $this->assertTrue($orSpec->isSatisfiedBy(new Item(150)));
    }

    public function testCanAnd()
    {
        $spec1 = new PriceSpecification(50, 100);
        $spec2 = new PriceSpecification(80, 200);

        $andSpec = new AndSpecification($spec1, $spec2);

        $this->assertFalse($andSpec->isSatisfiedBy(new Item(150)));
        $this->assertFalse($andSpec->isSatisfiedBy(new Item(1)));
        $this->assertFalse($andSpec->isSatisfiedBy(new Item(51)));
        $this->assertTrue($andSpec->isSatisfiedBy(new Item(100)));
    }

    public function testCanNot()
    {
        $spec1 = new PriceSpecification(50, 100);
        $notSpec = new NotSpecification($spec1);

        $this->assertTrue($notSpec->isSatisfiedBy(new Item(150)));
        $this->assertFalse($notSpec->isSatisfiedBy(new Item(50)));
    }
}

PHP Internet architect’s way to grow * “design patterns” Ultimate Guide

PHP Internet architect 50K growth Guide + general outline of industry problem solving (continuously updated)

Interview 10 companies, get 9 offers, PHP interview questions in 2020

If you like my article and want to communicate and learn with more senior developers, you can get more technical advice and guidance related to interview with large factories. Welcome to join our group, code: phpzh (junyang number: 856460874).

The latest PHP advanced tutorial 2020, full series!

If the content is good, I hope you can support and encourage you to click like / like, and you are welcome to exchange views. In addition, if you have any questions, you can suggest the content you want to see in the comments