Design Patterns

Table of Contents

Creational
Abstract Factory
Builder
Factory Method
Prototype
Singleton

Structural
Adapter
Bridge
Composite
Decorator
Facade
Flyweight
Proxy

Behavioral
Chain of Responsibility
Command
Interpreter
Iterator
Mediator
Memento
Observer
State
Strategy
Template Method
Visitor

Java EE
Model View Controller
Business Delegate
Composite Entity
Data Access Object
Front Controller
Intercepting Filter
Service Locator
Transfer Object

UML & OOD
Unified Modeling Language
SOLID

Business Delegate


Business Delegate Pattern is used to decouple business tier and presentation tier. It is use to reduce communication or remote lookup functionality to business tier code in presentation tier code.



public interface MessageService {
	public void sendMessage();
}

public class EmailMessage implements MessageService {

	@Override
	public void sendMessage() {
		System.out.println("Send Email");
	}
}

public class TextMessage implements MessageService {
	
	@Override
	public void sendMessage() {
		System.out.println("Send Text Message");
	}
}

public class PhoneMessage implements MessageService {

	@Override
	public void sendMessage() {
		System.out.println("Phone Call");
	}
}

public class MessageServiceLookup {
	
	public MessageService getMessageService(MessageType msgType) {
		
		switch(msgType) {
			case EMAIL:		return new EmailMessage();
			case TEXT:		return new TextMessage();
			default:		return new PhoneMessage();
		}
	}
}

public class MessageDelegate {
	
	private final MessageServiceLookup lookupService = new MessageServiceLookup();
	private MessageService messageService;
	private MessageType msgType;

	public void setServiceType(MessageType msgType) {
		this.msgType = msgType;
	}

	public void doTask() {
		messageService = lookupService.getMessageService(msgType);
		messageService.sendMessage();
	}
}

public class Client {

	private final MessageDelegate messageService;

	public Client(MessageDelegate messageService) {
		this.messageService = messageService;
	}

	public void doTask() {
		messageService.doTask();
	}
}

public class BusinessDelegatePattern {
	
	public static void main(String[] args) {
		
		MessageDelegate messageDelegate = new MessageDelegate();
			messageDelegate.setServiceType(MessageType.EMAIL);

		Client client = new Client(messageDelegate);
			client.doTask();

			messageDelegate.setServiceType(MessageType.TEXT);
			client.doTask();
	}
}