3. Rules engine
Replacing imperative model:
◦commands in sequence with conditionals and loops
◦set of rules – each rule has condition and action
4. Rules engine address the following issue
if car.owner.hasCellPhone then premium += 100;
if car.model.theftRating > 4 then premium += 200;
if car.owner.livesInDodgyArea && car.model.theftRating > 2
then premium += 300;
5. The solution
Create a
bunch
objects with
conditions
and actions
Store them
in a
collections
Run thought
them to
evaluate the
conditions
Execute the
actions
7. What are the benefits
Single Responsibility Principle (SRP)
Open/Close Principle (OCP)
Liskov substitution principle (LSP)
Interface segregation principle
Dependency inversion principle
8.
9.
10. • Algorithms can be selected on the
fly
Strategy
pattern
• Combinable business logic in a
Boolean fashion
Specification
pattern
• Command objects are handled or
passed on to other objects by
logic-containing processing object
Chain of
responsibility
pattern
Why we use design patterns?
Have you ever used design patterns?
Last used design patterns?
http://www.codeproject.com/Tips/808058/Reasons-for-using-design-patterns
How many of you have heard about SOLID - SRP, OCP?
Business rules and validation are an integral part of most business applications.
There are rules and validations that must occur during processing of business logic.
Most applications will combine the business logic with rules and data validation – when this happens, testing and maintaining applications becomes more difficult.
The Strategy Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it. (http://csharpbytes.azurewebsites.net/the-strategy-pattern/)
The Specification Pattern outlines a business rule that is combinable with other business rules. In this pattern, a unit of business logic inherits its functionality from the abstract aggregate Composite Specification class. The Composite Specification class has one function called IsSatisfiedBy that returns a boolean value. After instantiation, the specification is "chained" with other specifications, making new specifications easily maintainable, yet highly customizable business logic. Furthermore, upon instantiation the business logic may, through method invocation or inversion of control, have its state altered in order to become a delegate of other classes such as a persistence repository. (https://en.wikipedia.org/wiki/Specification_pattern, http://www.ilove-it.com/post/2016/06/17/using-the-specification-pattern-in-typescript)
http://www.oodesign.com/chain-of-responsibility-pattern.html
http://stackoverflow.com/questions/5039024/whats-the-difference-between-chain-of-responsibility-and-strategy-patterns