คุยกับผู้ใช้:Torpedo~thwiki
อะแด็ปเตอร์แพตเทิร์น (Adapter pattern) หรือ วาปเปอร์แพตเทิร์น (wrapper pattern) หรือเรียกสั้นๆว่า วาปเปอร (wrapper) เป็นดีไซน์แพตเทิร์นที่ใช้ในกรณีที่ รูปแบบการเชื่อมต่อของคลาสผู้เรียกและผู้ถูกเรียก ไม่สัมพันธ์กัน ดังนั้นจึงต้องใช้อะแด็ปเตอร์คลาสเข้ามาช่วยเป็นตัวกลาง ทำการเชื่อมต่อให้สามารถมาเรียกใช้งานกันได้
ตัวอย่างอะแด๊ปเตอร์แพตเทิร์นในขีวิตจริง[แก้]
ไฟล์:Adp1.jpg
จากรูป แสดงให้เห็นการใช้หลักการอะแด๊ปเตอร์ในชีวิตจริง เนื่องจากเต้ารับ กับเต้าเสียบมีอินเตอร์เฟสไม่เหมือนกัน ดังนั้นจึงใช้ตัวแปลงมาช่วย
โครงสร้างอะแด๊ปเตอร์แพตเทิร์น[แก้]
จากรูป ผู้เรียก (target) ต้องการเรียกใช้งาน เมทตอท SpecificRequset() แต่ไม่สามารถเรียกโดยตรงได้ เนื่องจากรูปแบบการเชื่อมต่อไม่สัมพันธ์กัน ดังนั้นจึงสร้าง คลาส Adapter มาเพื่อเรียกใช้งานผ่าน เมทตอท Request() ให้ไปเรียก เมทตอท SpecificRequest() อีกต่อหนึ่ง
ตัวอย่างการใช้อะแด๊ปเตอร์แพตเทิร์น[แก้]
ตัวอย่างที่โปรแกรมที่แสดง เป็นวิธีการนำอะแด๊ปเตอร์แพตเทิร์นมาใช้งานจริง โดยสมมุติว่า มีแลปทอปอยู่เครื่องหนึ่ง ในการเปิดใช้งานต้องใช้ไฟฟ้าแรงดัน 110 โวลต์ แต่ไฟฟ้าที่บ้านแรงดัน 220 โวลต์ ดังนั้นจึงต้องใช้อะแด๊ปเตอร์เข้ามาช่วย
class MainApp {
public static void main(String[] args) {
LapTop mylabtop = new LapTop(new Adapter(new Power(220)));
mylabtop.powerOn();
}
}
แสดงการ อินสแตทของ คลาส LabTop โดยจะทำการสร้างอินสแตทของคลาส Power กำหนดแรงดันให้เป็น 220 โวลต์ ขึ้นมาก่อน และนำอินสแตทที่ได้นั้นไปสร้าง อินสแตทของอะแด๊ปเตอร์เพื่อทำการแปลงแรงดันไฟฟ้าเป็น 110 โวลต์ จากนั้นนำอินสแตทของคลาสอะแด๊ปเตอร์ที่แปลงแล้ว ไปสร้างอินสแตทของ คลาส Laptop อีกที สุดท้ายสั่งเปิดเครื่อง หากใช้ขนาดแรงดันไฟฟ้าถูก จะแสดงคำว่า Power On ขึ้นมา
class LapTop {
private int laptopvoltage;
private Target target;
LapTop(Target t) {
target = t;
laptopvoltage = target.getVoltage();
}
public void powerOn() {
if(laptopvoltage == 110)
System.out.println("Power On"); {{ไม่ได้ลงชื่อ|Torpedo|21:50, 1 สิงหาคม 2551 (ICT)}}
else
System.out.println("Over Voltage");
}
}
แสดง คลาส LapTop ซึ่งจะไปเรียก เมทตอท getValtage() ผ่านตัวแปร target
abstract class Target {
abstract public int getVoltage();
}
class Adapter extends Target {
private Power adaptee;
public Adapter(final Power a) {
adaptee = a;
}
public int getVoltage() {
return adaptee.getVoltage()/2;
}
}
แสดง การสร้างคลาส Adapter ซึ่งสืบทอดมาจากแอปสแต๊กคลาส Target และทำการ override เมทตอท getVoltage ขึ้นมา ซึ่งทำหน้าที่แปลงแรงดันไฟฟ้า 220 โวลต์เป็น 110 โวลต์
class Power {
private int voltage;
Power(int voltage) {
this.voltage = voltage;
}
public int getVoltage() {
return voltage;
}
}
แสดง คลาส Power ซึ่งมีคอนสตัคเตอร์ ที่กำหนดค่าแรงดันไฟฟ้าได้ และมีเมทตอท getValtage ที่ส่งค่าระดับแรงดันไฟฟ้าออกมา
อ้างอิง[แก้]
แหล่งข้อมูลอื่น[แก้]
- http://www.vbdotnetheaven.com/UploadFile/rajeshvs/AdapterPattern05272005031848AM/AdapterPattern.aspx Adapter Pattern in VB.NET
- http://www.c-sharpcorner.com/UploadFile/rajeshvs/AdapterPatternInCS11142005005135AM/AdapterPatternInCS.aspx Adapter Pattern in C#
Your account will be renamed[แก้]
Hello,
The developer team at Wikimedia is making some changes to how accounts work, as part of our on-going efforts to provide new and better tools for our users like cross-wiki notifications. These changes will mean you have the same account name everywhere. This will let us give you new features that will help you edit and discuss better, and allow more flexible user permissions for tools. One of the side-effects of this is that user accounts will now have to be unique across all 900 Wikimedia wikis. See the announcement for more information.
Unfortunately, your account clashes with another account also called Torpedo. To make sure that both of you can use all Wikimedia projects in future, we have reserved the name Torpedo~thwiki that only you will have. If you like it, you don't have to do anything. If you do not like it, you can pick out a different name. If you think you might own all of the accounts with this name and this message is in error, please visit Special:MergeAccount to check and attach all of your accounts to prevent them from being renamed.
Your account will still work as before, and you will be credited for all your edits made so far, but you will have to use the new account name when you log in.
Sorry for the inconvenience.
Yours,
Keegan Peterzell
Community Liaison, Wikimedia Foundation
16:21, 20 มีนาคม 2558 (ICT)
Renamed[แก้]
This account has been renamed as part of single-user login finalisation. If you own this account you can log in using your previous username and password for more information. If you do not like this account's new name, you can choose your own using this form after logging in: พิเศษ:GlobalRenameRequest. -- Keegan (WMF) (talk)
19:46, 19 เมษายน 2558 (ICT)