Diese Präsentation wurde erfolgreich gemeldet.
Wir verwenden Ihre LinkedIn Profilangaben und Informationen zu Ihren Aktivitäten, um Anzeigen zu personalisieren und Ihnen relevantere Inhalte anzuzeigen. Sie können Ihre Anzeigeneinstellungen jederzeit ändern.

Facade pattern in rails

215 Aufrufe

Veröffentlicht am

Facade pattern provides a unified interface to a set of interfaces in a subsystem.

Veröffentlicht in: Ingenieurwesen
  • Als Erste(r) kommentieren

  • Gehören Sie zu den Ersten, denen das gefällt!

Facade pattern in rails

  1. 1. Facade Pattern in rails
  2. 2. Rasna Shakya Associate software engineer
  3. 3. Overview What is facade pattern? Why facade pattern? Advantage Drawbacks
  4. 4. Jyaasa : We Design, Build and Develop Products What is Facade Pattern? The Facade Pattern is simply a formalization of the idea that if you cannot avoid complexity, then the next best thing to do is to isolate it, to keep it from contaminating your whole system” -- Russ Olsen Facade pattern provides a unified interface to a set of interfaces in a subsystem. Facade pattern hides the complexities of the system and provides an interface to the client
  5. 5. Jyaasa : We Design, Build and Develop Products Why facade pattern?
  6. 6. Jyaasa : We Design, Build and Develop Products Ruby on rails is built on MVC paradigm. The problem in rails project, Controller is very weak and a lot of query and logics are happening on it to prepare data for views. Although we can use services and decorators for restructuring data to be more digestible by a view, still its not enough and we have fat controller.
  7. 7. Jyaasa : We Design, Build and Develop Products # app/controllers/chat_rooms_controller.rb Class ChatRoomsController < ApplicationController before_filter :set_chat_room, only: :show def show @message = @chat_room.messages.new @chat_message = @chat_room.messages.limit(5) @unread_message = @chat_room.unread_msg(current_user) end private def set_chat_room @chat_room = current_user.chat_rooms.find(params[:id]) end end
  8. 8. Jyaasa : We Design, Build and Develop Products A lot of initializations of data and helping objects in controller
  9. 9. Jyaasa : We Design, Build and Develop Products Solution ???
  10. 10. Jyaasa : We Design, Build and Develop Products We can use use Facade design pattern to remove that responsibility of preparing data for the view and create unified face visible to outside world, also to hide the components underneath. Let’s create a new folder in Rails project app/facades and let’s create our facade PORO class
  11. 11. # app/facades/chat_rooms_facade.rb Class ChatRoomsFacade attr_reader :chat_room, current_user def initialize(chat_room, current_user) @chat_room = chat_room @current_user = current_user end def unread_message @unread_message = chat_room.unread_msg(current_user) end def new_message @message = Message.new end Jyaasa : We Design, Build and Develop Products def chat_message @chat_message = chat_room.messages.limit(5) end end
  12. 12. Jyaasa : We Design, Build and Develop Products After moving all of the data preparations to ChatRoomsFacade, that allows us to simply controller: # app/controllers/chat_rooms_controller.rb Class ChatRoomsController < ApplicationController before_filter :set_chat_room, only: :show def show @chat_room = ChatRoomsFacade,new(@chat_room, current_user) end Private def set_chat_room @chat_room = current_user.chat_rooms.find(params[:id]) end end
  13. 13. Jyaasa : We Design, Build and Develop Products and use it in the view: <%= @chat_room.chat_message %> <%= @chat_room.unread_message %>
  14. 14. Jyaasa : We Design, Build and Develop Products Advantage Facade pattern can be very helpful for simplifying the controllers, especially if we have rather big project that is still growing.
  15. 15. - Introducing another layer of abstraction to our application Jyaasa : We Design, Build and Develop Products Demerits
  16. 16. Jyaasa : We Design, Build and Develop Products Thank you

×