1

I am new to design patterns and stuck on very basic step : different types of design patterns.

Creational patters are pretty easy to understand.

I just want to understand the simple definition of structural and behavioral design patterns, with explanation in simple terms of java and not in terms of various design patterns.

Note: I read qsns on stackoverflow but I am not clear with the answers. Can anyone explains it in terms of java with simple examples.

Onki
  • 1,879
  • 6
  • 38
  • 58
  • Wouldn't it be helpful to just look at real world examples in Java core APIs? http://stackoverflow.com/q/1673841 Perhaps you've more than often worked with such a class in standard Java and then it helps to realize why it was actually designed that way and so forth. – BalusC Jul 06 '15 at 18:24

2 Answers2

7

A behavioral pattern is used to abstract some kind of variation in behavior. One of the most common behavioral patterns is Strategy. A good example of the Strategy pattern in Java is the Collections.sort(List<T>, Comparator<? super T>) method. The Comparator in this method is the Strategy used to determine how the list will be sorted. There is one sort method, but you are free to pass in any number of Comparator implementations that effectively control how the sort is performed. This is the essence of the Strategy pattern.

A structural pattern is used to bring together existing objects into some new kind of design. One of the most common structural patterns is Adapter. A good example of the Adapter pattern in Java is the Arrays.asList() method. This method returns an object (the Adapter) that makes the array appear as if it implements the List interface, thus allowing you to pass the array to a method that expects an implementation of List.

Rusty Shackleford
  • 1,111
  • 2
  • 14
  • 19
0

Structural patterns focus on relationship in between objects. Group of entities that forms a structure (roughly a data structure). Ex: composite pattern to obtain a collection of object that are manipulated uniformly, see java TreeNode. java.awt.Container is a typical implementation of composite pattern: a GUI is obtained by aggregating GUI objects (A frame that contains a panel that contains a button and a label) :

Container panel = new JPanel();
Container subpanel = new JPanel();
Container button = new JButton("Click me");
// the following is independent of the real nature of objects, except that they are Containers
panel.add(subpanel);
subpanel.add(button);

Behavioral pattern focus on communication in between objects. Group of entities that communicates (roughly a network of communication). Ex: chain of responsability which lets you obtain a computation from a list of computation entities, see java Logger. Java Iterator or Observer are very common behavioral patterns.

Collection<Integer> c = ...
// The following is independent of the real nature of the collection (List, Array, etc.)
Iterator<Integer> i = c.iterator();
while (i.hasNext()) {
   Integer ii = i.next();
   // do something with ii
}
Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69
  • Yunes I have already mentioned that I am new to design patterns and if anyone can explain me in simple terms of java rather then in terms of design patterns, then it would be easier to understand – Onki Jul 06 '15 at 18:31
  • The observable Patterns relates 2 objects . The Observer knows the observable and viceversa : "Structural patterns focus on relationship in between objects" . Why is not a Structural Pattern? Am i not creating a larger structure? – Al Xx Dec 19 '22 at 22:38
  • 1
    @AlXx Most of patterns relates objects, but the question is more what is the main purpose of these relations? Observer is more focused on the way objects communicates: “**behavioral design patterns** are design patterns that identify common communication patterns among objects". – Jean-Baptiste Yunès Dec 21 '22 at 08:25