fbpx
วิกิพีเดีย

เทมเพลทเมธอดแพตเทิร์น

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

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

 
UML โครงสร้างของเทมเพลตเมธอด

องค์ประกอบของเทมเพลตเมธอดแพตเทิร์นมีดังนี้

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

ตัวอย่างโค้ด

ภาษาจาวา

ตัวอย่างโปรแกรมคำนวณหาปริมาตรของรูปทรงกระบอกและรูปทรงสี่เหลี่ยมซึ่งมีสูตรร่วมกันดังนี้

ปริมาตร = พื้นที่ฐาน * ความสูง 

โดยที่

  • คำนวณหาปริมาตรในเทมเพลตเมธอดของคลาสนามธรรม
  • พื้นที่ฐานของทรงกระบอกและทรงสี่เหลี่ยมมีวิธีการหาที่แตกต่างกัน อยู่ในเมธอดของซับคลาสรูปธรรม

คลาสนามธรรม ThreeDShape นิยามเมธอด getBaseArea () getHeight () และ getVolume () ซึ่งเป็นเทมเพลตเมธอดมีรายละเอียดการคำนวณหาปริมาตร

public abstract class ThreeDShape { public abstract double getBaseArea () ; public abstract double getHeight () ; /**  * The Template Method  */ public double getVolume () { return getBaseArea () * getHeight () ; } } 

คลาสรูปธรรม Cylinder กำหนดวิธีการหาพื้นที่ฐานซึ่งเป็นรูปวงกลมในเมธอด getBaseArea ()

public class Cylinder extends ThreeDShape { private final double radius; private final double height; public Cylinder (double radius, double height) { this.radius = radius; this.height = height; } @Override public double getBaseArea () { return Math.PI * radius * radius; } @Override public double getHeight () { return height; } } 

คลาสรูปธรรม Box กำหนดวิธีการหาพื้นที่ฐานซึ่งเป็นรูปสี่เหลี่ยมในเมธอด getBaseArea ()

public class Box extends ThreeDShape { private final double width; private final double length; private final double height; public Box (double width, double length, double height) { this.width = width; this.length = length; this.height = height; } @Override public double getBaseArea () { return width * length; } @Override public double getHeight () { return height; } } 

อ้างอิง

  • 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 (อังกฤษ)

เทมเพลทเมธอดแพตเท, เทมเพลตเมธอดแพตเท, งกฤษ, template, method, pattern, เป, นด, ไซน, แพตเท, นท, ใช, กำหนดแม, แบบหร, อโครงหล, กของข, นตอนว, โดยไม, ได, กำหนดรายละเอ, ยดของข, นตอนต, างๆ, บคลาสสามารถน, ยามรายละเอ, ยดของแต, ละข, นตอนย, อยได, ตามความต, องการ, เทมเพลต. ethmephltemthxdaephtethirn xngkvs Template method pattern epndiisnaephtethirnthiichkahndaemaebbhruxokhrnghlkkhxngkhntxnwithi odyimidkahndraylaexiydkhxngkhntxntang sbkhlassamarthniyamraylaexiydkhxngaetlakhntxnyxyidtamkhwamtxngkar ethmephltemthxdthukichepnswnhlkkhxngsxftaewrefrmewirk twxyangthinxkehnuxcakkarekhiynopraekrmechn aebbaeplnbanthiecakhxngbansamartheluxksiaelatkaetngidphayhlng enuxha 1 karnaipichngan 2 twxyangokhd 2 1 phasacawa 3 xangxing 4 aehlngkhxmulxunkarnaipichngan aekikh UML okhrngsrangkhxngethmephltemthxd xngkhprakxbkhxngethmephltemthxdaephtethirnmidngni khlasnamthrrm Abstract class epnphukahndokhrnghlkinkarthangan kahndkhntxnyxykhxngkarthanganodyimkahndraylaexiyd odysrangepnemthxdnamthrrm Abstract method hruxxaccakahndkarthanganphunthaniwkid srangethmephltemthxdthimiokhrngkhxngkhntxnwithiaelaeriykichkhntxnyxythiidniyamiwaelwepnemthxdnamthrrm khlasrupthrrm Concrete class sbkhlaskhxngkhlasnamthrrm kahndraylaexiydkhxngkhntxnyxythithukniyaminkhlasaemtwxyangokhd aekikhphasacawa aekikh twxyangopraekrmkhanwnhaprimatrkhxngrupthrngkrabxkaelarupthrngsiehliymsungmisutrrwmkndngni primatr phunthithan khwamsung odythi khanwnhaprimatrinethmephltemthxdkhxngkhlasnamthrrm phunthithankhxngthrngkrabxkaelathrngsiehliymmiwithikarhathiaetktangkn xyuinemthxdkhxngsbkhlasrupthrrmkhlasnamthrrm ThreeDShape niyamemthxd getBaseArea getHeight aela getVolume sungepnethmephltemthxdmiraylaexiydkarkhanwnhaprimatr public abstract class ThreeDShape public abstract double getBaseArea public abstract double getHeight The Template Method public double getVolume return getBaseArea getHeight khlasrupthrrm Cylinder kahndwithikarhaphunthithansungepnrupwngklminemthxd getBaseArea public class Cylinder extends ThreeDShape private final double radius private final double height public Cylinder double radius double height this radius radius this height height Override public double getBaseArea return Math PI radius radius Override public double getHeight return height khlasrupthrrm Box kahndwithikarhaphunthithansungepnrupsiehliyminemthxd getBaseArea public class Box extends ThreeDShape private final double width private final double length private final double height public Box double width double length double height this width width this length length this height height Override public double getBaseArea return width length Override public double getHeight return height 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 aekikhTemplate Method ody Vince Huston xngkvs ekhathungcak https th wikipedia org w index php title ethmephlthemthxdaephtethirn amp oldid 9549859, wikipedia, วิกิ หนังสือ, หนังสือ, ห้องสมุด,

บทความ

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