1b: composite pattern
This commit is contained in:
parent
67e5565b54
commit
3f30173a5b
@ -8,13 +8,13 @@ public class Client {
|
|||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
NewsCollection masterCollection = new NewsCollection("Daily news");
|
NewsCollection masterCollection = new NewsCollection("Daily news");
|
||||||
masterCollection.addCollection(new NewsCollection("Sports")
|
masterCollection.addItem(new NewsCollection("Sports")
|
||||||
.addArticle(new Article("Harder, Better, Faster, Stronger. Doping controls loosened","ups"))
|
.addItem(new Article("Harder, Better, Faster, Stronger. Doping controls loosened","ups"))
|
||||||
.addImage(new Image("Athletic cat contest", 800, 600, "zdf")))
|
.addItem(new Image("Athletic cat contest", 800, 600, "zdf")))
|
||||||
.addCollection(new NewsCollection("Local news")
|
.addItem(new NewsCollection("Local news")
|
||||||
.addArticle(new Article("Missing Cat missed.", "mfg"))
|
.addItem(new Article("Missing Cat missed.", "mfg"))
|
||||||
.addArticle(new Article("Corrupted local politician selling cats.", "ard"))
|
.addItem(new Article("Corrupted local politician selling cats.", "ard"))
|
||||||
.addImage(new Image("Missing cat", 1280, 720, "anonymous")));
|
.addItem(new Image("Missing cat", 1280, 720, "anonymous")));
|
||||||
|
|
||||||
System.out.println("\n===List===\n");
|
System.out.println("\n===List===\n");
|
||||||
System.out.print(masterCollection.getOverview());
|
System.out.print(masterCollection.getOverview());
|
||||||
|
@ -6,9 +6,7 @@ import java.util.List;
|
|||||||
public class NewsCollection extends Item {
|
public class NewsCollection extends Item {
|
||||||
|
|
||||||
private String topic;
|
private String topic;
|
||||||
private List<Article> articles = new LinkedList<Article>();
|
private List<Item> items = new LinkedList<>();
|
||||||
private List<Image> images = new LinkedList<Image>();
|
|
||||||
private List<NewsCollection> collections = new LinkedList<NewsCollection>();
|
|
||||||
|
|
||||||
public NewsCollection(String topic) {
|
public NewsCollection(String topic) {
|
||||||
this.topic = topic;
|
this.topic = topic;
|
||||||
@ -22,16 +20,8 @@ public class NewsCollection extends Item {
|
|||||||
StringBuilder builder = new StringBuilder();
|
StringBuilder builder = new StringBuilder();
|
||||||
builder.append("###" + getTopic() + "###");
|
builder.append("###" + getTopic() + "###");
|
||||||
builder.append("\n");
|
builder.append("\n");
|
||||||
for (Article article : getArticles()) {
|
for(Item item : getItems()){
|
||||||
builder.append(article.getOverview());
|
builder.append(item.getOverview());
|
||||||
builder.append("\n");
|
|
||||||
}
|
|
||||||
for (Image image : getImages()) {
|
|
||||||
builder.append(image.getOverview());
|
|
||||||
builder.append("\n");
|
|
||||||
}
|
|
||||||
for (NewsCollection collection : getCollections()) {
|
|
||||||
builder.append(collection.getOverview());
|
|
||||||
builder.append("\n");
|
builder.append("\n");
|
||||||
}
|
}
|
||||||
return builder.toString();
|
return builder.toString();
|
||||||
@ -45,14 +35,8 @@ public class NewsCollection extends Item {
|
|||||||
StringBuilder builder = new StringBuilder();
|
StringBuilder builder = new StringBuilder();
|
||||||
builder.append("###" + getTopic() + "###");
|
builder.append("###" + getTopic() + "###");
|
||||||
builder.append("\n");
|
builder.append("\n");
|
||||||
for (Article article : getArticles()) {
|
for (Item item : getItems()) {
|
||||||
builder.append(article.getDetails());
|
builder.append(item.getDetails());
|
||||||
}
|
|
||||||
for (Image image : getImages()) {
|
|
||||||
builder.append(image.getDetails());
|
|
||||||
}
|
|
||||||
for (NewsCollection collection : getCollections()) {
|
|
||||||
builder.append(collection.getDetails());
|
|
||||||
}
|
}
|
||||||
return builder.toString();
|
return builder.toString();
|
||||||
}
|
}
|
||||||
@ -67,56 +51,20 @@ public class NewsCollection extends Item {
|
|||||||
return topic;
|
return topic;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Get the list of articles stored directly in this collection.
|
* Get the list of items stored directly in this collection.
|
||||||
* @return articles stored in this collection
|
* @return items stored in this collection
|
||||||
*/
|
*/
|
||||||
public List<Article> getArticles() {
|
public List<Item> getItems() {
|
||||||
return articles;
|
return items;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the list of images stored directly in this collection.
|
* Store an item directly in this collection.
|
||||||
* @return images stored in this collection
|
* @param item
|
||||||
*/
|
|
||||||
public List<Image> getImages() {
|
|
||||||
return images;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the list of collections stored directly in this collection.
|
|
||||||
* @return collections stored in this collection
|
|
||||||
*/
|
|
||||||
public List<NewsCollection> getCollections() {
|
|
||||||
return collections;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Store an article directly in this collection.
|
|
||||||
* @param article
|
|
||||||
* @return this collection (enables method chaining)
|
* @return this collection (enables method chaining)
|
||||||
*/
|
*/
|
||||||
public NewsCollection addArticle(Article article) {
|
public NewsCollection addItem(Item item) {
|
||||||
articles.add(article);
|
items.add(item);
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Store an image directly in this collection.
|
|
||||||
* @param image
|
|
||||||
* @return this collection (enables method chaining)
|
|
||||||
*/
|
|
||||||
public NewsCollection addImage(Image image) {
|
|
||||||
images.add(image);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Store another collection directly in this collection.
|
|
||||||
* @param collection
|
|
||||||
* @return this collection (enables method chaining)
|
|
||||||
*/
|
|
||||||
public NewsCollection addCollection(NewsCollection collection) {
|
|
||||||
collections.add(collection);
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user