it

Design Patterns คืออะไร? สอน 10 Design Patterns ที่ Developer ต้องรู้ พร้อมตัวอย่างโค้ด 2026

Design Patterns คืออะไร? สอน 10 Design Patterns ที่ Developer ต้องรู้ พร้อมตัวอย่างโค้ด 2026

Design Patterns คืออะไร?

Design Patterns คือแนวทางหรือรูปแบบการแก้ปัญหาที่ได้รับการพิสูจน์แล้วว่าใช้ได้ผลสำหรับปัญหาที่พบบ่อยในกระบวนการพัฒนาซอฟต์แวร์ มันเป็นเหมือน 'สูตรสำเร็จ' ที่ช่วยให้โปรแกรมเมอร์สามารถเขียนโค้ดที่มีคุณภาพสูง, บำรุงรักษาได้ง่าย และสามารถขยายต่อยอดได้ง่าย

ทำไม Developer ต้องรู้ Design Patterns?

  • ลดเวลาในการพัฒนา: ใช้รูปแบบที่ผ่านการทดสอบแล้ว ไม่ต้องคิดค้นใหม่จากศูนย์
  • เพิ่มคุณภาพโค้ด: โครงสร้างโค้ดที่ดีกว่า ลดข้อผิดพลาดที่อาจเกิดขึ้น
  • บำรุงรักษาได้ง่าย: โค้ดที่อ่านง่าย ปรับเปลี่ยนได้ง่ายเมื่อต้องการ
  • สื่อสารกันได้ดีขึ้น: ใช้ภาษากลางที่ Developer ทุกคนเข้าใจ
  • ขยายต่อยอดได้ง่าย: โครงสร้างที่ยืดหยุ่น รองรับการเปลี่ยนแปลงในอนาคต

10 Design Patterns ที่ Developer ต้องรู้

1. Singleton Pattern (Creational)

คำอธิบาย: สร้างวัตถุเพียงตัวเดียวในคลาส และให้การเข้าถึงจากทุกที่

public class Singleton {

    private static Singleton instance;



    private Singleton() {}



    public static Singleton getInstance() {

        if (instance == null) {

            instance = new Singleton();

        }

        return instance;

    }

}

2. Factory Method Pattern (Creational)

คำอธิบาย: กำหนดสัญญาในการสร้างวัตถุ โดยไม่ระบุคลาสที่สร้าง

เนื้อหาเกี่ยวข้อง — JavaScript Deno Deploy Code Review Best Practice

public interface Product {

    void use();

}



public class ConcreteProduct implements Product {

    public void use() {

        System.out.println("Using Concrete Product");

    }

}



public class Creator {

    public Product createProduct() {

        return new ConcreteProduct();

    }

}

3. Abstract Factory Pattern (Creational)

คำอธิบาย: สร้างครอบครัวของวัตถุที่เกี่ยวข้องกันหรือขึ้นอยู่กัน ไม่ระบุคลาสเฉพาะ

public interface Button {

    void render();

}



public class WindowsButton implements Button {

    public void render() {

        System.out.println("Rendering Windows Button");

    }

}



public class MacButton implements Button {

    public void render() {

        System.out.println("Rendering Mac Button");

    }

}



public interface GUIFactory {

    Button createButton();

}



public class WindowsFactory implements GUIFactory {

    public Button createButton() {

        return new WindowsButton();

    }

}



public class MacFactory implements GUIFactory {

    public Button createButton() {

        return new MacButton();

    }

}

4. Builder Pattern (Creational)

คำอธิบาย: สร้างวัตถุที่ซับซ้อนโดยการแยกกระบวนการสร้างออกจากวัตถุที่สร้าง

แนะนำเพิ่มเติม — อีบุ๊กการลงทุน SiamCafeBook

เนื้อหาเกี่ยวข้อง — แนะนำให้อ่าน Linux io_uring Clean Architecture

public class Product {

    private String part1;

    private String part2;



    public void setPart1(String part1) {

        this.part1 = part1;

    }



    public void setPart2(String part2) {

        this.part2 = part2;

    }



    public void show() {

        System.out.println("Part1: " + part1 + " Part2: " + part2);

    }

}



public interface Builder {

    void buildPart1();

    void buildPart2();

    Product getResult();

}



public class ConcreteBuilder implements Builder {

    private Product product = new Product();



    public void buildPart1() {

        product.setPart1("Part1");

    }



    public void buildPart2() {

        product.setPart2("Part2");

    }



    public Product getResult() {

        return product;

    }

}



public class Director {

    public void construct(Builder builder) {

        builder.buildPart1();

        builder.buildPart2();

    }

}

5. Prototype Pattern (Creational)

คำอธิบาย: สร้างวัตถุใหม่โดยการคัดลอกวัตถุที่มีอยู่

public class Prototype implements Cloneable {

    private String data;



