Design Patterns – 03 – Abstract Factory Design Pattern

Merhaba Arkadaslar
Bu bolumde  Abstract Factory Design Pattern‘i inceleyecegiz. Abstract Factory Design Pattern bir Creational Pattern’dir ve Factory of Factories olarak da ifade edilmektedir.

Gang of Four’un kitabinda Abstract Factory Design Pattern icin su tanim gecmektedir ;

Provides an interface for creating families of related or 
dependent objects without specifying their concrete classes.

Abstract Factory Design Pattern’in diagrami ;

AbstractFactory (WidgetFactory)

  • interface
  • Abstract metotlar tanimli
  • Metotlar geriye AbstractProduct donmektedir (Window , ScrollBar)

ConcreteFactory (MotifWidgetFactory, PMWidgetFactory)

  • AbstractFactory ‘yi implements etmektedir.
  • Concrete class
  • Metotlarda ilgili concrete product objeleri olusturulmaktadir.

AbstractProduct (Window, ScrollBar)

  • interface
  • Concrete product objeleri icin genel tip

ConcreteProduct (MotifWindow, MotifScrollBar)

  • AbstractProduct interface’ini implements eder
  • Concrete Product class

Client

  • Sadece AbstractFactory ve AbstractProduct siniflarini kullanir.

WidgetFactory.java
WidgetFactory interface’sini inceleyelim…
2 metot tanimladik geriye ScrollBar ve Window donmektedir.

package _03.abstract$.factory.pattern.factory;

import _03.abstract$.factory.pattern.model.ScrollBar;
import _03.abstract$.factory.pattern.model.Window;

public interface WidgetFactory {

	public abstract ScrollBar createScrollBar();

	public abstract Window createWindow();

}

ScrollBar ve Window’u interface olarak tanimliyoruz.
abstract olarak birer metot tanimladik.

ScrollBar.java

package _03.abstract$.factory.pattern.model;

public interface ScrollBar {

	public abstract void setMax(int max);
}

Window.java

package _03.abstract$.factory.pattern.model;

public interface Window {
	
	public void repaint();
}

Simdi de concrete siniflari tanimlayalim ;

PMScrollBar.java

package _03.abstract$.factory.pattern.model;

public class PMScrollBar implements ScrollBar {

	@Override
	public void setMax(int max) {
		System.out.println("PMScrollBar#setMax : " + max);
	}

}

MotifScrollBar.java

package _03.abstract$.factory.pattern.model;

public class MotifScrollBar implements ScrollBar {

	@Override
	public void setMax(int max) {
		System.out.println("MotifScrollBar#setMax : " + max);
	}

}

MotifWindow.java

package _03.abstract$.factory.pattern.model;

public class MotifWindow implements Window{

	@Override
	public void repaint() {
		System.out.println("MotifWindow#repaint");
	}

}

PMWindow.java

package _03.abstract$.factory.pattern.model;

public class PMWindow implements Window {

	@Override
	public void repaint() {
		System.out.println("PMWindow#repaint");
	}

}

Simdi de concrete factory class’larimizi tanimlayalim…

PMWidgetFactory.java

package _03.abstract$.factory.pattern.factory;

import _03.abstract$.factory.pattern.model.ScrollBar;
import _03.abstract$.factory.pattern.model.Window;
import _03.abstract$.factory.pattern.model.PMWindow;
import _03.abstract$.factory.pattern.model.PMScrollBar;

public class PMWidgetFactory implements WidgetFactory{

	@Override
	public ScrollBar createScrollBar() {
		return new PMScrollBar();
	}

	@Override
	public Window createWindow() {
		return new PMWindow();
	}

}

MotifWidgetFactory.java

package _03.abstract$.factory.pattern.factory;

import _03.abstract$.factory.pattern.model.ScrollBar;
import _03.abstract$.factory.pattern.model.Window;
import _03.abstract$.factory.pattern.model.MotifWindow;
import _03.abstract$.factory.pattern.model.MotifScrollBar;

public class MotifWidgetFactory implements WidgetFactory{

	@Override
	public ScrollBar createScrollBar() {
		return new MotifScrollBar();
	}

	@Override
	public Window createWindow() {
		return new MotifWindow();
	}

}

Son olarak test/client sinifimizi yazalim ;

AbstractFactoryPatternTest.java

package _03.abstract$.factory.pattern.test;

import _03.abstract$.factory.pattern.factory.WidgetFactory;
import _03.abstract$.factory.pattern.model.ScrollBar;
import _03.abstract$.factory.pattern.model.Window;
import _03.abstract$.factory.pattern.factory.MotifWidgetFactory;

public class AbstractFactoryPatternTest {

	public static void main(String[] args) {
		
		WidgetFactory factory = new MotifWidgetFactory();
		
		ScrollBar motifScrollBar=factory.createScrollBar();
		Window motifWindow = factory.createWindow();
		
		ScrollBar pmScrollBar = factory.createScrollBar();
		Window pmWindow = factory.createWindow();
		
		motifScrollBar.setMax(10);
		motifWindow.repaint();
		System.out.println();
		
		pmScrollBar.setMax(20);
		pmWindow.repaint();
		
	}
}

Ornegimizi calistirdigimizda;

MotifScrollBar#setMax : 10
MotifWindow#repaint

MotifScrollBar#setMax : 20
MotifWindow#repaint

Github kaynak kodlar / source folder
leventerguder/injavawetrust.designpattern

Yazimi burada sonlandiriyorum.
Herkese bol Javali gunler dilerim.
Be an oracle man , import java.*;
Levent Erguder
OCP, Java SE 6 Programmer
OCE, Java EE 6 Web Component Developer

Print Friendly, PDF & Email

Leave a Reply

Your email address will not be published.