fbpx
วิกิพีเดีย

อินเทอร์พรีเตอร์แพตเทิร์น

อินเทอร์พรีเตอร์แพตเทิร์น (Interpreter pattern) เป็นดีไซน์แพตเทิร์น ที่ใช้ในกรณีที่ขอบเขตของปัญหาสามารถถูกอธิบายในลักษณะที่เป็นภาษาและกฎที่แน่นอนตายตัว เราสามารถสร้างคลาสที่เป็นตัวแทนขององค์ประกอบของภาษา จัดเรียงเข้าด้วยกันเพื่ออธิบายปัญหาหนึ่งๆ และแปลหรือประเมินผลโดยใช้โครงสร้างที่ได้จัดเรียงไว้นั้น ตัวอย่างเช่นสูตรคณิตศาสตร์ต่างๆ หรือยกตัวอย่างให้เฉพาะเจาะจงเช่น สูตรเรขาคณิตสำหรับหาพื้นที่รูปสามเหลี่ยม

พื้นที่รูปสามเหลี่ยม = 1/2 * ความยาวฐาน * ความสูง

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

จากการที่ปัญหาส่วนใหญ่เมื่อถูกอธิบายในแบบที่เป็นนิยามภาษาจะมีลักษณะที่เป็น recursive คือปัญหาใหญ่ๆ จะถูกแก้ได้โดยการแบ่งแยกเป็นปัญหาย่อยๆ และแก้ปัญหาย่อยๆ ลงไปตามลำดับๆ จนในที่สุดถึงปัญหาที่ไม่จำเป็นต้องแบ่งย่อยอีก ทำให้องค์ประกอบของอินเทอร์พรีเตอร์แพตเทิร์นมีลักษณะเดียวกับคอมโพสิตแพตเทิร์น

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

  • อินเตอร์เฟสหรือคลาสแบบแอ็บสแตรคเป็นแม่แบบที่นิยามพฤติกรรมที่แต่ละอ็อบเจกต์ของอินเทอร์พรีเตอร์ต้องมี
  • คลาสที่สามารถมีองค์ประกอบอื่นของอินเทอร์พรีเตอร์ได้
  • คลาสที่เป็นส่วนปลาย ไม่สามารถมีองค์ประกอบอื่นได้
  • คลาสสำหรับเป็นบริบท (context) หรือองค์ประกอบที่ใช้ในการประเมินผล
 
UML คลาสไดอะแกรมของอินเทอร์พรีเตอร์แพตเทิร์น

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

ภาษาจาวา

ตัวอย่างโปรแกรมเพื่อคำนวณหาพื้นที่รูปสามเหลี่ยมโดยสูตร พื้นที่รูปสามเหลี่ยม = 1/2 * ความยาวฐาน * ความสูง

อินเตอร์เฟส Expression เป็นแม่แบบของส่วนประกอบต่างๆ ของคอมโพสิต นิยามเมธอด evaluate() เพื่อให้แต่ละองค์ประกอบประเมินผลในส่วนของตัวเองและองค์ประกอบย่อยที่อยู่ภายใต้ ในที่นี้เราใช้ HashMap เป็น context เพื่อส่งผ่านค่าคัวแปรไปให้คลาสชนิด Variable ใช้ในการประเมินผล

import java.util.HashMap; public interface Expression { public double evaluate(HashMap<String, Double> context) throws Exception; } 

คลาส Multiplication เปรียบเสมือนเครื่องหมายคูณ สามารถมีองค์ประกอบย่อยได้สองตัวคือตัวคูณข้างซ้ายและตัวคูณข้างขวา

import java.util.HashMap; public class Multiplication implements Expression { private final Expression operand1; private final Expression operand2; public Multiplication(Expression operand1, Expression operand2) { this.operand1 = operand1; this.operand2 = operand2; } public double evaluate(HashMap<String, Double> context) throws Exception { return operand1.evaluate(context) * operand2.evaluate(context); } } 

คลาส Division เปรียบเสมือนเครื่องหมายหาร สามารถมีองค์ประกอบย่อยได้สองตัวคือตัวถูกหารและตัวหาร

import java.util.HashMap; public class Division implements Expression { private final Expression operand1; private final Expression operand2; public Division(Expression operand1, Expression operand2) { this.operand1 = operand1; this.operand2 = operand2; } public double evaluate(HashMap<String, Double> context) throws Exception { return operand1.evaluate(context) / operand2.evaluate(context); } } 

คลาส Constant เปรียบเสมือนค่าคงที่ในสมการ ไม่มีองค์ประกอบย่อย

import java.util.HashMap; public class Constant implements Expression { private final double value; public Constant(double value) { this.value = value; } public double evaluate(HashMap<String, Double> context) { return value; } } 

คลาส Variable เปรียบเสมือนตัวแปรในสมการ มีหน้าที่หาค่าของตนเองจาก context ไม่มีองค์ประกอบย่อย

import java.util.HashMap; public class Variable implements Expression { private final String name; public Variable(String name) { this.name = name; } public double evaluate(HashMap<String, Double> context) throws Exception { Double value = context.get(name); if (value == null)  throw new Exception("Unbound variable: " + name); return value; } } 

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

