3 Ekim 2023 Salı

GoF - Abstract Factory Örüntüsü

Örnek
Elimizde şöyle bir hiyerarşi olsun
// Abstract Product - Button
public interface Button {
  void render();
}

// Concrete Product - WindowsButton
public class WindowsButton implements Button {
  @Override
  public void render() {
    System.out.println("Rendering a Windows button");
  }
}

// Concrete Product - MacButton
public class MacButton implements Button {
  @Override
  public void render() {
    System.out.println("Rendering a Mac button");
  }
}
Şöyle yaparız
// Abstract Factory
public interface GUIFactory {
  Button createButton();
}

// Concrete Factory - WindowsFactory
public class WindowsFactory implements GUIFactory {
  @Override
  public Button createButton() {
    return new WindowsButton();
  }
}

// Concrete Factory - MacFactory
public class MacFactory implements GUIFactory {
  @Override
  public Button createButton() {
    return new MacButton();
  }
}


Hiç yorum yok:

Yorum Gönder