Factory Example

Die Factory ist ein Entwurfsmuster aus der Softwareentwicklung. Es beschreibt wie ein Objekt durch Aufruf einer Methode anstatt durch direkten Aufruf eines Konstruktors erzeugt wird.

class RunServiceFactory


package de.snowbits.factory;

import java.util.List;

public class RunServiceFactory {

	public static void main(String[] args) {
		send(ServiceType.A);
		send(ServiceType.B);
		
		/* debug output:
		handle for ServiceA: 1
		handle for ServiceA: 2
		handle for ServiceA: 3
		sent successfully: 3
		handle for ServiceB: Apfel
		handle for ServiceB: Birne
		handle for ServiceB: Pflaume
		handle for ServiceB: Kirsche
		sent successfully: 4		 
		 */
	}

	@SuppressWarnings({"unchecked", "rawtypes"})
	private static void send(ServiceType serviceType) {
		List list = ServiceFactory.load(serviceType);
		Integer result = ServiceFactory.getService(serviceType).send(list);
		
		if (result == list.size())
			System.out.println("sent successfully: " + result);
		else
			System.out.println("Send was not successful");
	}
	
}
	

class ServiceFactory


package de.snowbits.factory;

import java.util.List;

public class ServiceFactory {
	
	private ServiceFactory() {
	}

	public static List load(ServiceType serviceType) {
		switch (serviceType) {
		case A:
			return new LoaderA().load();
		case B:
			return new LoaderB().load();
		default:
			throw new IllegalArgumentException("Error this type is not supported: serviceType=" + serviceType);
		}
	}
	
	public static Service getService(ServiceType serviceType) {
		switch (serviceType) {
		case A:
			return new ServiceA();
		case B:
			return new ServiceB();
		default:
			throw new IllegalArgumentException("Error this type is not supported: serviceType=" + serviceType);
		}
	}
	
}
	

enum ServiceType


package de.snowbits.factory;

public enum ServiceType {
	A, B;
}
	

interface Loader


package de.snowbits.factory;

import java.util.List;

public interface Loader<T> {
	
	List<T> load();
}
	

interface Service


package de.snowbits.factory;

import java.util.List;

public interface Service<T> {
	
	int send(List<T> list);
}
	

class AbstractService


package de.snowbits.factory;

public abstract class AbstractService {
	
	protected void debug(Class<?> handleClass,String value) {
		System.out.println("handle for " + handleClass.getSimpleName() + ": " + value);
	}
}
	

class LoaderA


package de.snowbits.factory;

import java.util.ArrayList;
import java.util.List;

public class LoaderA implements Loader<Integer> {

	@Override
	public List<Integer> load() {
		List<Integer> result = new ArrayList<Integer>();
		result.add(10);
		result.add(20);
		result.add(30);
		
		return result;
	}

}
	

class LoaderB


package de.snowbits.factory;

import java.util.ArrayList;
import java.util.List;

public class LoaderB implements Loader<String> {

	@Override
	public List<String> load() {
		List<String> result = new ArrayList<String>();
		result.add("Apfel");
		result.add("Birne");
		result.add("Pflaume");
		result.add("Kirsche");
		
		return result;
	}

}
	

class ServiceA


package de.snowbits.factory;

import java.util.List;

public class ServiceA extends AbstractService implements Service<Integer> {

	@Override
	public int send(List<Integer> list) {
		int count = 0;
		
		for (Integer element : list) {
			debug(ServiceA.class, String.valueOf(element));
			count++;
		}
		
		return count;
	}

}
	

class ServiceB


package de.snowbits.factory;

import java.util.List;

public class ServiceB extends AbstractService implements Service<String> {

	@Override
	public int send(List<String> list) {
		int count = 0;
		
		for (String element : list) {
			debug(ServiceB.class, element);
			count++;
		}
		
		return count;
	}

}
	

Top


Example
Sitemap Kontakt Impressum