HashMap<String, Double> context = new HashMap<String, Double>(); context.put("base", 10.0); context.put("height", 5.0); Expression two = new Constant(2); Expression base = new Variable("base"); Expression height = new Variable("height"); Expression multi = new Multiplication(base, height); Expression triangleArea = new Division(multi, two); double result = triangleArea.evaluate(context); 

อ้างอิง

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

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

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

นเทอร, พร, เตอร, แพตเท, interpreter, pattern, เป, นด, ไซน, แพตเท, ใช, ในกรณ, ขอบเขตของป, ญหาสามารถถ, กอธ, บายในล, กษณะท, เป, นภาษาและกฎท, แน, นอนตายต, เราสามารถสร, างคลาสท, เป, นต, วแทนขององค, ประกอบของภาษา, ดเร, ยงเข, าด, วยก, นเพ, ออธ, บายป, ญหาหน, งๆ, และแป. xinethxrphrietxraephtethirn Interpreter pattern epndiisnaephtethirn thiichinkrnithikhxbekhtkhxngpyhasamarththukxthibayinlksnathiepnphasaaelakdthiaennxntaytw erasamarthsrangkhlasthiepntwaethnkhxngxngkhprakxbkhxngphasa cderiyngekhadwyknephuxxthibaypyhahnung aelaaeplhruxpraeminphlodyichokhrngsrangthiidcderiyngiwnn twxyangechnsutrkhnitsastrtang hruxyktwxyangihechphaaecaacngechn sutrerkhakhnitsahrbhaphunthirupsamehliymphunthirupsamehliym 1 2 khwamyawthan khwamsung enuxha 1 karnaipichngan 2 twxyangopraekrm 2 1 phasacawa 3 xangxing 4 aehlngkhxmulxunkarnaipichngan aekikhcakkarthipyhaswnihyemuxthukxthibayinaebbthiepnniyamphasacamilksnathiepn recursive khuxpyhaihy cathukaekidodykaraebngaeykepnpyhayxy aelaaekpyhayxy lngiptamladb cninthisudthungpyhathiimcaepntxngaebngyxyxik thaihxngkhprakxbkhxngxinethxrphrietxraephtethirnmilksnaediywkbkhxmophsitaephtethirnxngkhprakxbkhxngxinethxrphrietxraephtethirnmidngni xinetxrefshruxkhlasaebbaexbsaetrkhepnaemaebbthiniyamphvtikrrmthiaetlaxxbecktkhxngxinethxrphrietxrtxngmi khlasthisamarthmixngkhprakxbxunkhxngxinethxrphrietxrid khlasthiepnswnplay imsamarthmixngkhprakxbxunid khlassahrbepnbribth context hruxxngkhprakxbthiichinkarpraeminphl UML khlasidxaaekrmkhxngxinethxrphrietxraephtethirntwxyangopraekrm aekikhphasacawa aekikh twxyangopraekrmephuxkhanwnhaphunthirupsamehliymodysutr phunthirupsamehliym 1 2 khwamyawthan khwamsungxinetxrefs Expression epnaemaebbkhxngswnprakxbtang khxngkhxmophsit niyamemthxd evaluate ephuxihaetlaxngkhprakxbpraeminphlinswnkhxngtwexngaelaxngkhprakxbyxythixyuphayit inthinieraich HashMap epn context ephuxsngphankhakhwaepripihkhlaschnid Variable ichinkarpraeminphl import java util HashMap public interface Expression public double evaluate HashMap lt String Double gt context throws Exception khlas Multiplication epriybesmuxnekhruxnghmaykhun samarthmixngkhprakxbyxyidsxngtwkhuxtwkhunkhangsayaelatwkhunkhangkhwa import java util HashMap public class Multiplication implements Expression private final Expression operand1 private final Expression operand2 public Multiplication Expression operand1 Expression operand2 this operand1 operand1 this operand2 operand2 public double evaluate HashMap lt String Double gt context throws Exception return operand1 evaluate context operand2 evaluate context khlas Division epriybesmuxnekhruxnghmayhar samarthmixngkhprakxbyxyidsxngtwkhuxtwthukharaelatwhar import java util HashMap public class Division implements Expression private final Expression operand1 private final Expression operand2 public Division Expression operand1 Expression operand2 this operand1 operand1 this operand2 operand2 public double evaluate HashMap lt String Double gt context throws Exception return operand1 evaluate context operand2 evaluate context khlas Constant epriybesmuxnkhakhngthiinsmkar immixngkhprakxbyxy import java util HashMap public class Constant implements Expression private final double value public Constant double value this value value public double evaluate HashMap lt String Double gt context return value khlas Variable epriybesmuxntwaeprinsmkar mihnathihakhakhxngtnexngcak context immixngkhprakxbyxy import java util HashMap public class Variable implements Expression private final String name public Variable String name this name name public double evaluate HashMap lt String Double gt context throws Exception Double value context get name if value null throw new Exception Unbound variable name return value kareriykichngan HashMap lt String Double gt context new HashMap lt String Double gt context put base 10 0 context put height 5 0 Expression two new Constant 2 Expression base new Variable base Expression height new Variable height Expression multi new Multiplication base height Expression triangleArea new Division multi two double result triangleArea evaluate context 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 aekikhInterpreter ody Vince Huston xngkvs ekhathungcak https th wikipedia org w index php title xinethxrphrietxraephtethirn amp oldid 9350085, wikipedia, วิกิ หนังสือ, หนังสือ, ห้องสมุด,

บทความ

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