fbpx
วิกิพีเดีย

อะแด็ปเตอร์แพตเทิร์น

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

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

การสร้างคลาสสำหรับทำหน้าที่เป็นอะแด็ปเตอร์สามารถทำได้สองวิธีดังนี้

วิธีการแบบ inheritance ให้คลาสอะแด็ปเตอร์ซับคลาสจากคลาสที่จะถูกใช้งานจริงและให้อิมพลิเมนท์อินเตอร์เฟสตามที่คลาสผู้เรียกคาดหวัง ความสัมพันธ์ระหว่างผู้แปลงและผู้ถูกแปลงจะเป็นในแบบ is-a ดังภาพประกอบ

 
คลาสไดอะแกรมของอะแด็ปเตอร์แพตเทิร์นชนิด inheritance ในแบบ UML

วิธีการแบบ delegation ให้คลาสอะแด็ปเตอร์อิมพลิเมนท์อินเตอร์เฟสตามที่คลาสผู้เรียกคาดหวังแต่ไม่ต้องซับคลาส คลาสอะแด็ปเตอร์เรียกเมธอดของคลาสที่จะถูกใช้งานผ่านทางอ็อบเจกต์ของคลาสที่ถูกใช้งานนั้น ความสัมพันธ์ระหว่างผู้แปลงและผู้ถูกแปลงจะเป็นในแบบ has-a ดังภาพ

 
คลาสไดอะแกรมของอะแด็ปเตอร์แพตเทิร์นชนิด delegation ในแบบ UML

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

ภาษาจาวา

ซอร์สโค้ดอะแด็ปเตอร์แบบ inheritance

public class Adapter extends Adaptee implements ExpectedInterface { public void doWork() { actualDoWork(); } } 

ซอร์สโค้ดอะแด็ปเตอร์แบบ delegation

public class Adapter implements ExpectedInterface { private Adaptee adaptee; public void doWork() { adaptee.actualDoWork(); } } 

อินเตอร์เฟสที่โค้ดผู้เรียกคาดหวัง

public interface ExpectedInterface { public void doWork(); } 

คลาสที่ถูกแปลง

public class Adaptee { public void actualDoWork() { // do work } } 

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

expectedInterface.doWork(); 

อ้างอิง

  • 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)

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

  • Adopt Adapter โดย David Geary (อังกฤษ)
  • Design Patterns in Java: Adapter โดย Steven John Metsker (อังกฤษ)
  • อะแด็ปเตอร์แพตเทิร์น โดย อนุภาค มาตรมูล(ไทย)

อะแด, ปเตอร, แพตเท, adapter, pattern, เป, นด, ไซน, แพตเท, ใช, เช, อมการทำงานระหว, างคลาสผ, เร, ยกและคลาสท, กเร, ยกในกรณ, คลาสท, กเร, ยกม, เมธอดท, แตกต, างจากท, เร, ยกคาดหว, วอย, างท, เห, นได, ในช, ตประจำว, นเช, หม, อแปลงไฟฟ, หร, อปล, กไฟท, ขาต, างร, ปทรงต, างก. xaaedpetxraephtethirn Adapter pattern epndiisnaephtethirn thiichechuxmkarthanganrahwangkhlasphueriykaelakhlasthithukeriykinkrnithikhlasthithukeriykmiemthxdthiaetktangcakthiphueriykkhadhwng twxyangthiehnidinchiwitpracawnechn hmxaeplngiffa hruxplkifthimikhatangrupthrngtangkncatxngmitwaeplngephuxtxechuxmepntn enuxha 1 karnaipichngan 2 twxyangopraekrm 2 1 phasacawa 3 xangxing 4 aehlngkhxmulxunkarnaipichngan aekikhkarsrangkhlassahrbthahnathiepnxaaedpetxrsamarththaidsxngwithidngniwithikaraebb inheritance ihkhlasxaaedpetxrsbkhlascakkhlasthicathukichngancringaelaihximphliemnthxinetxrefstamthikhlasphueriykkhadhwng khwamsmphnthrahwangphuaeplngaelaphuthukaeplngcaepninaebb is a dngphaphprakxb khlasidxaaekrmkhxngxaaedpetxraephtethirnchnid inheritance inaebb UML withikaraebb delegation ihkhlasxaaedpetxrximphliemnthxinetxrefstamthikhlasphueriykkhadhwngaetimtxngsbkhlas khlasxaaedpetxreriykemthxdkhxngkhlasthicathukichnganphanthangxxbecktkhxngkhlasthithukichngannn khwamsmphnthrahwangphuaeplngaelaphuthukaeplngcaepninaebb has a dngphaph khlasidxaaekrmkhxngxaaedpetxraephtethirnchnid delegation inaebb UMLtwxyangopraekrm aekikhphasacawa aekikh sxrsokhdxaaedpetxraebb inheritance public class Adapter extends Adaptee implements ExpectedInterface public void doWork actualDoWork sxrsokhdxaaedpetxraebb delegation public class Adapter implements ExpectedInterface private Adaptee adaptee public void doWork adaptee actualDoWork xinetxrefsthiokhdphueriykkhadhwng public interface ExpectedInterface public void doWork khlasthithukaeplng public class Adaptee public void actualDoWork do work kareriykichngan expectedInterface doWork 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 aekikhAdopt Adapter ody David Geary xngkvs Design Patterns in Java Adapter ody Steven John Metsker xngkvs xaaedpetxraephtethirn ody xnuphakh matrmul ithy ekhathungcak https th wikipedia org w index php title xaaedpetxraephtethirn amp oldid 9350144, wikipedia, วิกิ หนังสือ, หนังสือ, ห้องสมุด,

บทความ

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