0Pricing
Clean Architecture & Design Patterns in Practice · บทเรียน

รูปแบบ Facade และ Proxy

ลดความซับซ้อนของระบบย่อยด้วย Facade และควบคุมการเข้าถึงออบเจ็กต์ด้วยรูปแบบ Proxy

รูปแบบ Facade และ Proxy เป็นบทเรียน Clean Architecture & Design Patterns in Practice ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Clean Architecture & Design Patterns in Practice และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Clean Architecture & Design Patterns in Practice มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Welcome to Facade & Proxy!

In this lesson, we'll dive into two powerful Structural Design Patterns: the Facade and Proxy patterns.

Structural patterns help you compose objects and classes into larger structures, making systems more flexible and efficient.

We'll learn how Facade simplifies complex systems and how Proxy controls access to objects.

Meet the Facade Pattern

Imagine a complex machine with many buttons and levers. The Facade pattern provides a simple, unified interface to a set of interfaces in a subsystem.

  • It defines a higher-level interface that makes the subsystem easier to use.
  • Think of it as a simplified front panel for a complicated system.

Its main goal is to reduce complexity and decouple the client from the subsystem's inner workings.

Dealing with Complexity

Without a Facade, a client might need to interact with many different classes and objects within a subsystem to perform a single task.

This leads to:

  • Tight coupling: Client code becomes dependent on all subsystem components.
  • Increased complexity: Clients need to know too much about the subsystem's internal structure.
  • Harder maintenance: Changes in the subsystem's internal parts might break client code.

How Facade Simplifies

The Facade pattern introduces a single Facade class that wraps the complex subsystem.

The Facade class:

  • Knows which subsystem classes are responsible for a request.
  • Delegates client requests to the appropriate subsystem objects.
  • Converts complex interactions into a simple method call.

Clients interact only with the Facade, not the individual subsystem classes.

Facade in Action: Home Theater

Let's see a simple example with a home theater system. Instead of turning on the TV, amplifier, and DVD player separately, a Facade does it all.

public class Amplifier {
  public void on() { System.out.println("Amplifier On"); }
  public void setDvd(DvdPlayer dvd) { System.out.println("Amplifier setting DVD"); }
  public void setVolume(int volume) { System.out.println("Amplifier volume " + volume); }
  public void off() { System.out.println("Amplifier Off"); }
}

public class DvdPlayer {
  public void on() { System.out.println("DVD Player On"); }
  public void play(String movie) { System.out.println("Playing movie: " + movie); }
  public void off() { System.out.println("DVD Player Off"); }
}

public class Television {
  public void on() { System.out.println("TV On"); }
  public void off() { System.out.println("TV Off"); }
}

// The Facade
public class HomeTheaterFacade {
  Amplifier amp;
  DvdPlayer dvd;
  Television tv;

  public HomeTheaterFacade(Amplifier amp, DvdPlayer dvd, Television tv) {
    this.amp = amp;
    this.dvd = dvd;
    this.tv = tv;
  }

  public void watchMovie(String movie) {
    System.out.println("Get ready to watch a movie...");
    tv.on();
    amp.on();
    amp.setDvd(dvd);
    amp.setVolume(5);
    dvd.on();
    dvd.play(movie);
  }

  public void endMovie() {
    System.out.println("Shutting down home theater...");
    dvd.off();
    amp.off();
    tv.off();
  }
}

public class Main {
  public static void main(String[] args) {
    Amplifier amp = new Amplifier();
    DvdPlayer dvd = new DvdPlayer();
    Television tv = new Television();

    HomeTheaterFacade homeTheater = new HomeTheaterFacade(amp, dvd, tv);
    homeTheater.watchMovie("Inception");
    System.out.println("---");
    homeTheater.endMovie();
  }
}

Why Use Facade?

The Facade pattern offers several key advantages:

  • Simplifies client code: Clients don't need to learn the complex API of the subsystem.
  • Decouples client from subsystem: Reduces dependencies, making the system more robust to changes.
  • Improves testability: You can mock the Facade for easier testing of client code.
  • Promotes layering: Helps structure a system into layers with clear responsibilities.

