SlideShare a Scribd company logo
1 of 16
Facade Pattern in rails
Rasna Shakya
Associate software engineer
Overview
What is facade pattern?
Why facade pattern?
Advantage
Drawbacks
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
Jyaasa : We Design, Build and Develop Products
Why facade pattern?
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.
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
Jyaasa : We Design, Build and Develop Products
A lot of initializations of data and helping objects in
controller
Jyaasa : We Design, Build and Develop Products
Solution ???
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
# 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
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
Jyaasa : We Design, Build and Develop Products
and use it in the view:
<%= @chat_room.chat_message %>
<%= @chat_room.unread_message %>
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.
- Introducing another layer of abstraction to our application
Jyaasa : We Design, Build and Develop Products
Demerits
Jyaasa : We Design, Build and Develop Products
Thank you

More Related Content

Similar to Facade pattern in rails

2014_report
2014_report2014_report
2014_report
K SEZER
 
.NET Architecture for Enterprises
.NET Architecture for Enterprises.NET Architecture for Enterprises
.NET Architecture for Enterprises
Wade Wegner
 
Chandra Sekhar Cheekuru NET UI
Chandra Sekhar Cheekuru  NET UIChandra Sekhar Cheekuru  NET UI
Chandra Sekhar Cheekuru NET UI
Chandra Sekhar
 
COMP6210 Web Services And Design Methodologies.docx
COMP6210 Web Services And Design Methodologies.docxCOMP6210 Web Services And Design Methodologies.docx
COMP6210 Web Services And Design Methodologies.docx
write31
 

Similar to Facade pattern in rails (20)

Onion Architecture with S#arp
Onion Architecture with S#arpOnion Architecture with S#arp
Onion Architecture with S#arp
 
2014_report
2014_report2014_report
2014_report
 
.NET Architecture for Enterprises
.NET Architecture for Enterprises.NET Architecture for Enterprises
.NET Architecture for Enterprises
 
Final ppt
Final pptFinal ppt
Final ppt
 
Architecting for Change: An Agile Approach
Architecting for Change: An Agile ApproachArchitecting for Change: An Agile Approach
Architecting for Change: An Agile Approach
 
Cloud computing - an architect's perspective
Cloud computing - an architect's perspectiveCloud computing - an architect's perspective
Cloud computing - an architect's perspective
 
Prakash_Ganapathy
Prakash_GanapathyPrakash_Ganapathy
Prakash_Ganapathy
 
Domain Driven Design
Domain Driven DesignDomain Driven Design
Domain Driven Design
 
Top 5 backend frameworks for web development in.pptx
Top 5 backend frameworks for web development in.pptxTop 5 backend frameworks for web development in.pptx
Top 5 backend frameworks for web development in.pptx
 
Mvp4 croatia - Being a dba in a devops world
Mvp4 croatia - Being a dba in a devops worldMvp4 croatia - Being a dba in a devops world
Mvp4 croatia - Being a dba in a devops world
 
Actively looking for an opportunity to work as a challenging Dot Net Developer
Actively looking for an opportunity to work as a challenging Dot Net DeveloperActively looking for an opportunity to work as a challenging Dot Net Developer
Actively looking for an opportunity to work as a challenging Dot Net Developer
 
Actively looking for an opportunity to work as a challenging Dot Net Developer
Actively looking for an opportunity to work as a challenging Dot Net DeveloperActively looking for an opportunity to work as a challenging Dot Net Developer
Actively looking for an opportunity to work as a challenging Dot Net Developer
 
Chandra Sekhar Cheekuru NET UI
Chandra Sekhar Cheekuru  NET UIChandra Sekhar Cheekuru  NET UI
Chandra Sekhar Cheekuru NET UI
 
Latest Web development technologies 2021
 Latest Web development technologies 2021 Latest Web development technologies 2021
Latest Web development technologies 2021
 
Designingapplswithnet
DesigningapplswithnetDesigningapplswithnet
Designingapplswithnet
 
