fbpx
วิกิพีเดีย

แฟกทอรีเมธอดแพตเทิร์น

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

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

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

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

  • ผลิตภัณฑ์ (Product) นิยามอินเตอร์เฟสของของอ็อบเจกต์ที่แฟกทอรีเมธอดจะเป็นผู้สร้าง
  • ผลิตภัณฑ์รูปธรรม (Concrete product) ตัวคลาสที่แท้จริงที่จะถูกสร้างโดยแฟกทอรีเมธอด
  • คลาสนามธรรม (Abstract class) เป็นผู้กำหนดแฟกทอรีเมธอด โดยสร้างเป็นเมธอดนามธรรม (Abstract method)
  • คลาสรูปธรรม (Concrete class) ซับคลาสของคลาสนามธรรม กำหนดรายละเอียดของแฟกทอรีเมธอดที่ถูกนิยามในคลาสแม่

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

ภาษาจาวา

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

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

โดยที่

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

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

public abstract class Container { /**  * Factory method  */ public abstract TwoDShape createBase(); public abstract double getHeight(); /**  * Template method  */ public double getVolume() { return createBase().getArea() * getHeight(); } } 

คลาสรูปธรรม Cylinder สร้างรูปวงกลม Circle ในแฟกทอรีเมธอด createBase()

public class Cylinder extends Container { private double radius; private double height; public Cylinder(double radius, double height) { this.radius = radius; this.height = height; } @Override public TwoDShape createBase() { return new Circle(radius); } @Override public double getHeight() { return height; } } 

คลาส TwoDShape และ Circle

public interface TwoDShape { public double getArea(); } public class Circle implements TwoDShape { private double radius; public Circle(double radius) { this.radius = radius; } public double getArea() { return Math.PI * radius * radius; } } 

อ้างอิง

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

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

  • Factory Method โดย Vince Huston (อังกฤษ)
  • Pattern Summaries: Factory Method โดย Mark Grand (อังกฤษ)
  • The Factory Method (Creational) Design Pattern โดย Gopalan Suresh Raj (อังกฤษ)
  • Factory Method Pattern โดย OOSE Student (ไทย)

แฟกทอร, เมธอดแพตเท, factory, method, pattern, เป, นด, ไซน, แพตเท, นท, ยามเมธอดสำหร, บสร, างอ, อบเจกต, ของคลาสชน, ดหน, งๆ, แต, ไม, ได, เจาะจงชน, ดท, แน, นอน, บคลาสเป, นผ, กำหนดเจาะจงว, าจะสร, างอ, อบเจกต, ของคลาสใด, แฟกทอร, เมธอดในความหมายโดยท, วไปค, อเมธอดท, ท. aefkthxriemthxdaephtethirn Factory method pattern epndiisnaephtethirnthiniyamemthxdsahrbsrangxxbecktkhxngkhlaschnidhnung aetimidecaacngchnidthiaennxn sbkhlasepnphukahndecaacngwacasrangxxbecktkhxngkhlasid aefkthxriemthxdinkhwamhmayodythwipkhuxemthxdthithahnathisrangxxbecktnnexng aefkthxriemthxdaephtethirncathukichrwmkbethmephltemthxdaephtethirn Template method pattern odythukeriykichngancakinethmephltemthxd enuxha 1 karnaipichngan 2 twxyangokhd 2 1 phasacawa 3 xangxing 4 aehlngkhxmulxunkarnaipichngan aekikh UML okhrngsrangkhxngethmephltemthxd xngkhprakxbkhxngaefkthxriemthxdaephtethirnmidngni phlitphnth Product niyamxinetxrefskhxngkhxngxxbecktthiaefkthxriemthxdcaepnphusrang phlitphnthrupthrrm Concrete product twkhlasthiaethcringthicathuksrangodyaefkthxriemthxd khlasnamthrrm Abstract class epnphukahndaefkthxriemthxd odysrangepnemthxdnamthrrm Abstract method khlasrupthrrm Concrete class sbkhlaskhxngkhlasnamthrrm kahndraylaexiydkhxngaefkthxriemthxdthithukniyaminkhlasaemtwxyangokhd aekikhphasacawa aekikh twxyangopraekrmkhanwnhaprimatrkhxngrupthrngkrabxksungmisutrdngni primatr phunthithan khwamsung odythi aefkthxriemthxdthahnathisrangrupwngklm primatrkhxngthrngkrabxkkhuxphlkhunkhxngkhwamsungaelaphunthithancakrupwngklmthithuksrangodyaefkthxriemthxdkhlasnamthrrm Container niyamaefkthxriemthxd createBase getHeight aela getVolume sungepnethmephltemthxdmiraylaexiydkarkhanwnhaprimatr public abstract class Container Factory method public abstract TwoDShape createBase public abstract double getHeight Template method public double getVolume return createBase getArea getHeight khlasrupthrrm Cylinder srangrupwngklm Circle inaefkthxriemthxd createBase public class Cylinder extends Container private double radius private double height public Cylinder double radius double height this radius radius this height height Override public TwoDShape createBase return new Circle radius Override public double getHeight return height khlas TwoDShape aela Circle public interface TwoDShape public double getArea public class Circle implements TwoDShape private double radius public Circle double radius this radius radius public double getArea return Math PI radius radius 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 aekikhFactory Method ody Vince Huston xngkvs Pattern Summaries Factory Method ody Mark Grand xngkvs The Factory Method Creational Design Pattern ody Gopalan Suresh Raj xngkvs Factory Method Pattern ody OOSE Student ithy ekhathungcak https th wikipedia org w index php title aefkthxriemthxdaephtethirn amp oldid 9352867, wikipedia, วิกิ หนังสือ, หนังสือ, ห้องสมุด,

บทความ

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