DEV Community

Caleb Hearth
Caleb Hearth

Posted on • Originally published at on

The Decree Design Pattern

Majesties! Cease groveling before your codebase, and issue your applications Decrees! A Decree is a pattern designed to aid developers in extracting processes to places where they can be usefully reused. A class that implements the decree pattern has several benefits: it can easily be replaced as the API surface area is a single method; it can compose other Decrees; it can easily be faked for development; and it can easily be mocked for testing. The Decree pattern helps to both isolate and centralize the core logic of your app. In Rails apps, service objects are pretty much any object that’s not a controller or model. A service object can follow the Decree pattern by having names that tell our code what to do (it’s imperative to do this) and a single class-level entry point called .(...). A few examples of imperative names are CreateObject, GenerateReport, and PerformMentalGymastics. Names like these, combined with the shorthand #call() syntax, give us very nice ergonomics compared...

Read more

Top comments (0)