Vishal Jadhav
Vishal JadhavVishal Jadhav
Vishal Jadhav
 
Eclipse Developement @ Progress Software
Eclipse Developement @ Progress SoftwareEclipse Developement @ Progress Software
Eclipse Developement @ Progress Software
 
COMP6210 Web Services And Design Methodologies.docx
COMP6210 Web Services And Design Methodologies.docxCOMP6210 Web Services And Design Methodologies.docx
COMP6210 Web Services And Design Methodologies.docx
 
Electric Microservices Land - Tsuyoshi Ushio
Electric Microservices Land - Tsuyoshi UshioElectric Microservices Land - Tsuyoshi Ushio
Electric Microservices Land - Tsuyoshi Ushio
 
CV - Manuel_Lara
CV - Manuel_LaraCV - Manuel_Lara
CV - Manuel_Lara
 

More from Jyaasa Technologies

More from Jyaasa Technologies (20)

Incident management with jira
Incident management with jiraIncident management with jira
Incident management with jira
 
Extreme programming practices ( xp )
Extreme programming practices ( xp ) Extreme programming practices ( xp )
Extreme programming practices ( xp )
 
The myth of 'real javascript developer'
The myth of 'real javascript developer'The myth of 'real javascript developer'
The myth of 'real javascript developer'
 
Scrum ceromonies
Scrum ceromoniesScrum ceromonies
Scrum ceromonies
 
An introduction to bitcoin
An introduction to bitcoinAn introduction to bitcoin
An introduction to bitcoin
 
Tor network
Tor networkTor network
Tor network
 
Collective ownership in agile teams
Collective ownership in agile teamsCollective ownership in agile teams
Collective ownership in agile teams
 
Push notification
Push notificationPush notification
Push notification
 
The Design Thinking Process
The Design Thinking ProcessThe Design Thinking Process
The Design Thinking Process
 
User story
User storyUser story
User story
 
Design sprint
Design sprintDesign sprint
Design sprint
 
Data Flow Diagram
Data Flow DiagramData Flow Diagram
Data Flow Diagram
 
OKRs and Actions Overview
OKRs and Actions OverviewOKRs and Actions Overview
OKRs and Actions Overview
 
Vue.js
Vue.jsVue.js
Vue.js
 
Active record in rails 5
Active record in rails 5Active record in rails 5
Active record in rails 5
 
Design Patern::Adaptor pattern
Design Patern::Adaptor patternDesign Patern::Adaptor pattern
Design Patern::Adaptor pattern
 
Association in rails
Association in railsAssociation in rails
Association in rails
 
Web design layout pattern
Web design layout patternWeb design layout pattern
Web design layout pattern
 
Command Pattern in Ruby
Command Pattern in RubyCommand Pattern in Ruby
Command Pattern in Ruby
 
Design Pattern:: Template Method
Design Pattern:: Template MethodDesign Pattern:: Template Method
Design Pattern:: Template Method
 

Recently uploaded

Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
amitlee9823
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
ssuser89054b
 
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
dharasingh5698
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Kandungan 087776558899
 

Recently uploaded (20)

Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
 
Unit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdfUnit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdf
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.ppt
 
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
Intro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdfIntro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdf
 
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 

Facade pattern in rails

  • 3. Overview What is facade pattern? Why facade pattern? Advantage Drawbacks
  • 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. Jyaasa : We Design, Build and Develop Products Why facade pattern?
  • 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. 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. Jyaasa : We Design, Build and Develop Products A lot of initializations of data and helping objects in controller
  • 9. Jyaasa : We Design, Build and Develop Products Solution ???
  • 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. # 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. 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. Jyaasa : We Design, Build and Develop Products and use it in the view: <%= @chat_room.chat_message %> <%= @chat_room.unread_message %>
  • 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. - Introducing another layer of abstraction to our application Jyaasa : We Design, Build and Develop Products Demerits
  • 16. Jyaasa : We Design, Build and Develop Products Thank you

Editor's Notes

  1. https://sprint.ly/blog/scrum-meeting-best-practices/