fbpx
วิกิพีเดีย

ฟะซาดแพตเทิร์น

ฟะซาดแพตเทิร์น (อังกฤษ: Façade pattern,Facade pattern) เป็นดีไซน์แพตเทิร์นเพื่อช่วยให้การเรียกใช้งานคลาสต่างๆ ในระบบที่ซับซ้อนทำได้ง่ายขึ้น ฟะซาดแพตเทิร์นได้ชื่อมาจากส่วนตกแต่งด้านหน้าของอาคาร ที่ทำเพื่อความสวยงามและซ่อนโครงสร้างของตัวตึก ตัวอย่างที่เห็นได้ในชีวิตประจำวันเช่น เกียร์รถยนต์แบบอัตโนมัติที่ผู้ใช้ไม่จำเป็นต้องกังวลกับรายละเอียดวิธีการเปลี่ยนเกียร์ เป็นต้น

การนำไปใช้งาน

ถ้าเรามีคลาสกลุ่มหนึ่ง (หรือแม้แต่คลาสเดียว) ที่มีวิธีการเรียกใช้งานที่สลับซับซ้อน เราสามารถซ่อนรายละเอียดวิธีการเรียกใช้งานนี้โดยสร้างคลาสขึ้นมาเพื่อทำหน้าที่เป็นฟะซาด และรวบรวมโค้ดที่ซับซ้อนไว้ในเมธอดของคลาสฟะซาดที่ง่ายต่อการเรียกใช้งาน คลาสผู้ใช้บริการเพียงเรียกเมธอดของคลาสฟะซาด โดยไม่จำเป็นต้องรู้รายละเอียดนอกเหนือไปจากนี้

 
UML คลาสไดอะแกรมของฟะซาดแพตเทิร์น

ตัวอย่างโปรแกรม

ภาษาจาวา

ซอร์สโค้ดของฟะซาดเพื่อช่วยเขียนไฟล์ (เรียกใช้ File และ FileWriter) และอ่านไฟล์ (เรียกใช้ File, FileReader และ BufferedReader)

import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class FileFacade { public void write (String filePath, String content) { File file = new File (filePath) ; FileWriter fileWriter = null; try { fileWriter = new FileWriter (file) ; fileWriter.write (content) ; fileWriter.flush () ; } catch (IOException e) { e.printStackTrace () ; } finally { if (fileWriter != null) { try { fileWriter.close () ; } catch (IOException e) { e.printStackTrace () ; } } } } public String read (String filePath) { StringBuilder sb = new StringBuilder () ; File file = new File (filePath) ; BufferedReader bufferedReader = null; try { FileReader fileReader = new FileReader (file) ; bufferedReader = new BufferedReader (fileReader) ; String s; while ((s = bufferedReader.readLine ()) != null) { sb.append (s) ; } } catch (FileNotFoundException e) { e.printStackTrace () ; } catch (IOException e) { e.printStackTrace () ; } finally { if (bufferedReader != null) { try { bufferedReader.close () ; } catch (IOException e) { e.printStackTrace () ; } } } return sb.toString () ; } } 

การเรียกใช้งาน

String filePath = "filefacade.txt"; FileFacade ff = new FileFacade () ; // write to file ff.write (filePath, "Hello World!") ; // read from file String content = ff.read (filePath) ; 

อ้างอิง

  • Design Patterns: Elements of Reusable Object-Oriented Software (ISBN 0-201-63361-2) โดย Erich Gamma, Richard Helm, Ralph Johnson และ John Vlissides (Gang of four: GoF)

แหล่งข้อมูลอื่น

  • โดย Vince Huston (อังกฤษ)

ฟะซาดแพตเท, งกฤษ, façade, pattern, facade, pattern, เป, นด, ไซน, แพตเท, นเพ, อช, วยให, การเร, ยกใช, งานคลาสต, างๆ, ในระบบท, บซ, อนทำได, ายข, ได, อมาจากส, วนตกแต, งด, านหน, าของอาคาร, ทำเพ, อความสวยงามและซ, อนโครงสร, างของต, วต, วอย, างท, เห, นได, ในช, ตประจำว,. fasadaephtethirn xngkvs Facade pattern Facade pattern epndiisnaephtethirnephuxchwyihkareriykichngankhlastang inrabbthisbsxnthaidngaykhun fasadaephtethirnidchuxmacakswntkaetngdanhnakhxngxakhar thithaephuxkhwamswyngamaelasxnokhrngsrangkhxngtwtuk twxyangthiehnidinchiwitpracawnechn ekiyrrthyntaebbxtonmtithiphuichimcaepntxngkngwlkbraylaexiydwithikarepliynekiyr epntn enuxha 1 karnaipichngan 2 twxyangopraekrm 2 1 phasacawa 3 xangxing 4 aehlngkhxmulxunkarnaipichngan aekikhthaeramikhlasklumhnung hruxaemaetkhlasediyw thimiwithikareriykichnganthislbsbsxn erasamarthsxnraylaexiydwithikareriykichnganniodysrangkhlaskhunmaephuxthahnathiepnfasad aelarwbrwmokhdthisbsxniwinemthxdkhxngkhlasfasadthingaytxkareriykichngan khlasphuichbrikarephiyngeriykemthxdkhxngkhlasfasad odyimcaepntxngruraylaexiydnxkehnuxipcakni UML khlasidxaaekrmkhxngfasadaephtethirntwxyangopraekrm aekikhphasacawa aekikh sxrsokhdkhxngfasadephuxchwyekhiynifl eriykich File aela FileWriter aelaxanifl eriykich File FileReader aela BufferedReader import java io BufferedReader import java io File import java io FileNotFoundException import java io FileReader import java io FileWriter import java io IOException public class FileFacade public void write String filePath String content File file new File filePath FileWriter fileWriter null try fileWriter new FileWriter file fileWriter write content fileWriter flush catch IOException e e printStackTrace finally if fileWriter null try fileWriter close catch IOException e e printStackTrace public String read String filePath StringBuilder sb new StringBuilder File file new File filePath BufferedReader bufferedReader null try FileReader fileReader new FileReader file bufferedReader new BufferedReader fileReader String s while s bufferedReader readLine null sb append s catch FileNotFoundException e e printStackTrace catch IOException e e printStackTrace finally if bufferedReader null try bufferedReader close catch IOException e e printStackTrace return sb toString kareriykichngan String filePath filefacade txt FileFacade ff new FileFacade write to file ff write filePath Hello World read from file String content ff read filePath xangxing aekikhDesign Patterns Elements of Reusable Object Oriented Software ISBN 0 201 63361 2 ody Erich Gamma Richard Helm Ralph Johnson aela John Vlissides Gang of four GoF aehlngkhxmulxun aekikhFacade ody Vince Huston xngkvs ekhathungcak https th wikipedia org w index php title fasadaephtethirn amp oldid 9576790, wikipedia, วิกิ หนังสือ, หนังสือ, ห้องสมุด,

บทความ

, อ่าน, ดาวน์โหลด, ฟรี, ดาวน์โหลดฟรี, mp3, วิดีโอ, mp4, 3gp, jpg, jpeg, gif, png, รูปภาพ, เพลง, เพลง, หนัง, หนังสือ, เกม, เกม