Home » Core Java » Design Patterns

Design Patterns

Java EE 6 Decorators advanced usage

The example we’ll use is a Social media feed processor. So I have created an interface:   public interface SocialFeedProcessor { Feed process(String feed); } and provided 2 implementations, twitter and google+   public class TwitterFeedProcessor implements SocialFeedProcessor{ @Override public Feed process(String feed) { System.out.println("processing this twitter feed"); // processing logics return new Feed(feed); } } public class GooglePlusFeedProcessor implements ...

Read More »

Decorating classes at injection time with Java EE 6

Let’s say you have a ticket service that lets you order tickets for a certain event. The TicketService handles the registration etc, but we want to add catering. We don’t see this as part of the ticket ordering logic, so we created a decorator. The decorator will call the TicketService and add catering for the number of tickets. The interface: ...

Read More »

Implement a shopping cart using the Strategy Pattern

First a definition: in the simplest terms, you can define the Strategy Pattern as telling an object to do a job and to do it using ANOTHER object. To clarify this further I’m going to redesign the ShoppingCart slightly, by giving it a pay()* method: public class ShoppingCart { private final List<Item> items; public ShoppingCart() { items = new ArrayList<Item>(); ...

Read More »