Ruby on Rails Patterns Decorator Vs. Presenter
In the present times, Ruby on rails programming has emerged as a full-fledged web development technology, all thanks to Ruby developers worldwide for their contribution, and donation of their valuable time. Therefore if you are planning to hire Ruby on Rails developers to create engaging web experience for your business, you ought to seek technical competency of resources of specific Ruby On Rails Development Company. The framework of Rails is built on the principal of MVC (model-view-controller) at its core. We, programmers are blessed with a variety of data structures in this comprehensive Rails library including strings, numbers, date and time, arrays, hashes, files and directories. Each construct is in place to tackle a different type of problem. However, we will take a brief tour on two unique tokens in this text – decorator and presenter. Although both these patterns look similar on the surface, they have subtle differences under the hood. If you used to be confused for identifying between decorator and presenter, or are still in the block, believe me, it is normal. Due to homogeneous genre found in both gems. Decorators A decorator resembles functionality of inheritance that we find in many object oriented programming languages. It allows adding of non-generic methods to define contextual objects which already possess all features of any common entity. Like Mercedes brand’s motorcars hold advance facilities on top of typical automobile machines. Take for example, following is our base class. We will generate decorator first, and then presenter to see distinctions between them.
Example Base Class class car attr_reader :price def initialize(price) @price = price end end Suppose our Mercedes series of cars have flexible, 360 degree stretchable seats and costs extra. To make decorator for the same, you may use couple of ways. We will go for less trivial method, shown below: Decorator on Base Class class CarWithFlexibleSeats < Decorator def price mercedes + 5_000 end end car = Car.new car.price #=> it is 10_000, suppose car = CarWithFlexibleSeats.new(car) car.price #=> 15_500
Presenter Presenter is a type or subset of decorator itself. It is close to view model of MVC architecture. Presenters are composite objects and we feed multiple options to it. It renders desired outcome based on successful matching of condition. In above example, presenter can be defines as follows: Presenter on Base Class class CarPresenter < Decorator def description if price > 10_000 "Expensive!" else "Cheap!" end end end car = CarPresenter.new(Car.new) car.price #=> 10,000 suppose car.description => "Cheap!" The main objective of presenters is to put logic out of the view. Wrap up Decorators are just a bit broader in nature; they work on open/closed principle into the practice. This principle states that a class can be open for extension but not for modification. This way, Rails implements robust form of OOPS inheritance concept than other kinds of technologies. Premier borderline between presenter and decorator is that presenters never live close to business logic layer and decorators are created independent of view layer.
Nevertheless, learning about all types of decorator design patterns, be it exhibit, presenter or core decorator itself, is very useful and appreciated to have under your toolbelt. Ruby coders at Bacancy technology draw years of experience and have internalized the Ruby culture. Call us today for making of impeccable web and mobile solutions for your business using Ruby or any other technology of preference.