Skip to content

Commit 0aafa61

Browse files
committed
composite added
1 parent 1a42c76 commit 0aafa61

File tree

10 files changed

+209
-0
lines changed

10 files changed

+209
-0
lines changed

composite/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# composite
2+
This is repository of http://androidcode.pl blog. It shows uses of Composite pattern in Android. It is a part of Design Patterns - Composite post in the blog.

composite/example/Box.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
public class Box implements Item {
2+
3+
private String name;
4+
private String sector;
5+
private Status status;
6+
private List<Item> items;
7+
8+
public Box(String name) {
9+
this.name = name;
10+
this.sector = "main";
11+
this.status = Status.MAGAZINE;
12+
this.items = new ArrayList();
13+
}
14+
15+
@Override
16+
public String getInfo() {
17+
String info = name + " contains: ";
18+
for(Item item : items)
19+
info = info + item.getInfo() + "\n";
20+
return info;
21+
}
22+
23+
@Override
24+
public double getPrice() {
25+
double price = 0;
26+
for(Item item : items)
27+
price += item.getPrice();
28+
return price;
29+
}
30+
31+
@Override
32+
public void changeStatus(Status status) {
33+
this.status = status;
34+
for(Item item : items)
35+
item.changeStatus(status);
36+
}
37+
38+
public void addItem(Item item) {
39+
items.add(item);
40+
}
41+
42+
public void removeItem(Item item) {
43+
item.changeStatus(Status.MAGAZINE);
44+
items.remove(item);
45+
}
46+
47+
public void changeSector(String sector) {
48+
this.sector = sector;
49+
}
50+
51+
public String getSector() {
52+
return sector;
53+
}
54+
}

composite/example/Item.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
interface Item {
2+
3+
String getInfo();
4+
void getPrice();
5+
void changeStatus(Status status);
6+
}

composite/example/Main.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
public class Main {
2+
3+
public static void main() {
4+
//workers colect the order
5+
//small items in bag
6+
Box bag = new Box("Bag");
7+
Item item1 = new Item("Bulb", 10.0);
8+
Item item2 = new Item("Towel", 5.00);
9+
bag.addItem(item1);
10+
bag.addItem(item2);
11+
12+
//fragile products in box
13+
Box box = new Box("Box");
14+
Item item3 = new Item("Wine glass", 8.00);
15+
Item item4 = new Item("Wine glass", 8.00);
16+
Item item5 = new Item("Jug", 13.00);
17+
box.addItem(item3);
18+
box.addItem(item4);
19+
box.addItem(item5);
20+
21+
//small and fragile items moved to deliver zone
22+
bag.changeSector("A1");
23+
bag.changeStatus(Status.READY);
24+
box.changeSector("B2");
25+
box.changeStatus(Status.READY);
26+
27+
//client canceled one item
28+
box.removeItem(item4);
29+
30+
//order is ready to deliver
31+
//workers get boxes from sectors
32+
String bagSector = bag.getSector();
33+
String boxSector = box.getSector();
34+
Box container = new Box("Container");
35+
container.addItem(bag);
36+
container.addItem(box);
37+
38+
//prepare recipe
39+
String orderInfo = container.getInfo();
40+
double orderPrice = container.getPrice();
41+
42+
//deliver order to client
43+
container.changeStatus(Status.DELIVER);
44+
}
45+
}

composite/example/Product.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
public class Product implements Item {
2+
3+
private String name;
4+
private Status status;
5+
private double price;
6+
7+
public Product(String name, double price) {
8+
this.name = name;
9+
this.price = price;
10+
this.status = Status.MAGAZINE;
11+
}
12+
13+
@Override
14+
public String getInfo() {
15+
return name;
16+
}
17+
18+
@Override
19+
public double getPrice() {
20+
return price;
21+
}
22+
23+
@Override
24+
public void changeStatus(Status status) {
25+
this.status = status;
26+
}
27+
}

composite/example/enum/Status.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
public enum Status {
2+
3+
MAGAZINE(0),
4+
READY(1),
5+
DELIVER(2),
6+
DELIVERED(3);
7+
8+
int status;
9+
10+
public Status(int status) {
11+
this.status = status;
12+
}
13+
}

composite/pattern/Component.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
interface Component {
2+
3+
void operation();
4+
}

composite/pattern/Composite.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
public class Composite implements Component {
2+
3+
private List<Component> childrens;
4+
//other fields
5+
6+
public Composite() {
7+
childrens = new ArrayList();
8+
}
9+
10+
public void addChild(Component component) {
11+
childrens.add(component);
12+
}
13+
14+
public void removeChild(Component component) {
15+
childrens.remove(component);
16+
}
17+
18+
@Override
19+
public void operation() {
20+
//do action
21+
for(Component children : childrens) {
22+
children.operation();
23+
}
24+
}
25+
26+
//other methods
27+
}

composite/pattern/Leaf.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
public class Leaf implements Component {
2+
3+
//some fields
4+
5+
@Override
6+
public void operation() {
7+
//do action
8+
}
9+
10+
//other methods
11+
}

composite/pattern/Main.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
public class Main {
2+
3+
public static void main() {
4+
//create components
5+
Composite composite1 = new Composite();
6+
Composite composite2 = new Composite();
7+
Leaf leaf1 = new Leaf();
8+
Leaf leaf1 = new Leaf();
9+
Leaf leaf1 = new Leaf();
10+
11+
//create tree structure
12+
composite2.addChild(leaf2);
13+
composite2.addChild(leaf3);
14+
composite1.addChild(leaf1);
15+
composite1.addChild(composite2);
16+
17+
//do some action
18+
composite1.action(); //do action for every child in composite1
19+
}
20+
}

0 commit comments

Comments
 (0)