forked from luisburgos/design-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added notes, examples and pattern files for Composite pattern.
- Loading branch information
1 parent
71922b2
commit f02f806
Showing
15 changed files
with
451 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package composite.examples.directories; | ||
|
||
import java.util.ArrayList; | ||
|
||
/** | ||
* Created by luisburgos on 18/07/15. | ||
*/ | ||
public class Directory extends File { | ||
|
||
private ArrayList<File> files; | ||
|
||
public Directory (String name) { | ||
this.name = name; | ||
files = new ArrayList<>(); | ||
} | ||
|
||
@Override | ||
public void add(File file) { | ||
files.add(file); | ||
} | ||
|
||
@Override | ||
public void remove(File file) { | ||
files.remove(file); | ||
} | ||
|
||
@Override | ||
public void showInfo() { | ||
System.out.print(identado.toString() + "* Directory: " + getName() + "\n"); | ||
identado.append(" "); | ||
for(File file : files){ | ||
file.showInfo(); | ||
} | ||
identado.setLength(identado.length() - 3); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package composite.examples.directories; | ||
|
||
/** | ||
* Created by luisburgos on 18/07/15. | ||
*/ | ||
public abstract class File { | ||
|
||
protected String name; | ||
protected static StringBuffer identado = new StringBuffer(); | ||
|
||
public void add(File component) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
public void remove(File component) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void showInfo() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package composite.examples.directories; | ||
|
||
/** | ||
* Created by luisburgos on 18/07/15. | ||
*/ | ||
public class FileSystem { | ||
|
||
private File allFiles; | ||
|
||
public FileSystem(File allFiles) { | ||
this.allFiles = allFiles; | ||
} | ||
|
||
public void printFiles() { | ||
allFiles.showInfo(); | ||
} | ||
|
||
|
||
} |
34 changes: 34 additions & 0 deletions
34
src/composite/examples/directories/FileSystemTestDrive.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package composite.examples.directories; | ||
|
||
/** | ||
* Created by luisburgos on 18/07/15. | ||
*/ | ||
public class FileSystemTestDrive { | ||
|
||
public static void main(String[] args) { | ||
|
||
//Dummy linux file system. | ||
|
||
File home = new Directory("home"); | ||
File opt = new Directory("opt"); | ||
File usr = new Directory("usr"); | ||
|
||
File root = new Directory("root"); | ||
|
||
root.add(home); | ||
root.add(opt); | ||
root.add(usr); | ||
|
||
usr.add(new SimpleFile("bin")); | ||
usr.add(new SimpleFile("lib")); | ||
|
||
opt.add(new SimpleFile("google")); | ||
opt.add(new SimpleFile("idea")); | ||
opt.add(new SimpleFile("spotify")); | ||
|
||
home.add(new SimpleFile("luisburgos")); | ||
|
||
FileSystem fileSystem = new FileSystem(root); | ||
fileSystem.printFiles(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package composite.examples.directories; | ||
|
||
/** | ||
* Created by luisburgos on 18/07/15. | ||
*/ | ||
public class SimpleFile extends File { | ||
|
||
public SimpleFile(String name) { | ||
this.name = name; | ||
} | ||
|
||
@Override | ||
public void showInfo() { | ||
System.out.print(identado.toString() + "-Simple File: " + getName() + "\n"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package composite.examples.menu; | ||
|
||
public class Client { | ||
|
||
private MenuComponent allMenus; | ||
|
||
public Client(MenuComponent todosLosMenus) { | ||
this.allMenus = todosLosMenus; | ||
} | ||
|
||
public void printMenu() { | ||
allMenus.print(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package composite.examples.menu; | ||
|
||
public abstract class MenuComponent { | ||
|
||
protected String name; | ||
protected static StringBuffer identado = new StringBuffer(); | ||
|
||
public void add(MenuComponent component) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
public void remove(MenuComponent component) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
public String getName() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
public double getPrice() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
public boolean isVegetarian() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
public void print() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package composite.examples.menu; | ||
|
||
import java.util.ArrayList; | ||
|
||
public class MenuComposite extends MenuComponent { | ||
|
||
private ArrayList<MenuComponent> menuComponents; | ||
|
||
public MenuComposite(String name) { | ||
this.name = name; | ||
menuComponents = new ArrayList<>(); | ||
} | ||
|
||
@Override | ||
public void add(MenuComponent component) { | ||
this.menuComponents.add(component); | ||
} | ||
|
||
@Override | ||
public void remove(MenuComponent component) { | ||
this.menuComponents.remove(component); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return this.name; | ||
} | ||
|
||
@Override | ||
public void print() { | ||
|
||
System.out.print(identado.toString() + "* " + getName() + "\n"); | ||
//System.out.println(identado.toString() + "---------------------"); | ||
identado.append(" "); | ||
for(MenuComponent menuComponent : menuComponents){ | ||
menuComponent.print(); | ||
} | ||
identado.setLength(identado.length() - 5); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* To change this license header, choose License Headers in Project Properties. | ||
* To change this template file, choose Tools | Templates | ||
* and open the template in the editor. | ||
*/ | ||
package composite.examples.menu; | ||
|
||
/** | ||
* | ||
* @author luisburgos | ||
*/ | ||
public class MenuItem extends MenuComponent { | ||
|
||
private boolean vegetarian; | ||
private double price; | ||
|
||
public MenuItem(String name, boolean vegetarian, double price) { | ||
this.name = name; | ||
this.vegetarian = vegetarian; | ||
this.price = price; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return this.name; | ||
} | ||
|
||
@Override | ||
public double getPrice() { | ||
return this.price; | ||
} | ||
|
||
@Override | ||
public boolean isVegetarian() { | ||
return this.vegetarian; | ||
} | ||
|
||
@Override | ||
public void print() { | ||
System.out.print(identado.toString() + "# " + getName()); | ||
if (isVegetarian()) { | ||
System.out.print("(v)"); | ||
} | ||
System.out.println("," + getPrice()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package composite.examples.menu; | ||
|
||
public class MenuTestDrive { | ||
|
||
|
||
public static void main(String args[]) { | ||
|
||
MenuComponent meals = new MenuComposite("Comidas"); | ||
MenuComponent dinners = new MenuComposite("Cenas"); | ||
MenuComponent desserts = new MenuComposite("Postres"); | ||
MenuComponent mainCourse = new MenuComposite("Plato Fuerte"); | ||
|
||
MenuComponent allMenus = new MenuComposite("Menus"); | ||
|
||
allMenus.add(meals); | ||
allMenus.add(dinners); | ||
|
||
meals.add(mainCourse); | ||
meals.add(desserts); | ||
|
||
mainCourse.add(new MenuItem( | ||
"Crispy Chicken", | ||
false, | ||
100.89) | ||
); | ||
|
||
desserts.add(new MenuItem( | ||
"Apple Pie", | ||
false, | ||
15.59) | ||
); | ||
|
||
desserts.add(new MenuItem( | ||
"Cheesecake", | ||
false, | ||
19.99) | ||
); | ||
|
||
dinners.add(new MenuItem( | ||
"Hotdogs", | ||
false, | ||
6.05) | ||
); | ||
|
||
dinners.add(new MenuItem( | ||
"Spaghetti ", | ||
true, | ||
30.89) | ||
); | ||
|
||
|
||
//The client does not distinguish between item and composite | ||
Client client = new Client(allMenus); | ||
client.printMenu(); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package composite.pattern; | ||
|
||
/** | ||
* Created by luisburgos on 18/07/15. | ||
*/ | ||
public abstract class Component { | ||
|
||
protected String name; | ||
|
||
public void add(Component component) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
public void remove(Component component) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
public abstract void doSomething(); | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
} |
Oops, something went wrong.