    public Prototype(String data) {

        this.data = data;

    }



    public Prototype clone() {

        try {

            return (Prototype) super.clone();

        } catch (CloneNotSupportedException e) {

            throw new RuntimeException(e);

        }

    }



    public void setData(String data) {

        this.data = data;

    }



    public String getData() {

        return data;

    }

}

6. Adapter Pattern (Structural)

คำอธิบาย: แปลงอินเทอร์เฟซของคลาสหนึ่งให้เป็นอินเทอร์เฟซที่เป็นที่รู้จักอีกอินเทอร์เฟซหนึ่ง

public interface Target {

    void request();

}



public class Adaptee {

    public void specificRequest() {

        System.out.println("Specific Request");

    }

}



public class Adapter extends Adaptee implements Target {

    public void request() {

        specificRequest();

    }

}

7. Decorator Pattern (Structural)

คำอธิบาย: ขยายความสามารถของคลาสที่มีอยู่โดยการใส่คลาสอื่น

แนะนำเพิ่มเติม — เรียนเทรดกับ iCafeForex

เนื้อหาเกี่ยวข้อง — บทความที่เกี่ยวข้อง: eBPF Networking Interview Preparation

public interface Component {

    void operation();

}



public class ConcreteComponent implements Component {

    public void operation() {

        System.out.println("Concrete Component Operation");

    }

}



public abstract class Decorator implements Component {

    protected Component component;



    public Decorator(Component component) {

        this.component = component;

    }



    public void operation() {

        component.operation();

    }

}



public class ConcreteDecorator extends Decorator {

    public ConcreteDecorator(Component component) {

        super(component);

    }



    public void operation() {

        super.operation();

        System.out.println("Decorator Operation");

    }

}

8. Observer Pattern (Behavioral)

คำอธิบาย: สร้างความสัมพันธ์ระหว่างวัตถุที่สังเกตการณ์และวัตถุที่ถูกสังเกตการณ์

public interface Observer {

    void update(String message);

}



public class ConcreteObserver implements Observer {

    public void update(String message) {

        System.out.println("Received: " + message);

    }

}



public class Subject {

    private List observers = new ArrayList<>();



    public void addObserver(Observer observer) {

        observers.add(observer);

    }



    public void notifyObservers(String message) {

        for (Observer observer : observers) {

            observer.update(message);

        }

    }

}

9. Strategy Pattern (Behavioral)

คำอธิบาย: กำหนดกลยุทธ์ที่แยกจากวัตถุที่ใช้กลยุทธ์

เนื้อหาเกี่ยวข้อง — แนะนำให้อ่าน Kubernetes Admission Webhook Blue Green Canary

public interface Strategy {

    void execute();

}



public class ConcreteStrategyA implements Strategy {

    public void execute() {

        System.out.println("Executing Strategy A");

    }

}



public class ConcreteStrategyB implements Strategy {

    public void execute() {

        System.out.println("Executing Strategy B");

    }

}



public class Context {

    private Strategy strategy;



    public void setStrategy(Strategy strategy) {

        this.strategy = strategy;

    }



    public void executeStrategy() {

        strategy.execute();

    }

}

10. Command Pattern (Behavioral)

คำอธิบาย: แปลงคำขอเป็นวัตถุ ทำให้สามารถเพิ่มคำขอใหม่, รันคำขอ, ยกเลิกคำขอ, และตรวจสอบคำขอได้

public interface Command {

    void execute();

}



public class ConcreteCommand implements Command {

    private Receiver receiver;



    public ConcreteCommand(Receiver receiver) {

        this.receiver = receiver;

    }



    public void execute() {

        receiver.action();

    }

}



public class Receiver {

    public void action() {

        System.out.println("Receiver Action");

    }

}



public class Invoker {

    private Command command;



    public void setCommand(Command command) {

        this.command = command;

    }



    public void executeCommand() {

        command.execute();

    }

}

สรุป

Design Patterns เป็นเครื่องมือที่มีประโยชน์อย่างมากสำหรับ Developer ในการพัฒนาซอฟต์แวร์ที่มีคุณภาพสูง, บำรุงรักษาได้ง่าย และสามารถขยายต่อยอดได้ง่าย การเรียนรู้และนำ Design Patterns มาใช้ จะช่วยให้ Developer สามารถพัฒนาซอฟต์แวร์ได้อย่างมีประสิทธิภาพและมีประสิทธิผลมากยิ่งขึ้น

XM Legend · เทรดเดอร์ & ผู้สอน Forex 13 ปี

ผู้ก่อตั้ง SiamCafe ตั้งแต่ปี 1997 · เทรดเดอร์สาย Forex มากกว่า 13 ปี ได้รับการยกย่องเป็น XM Legend · แบ่งปันความรู้ Forex, ไอที, AI และการเทรด จากประสบการณ์จริงในตลาดจริง