Introducing the Proxy Pattern

The Proxy pattern provides a surrogate or placeholder for another object to control access to it.

Think of it as a middleman. Instead of talking directly to the real object, you talk to its proxy.

The proxy can add extra logic before or after accessing the real object, like security checks, lazy loading, or logging.

Common Proxy Applications

Proxies are versatile and used in many scenarios:

  • Protection Proxy: Controls access based on permissions (e.g., only authenticated users can access).
  • Virtual Proxy: Creates expensive objects on demand (lazy loading), improving performance.
  • Remote Proxy: Represents an object located in a different address space (e.g., network proxy for a remote service).
  • Logging Proxy: Logs method calls to the real object.

Proxy in Action: Secure Access

Here's an example of a Protection Proxy. A user tries to access a sensitive database. The proxy checks if the user has admin rights first.

// Subject Interface
interface Database {
  void executeQuery(String query);
}

// Real Subject
class RealDatabase implements Database {
  public void executeQuery(String query) {
    System.out.println("Executing query: " + query);
  }
}

// Proxy
class DatabaseProxy implements Database {
  private RealDatabase realDatabase;
  private String userRole;

  public DatabaseProxy(String userRole) {
    this.userRole = userRole;
    this.realDatabase = new RealDatabase(); // Create real object if needed, or lazily
  }

  public void executeQuery(String query) {
    if (userRole.equals("ADMIN")) {
      System.out.println("Proxy: Admin access granted.");
      realDatabase.executeQuery(query);
    } else {
      System.out.println("Proxy: Access denied! Only ADMIN can execute queries.");
    }
  }
}

public class Main {
  public static void main(String[] args) {
    Database adminUser = new DatabaseProxy("ADMIN");
    adminUser.executeQuery("SELECT * FROM sensitive_data");

    System.out.println("---");

    Database regularUser = new DatabaseProxy("USER");
    regularUser.executeQuery("DELETE FROM sensitive_data");
  }
}

Why Use Proxy?

The Proxy pattern offers significant benefits:

  • Controlled Access: Can restrict or enhance access to the real object.
  • Lazy Initialization: Delays object creation until it's actually needed, saving resources.
  • Remote Access Abstraction: Hides the complexities of accessing objects in different locations.
  • Added Functionality: Can inject logging, caching, or security checks without modifying the real object.

Quick Check: Facade vs. Proxy

Consider a scenario where you have a complex image processing library with dozens of classes and methods. You want to provide a simple function like processImage(imageFile) to your users.

Which design pattern would be most suitable to achieve this simplification?

Facade & Proxy: Key Takeaways

We've explored two powerful structural patterns:

  • The Facade pattern provides a simplified, high-level interface to a complex subsystem, making it easier to use and reducing coupling.
  • The Proxy pattern provides a surrogate or placeholder for another object to control access to it, adding logic like security, lazy loading, or logging.

Both patterns enhance system structure and maintainability by managing complexity and controlling interactions between objects.

คำถามที่พบบ่อย

บทเรียน “รูปแบบ Facade และ Proxy” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “รูปแบบ Facade และ Proxy” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Clean Architecture & Design Patterns in Practice ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Clean Architecture & Design Patterns in Practice มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “รูปแบบ Facade และ Proxy”

ลดความซับซ้อนของระบบย่อยด้วย Facade และควบคุมการเข้าถึงออบเจ็กต์ด้วยรูปแบบ Proxy คุณปฏิบัติ Clean Architecture & Design Patterns in Practice ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Clean Architecture & Design Patterns in Practice หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Clean Architecture & Design Patterns in Practice บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “รูปแบบ Facade และ Proxy” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Clean Architecture & Design Patterns in Practice นี้ได้ไหม

ได้ บทเรียน Clean Architecture & Design Patterns in Practice ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. รูปแบบ Adapter และ Decorator
  2. รูปแบบ Facade และ Proxy
  3. รูปแบบ Composite และ Bridge
  4. รูปแบบ Flyweight เพื่อประสิทธิภาพหน่วยความจำ
← กลับไปที่ Clean Architecture & Design Patterns in Practice