-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0021e04
commit f7166fe
Showing
6 changed files
with
65 additions
and
0 deletions.
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
8 changes: 8 additions & 0 deletions
8
JavaSource/src/main/java/ru/akhcheck/patterns/composite/Breakfast.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,8 @@ | ||
package ru.akhcheck.patterns.composite; | ||
|
||
public class Breakfast extends Dish { | ||
@Override | ||
public int GetCalories() { | ||
return super.GetCalories() + 10; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
JavaSource/src/main/java/ru/akhcheck/patterns/composite/Dish.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,21 @@ | ||
package ru.akhcheck.patterns.composite; | ||
|
||
import java.util.ArrayList; | ||
|
||
public class Dish implements DishComponent{ | ||
private ArrayList<DishComponent> components = new ArrayList<>(); | ||
|
||
public int GetCalories() { | ||
int calories = 0; | ||
for (DishComponent component: components) { | ||
calories += component.GetCalories(); | ||
} | ||
return calories; | ||
} | ||
|
||
public Dish add(DishComponent component) { | ||
components.add(component); | ||
return this; | ||
} | ||
|
||
} |
5 changes: 5 additions & 0 deletions
5
JavaSource/src/main/java/ru/akhcheck/patterns/composite/DishComponent.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,5 @@ | ||
package ru.akhcheck.patterns.composite; | ||
|
||
public interface DishComponent { | ||
int GetCalories(); | ||
} |
16 changes: 16 additions & 0 deletions
16
JavaSource/src/main/java/ru/akhcheck/patterns/composite/MainComposite.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,16 @@ | ||
package ru.akhcheck.patterns.composite; | ||
|
||
public class MainComposite { | ||
|
||
|
||
public static void main(String[] args) { | ||
Dish ration = new Dish().add( | ||
new Breakfast().add( | ||
new Milk() | ||
).add( | ||
new Milk() | ||
) | ||
).add(new Milk()); | ||
System.out.println(ration.GetCalories()); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
JavaSource/src/main/java/ru/akhcheck/patterns/composite/Milk.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,9 @@ | ||
package ru.akhcheck.patterns.composite; | ||
|
||
public class Milk implements DishComponent { | ||
|
||
@Override | ||
public int GetCalories() { | ||
return 100; | ||
} | ||
} |