Composite Design Pattern by example
This post explains Composite design pattern with an example.
When to use ? – Use Composite pattern when you have treat a collection of objects as they were once one thing. With Composite design pattern it is very easy to simplify operations over collection data structure. It reduces complexity of the code required if you were going to handle collection as special case.
How to Use? - The Composite pattern consist of Component, Leaf and Composite Classes as shown below class diagram
Component – Component declares the common interface that both the single and composite nodes will implements
Leaf – The leaf class represent the singular atomic data type implementing he component interface.
Composite – Composite is an abstract entity which declares common functionality which are needed by concrete implementation for handling collection data.
An Example -
Item.java
public interface Item {
public double getPrice();
}
ColdDrink.java
public class ColdDrink implements Item {
private String itemName;
private String iemDesc;
private double price;
public ColdDrink(String itemName, String desc, double price){
this.itemName=itemName;
this.iemDesc= desc;
this.price = price;
}
public double getPrice() {
return price;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public String getItemName() {
return itemName;
}
public void setIemDesc(String iemDesc) {
this.iemDesc = iemDesc;
}
public String getIemDesc() {
return iemDesc;
}
}
Composite.java
public abstract class Composite implements Item {
/**
* @associates <{demp.composite.Item}>
*/
List<Item> items = new ArrayList<Item>();
public void add(Item itm) {
items.add(itm);
}
public void remove(Item itm) {
items.remove(itm);
}
public void addAll(List lst) {
items.addAll(lst);
}
public double getPrice() {
double sum=0;
for (Item i: items) {
sum += i.getPrice();
}
return sum;
}
}
ColdDrinkPartyPack.java
import java.util.List;
public class ColdDrinkPartyPack extends Composite {
public ColdDrinkPartyPack(List items){
super.addAll(items);
}
public double getPrice(){
return super.getPrice() - super.getPrice()*.25; // get 25% discount on family pack
}
}
ColdDrinkFamilyPack .java
import java.util.List;
public class ColdDrinkFamilyPack extends Composite{
public ColdDrinkFamilyPack(List items){
super.addAll(items);
}
public double getPrice(){
return super.getPrice() - super.getPrice()*.10; // get 15% discount on family pack
}
}
The application Class
public class Application {
public Application() {
}
public static void main(String[] args){
List items = new ArrayList();
items.add(new ColdDrink("Pepsi","cold drink",10));
items.add(new ColdDrink("Cock","cold drink",20));
items.add(new ColdDrink("maza","cold drink",15));
ColdDrinkFamilyPack familyPack = new ColdDrinkFamilyPack(items);
System.out.println(familyPack.getPrice());
List item2s = new ArrayList();
item2s.add(new ColdDrink("Pepsi","cold drink",10));
item2s.add(new ColdDrink("Cock","cold drink",20));
item2s.add(new ColdDrink("maza","cold drink",15));
item2s.add(new ColdDrink("Pepsi","cold drink",10));
item2s.add(new ColdDrink("Cock","cold drink",20));
item2s.add(new ColdDrink("maza","cold drink",15));
ColdDrinkPartyPack partyPack = new ColdDrinkPartyPack(item2s);
System.out.println(partyPack.getPrice());
}
}
Output
40.5
67.5
you spelled Coke Cock LOL
ReplyDeletedüzce evden eve nakliyat
ReplyDeletedenizli evden eve nakliyat
kırşehir evden eve nakliyat
çorum evden eve nakliyat
afyon evden eve nakliyat
A3E
C7FA4
ReplyDeleteMaraş Parça Eşya Taşıma
Bitlis Parça Eşya Taşıma
Gölbaşı Fayans Ustası
Karapürçek Fayans Ustası
Cointiger Güvenilir mi
Ort Coin Hangi Borsada
AAX Güvenilir mi
Arbitrum Coin Hangi Borsada
Hakkari Şehirler Arası Nakliyat
08849
ReplyDeleteYenimahalle Fayans Ustası
Eskişehir Şehir İçi Nakliyat
Kırıkkale Şehirler Arası Nakliyat
Uşak Lojistik
Muş Şehir İçi Nakliyat
Çerkezköy Asma Tavan
Çerkezköy Korkuluk
Kilis Parça Eşya Taşıma
Ünye Parke Ustası
587C8
ReplyDeleteAksaray Şehirler Arası Nakliyat
Ünye Çelik Kapı
Ordu Şehirler Arası Nakliyat
Diyarbakır Şehirler Arası Nakliyat
Düzce Şehir İçi Nakliyat
Elazığ Parça Eşya Taşıma
Kayseri Parça Eşya Taşıma
order pharmacy steroids
Çankaya Parke Ustası
CBFB7
ReplyDeleteEskişehir Evden Eve Nakliyat
Ünye Marangoz
Ünye Yol Yardım
Gölbaşı Parke Ustası
Hakkari Evden Eve Nakliyat
Kırklareli Evden Eve Nakliyat
Binance Referans Kodu
Çerkezköy Cam Balkon
Etlik Parke Ustası
CDD71
ReplyDeletereferans kimliği nedir
binance referans kodu
binance referans kodu
resimli magnet
resimli magnet
binance referans kodu
binance referans kodu
referans kimliği nedir
resimli magnet
605F2
ReplyDeletevindax
mobil proxy 4g
btcturk
toptan mum
4g proxy
kraken
bitcoin ne zaman çıktı
en düşük komisyonlu kripto borsası
okex
319DA
ReplyDeletekucoin
binance
referans kimligi nedir
cointiger
telegram kripto grupları
telegram en iyi kripto grupları
kredi kartı ile kripto para alma
canlı sohbet uygulamaları
paribu
421BD
ReplyDeleteokex
mexc
telegram türk kripto kanalları
bitexen
bitcoin seans saatleri
binance referans kodu
aax
türk kripto telegram grupları
bitcoin nasıl üretilir
CB3F0
ReplyDeleteApril 2024 Calendar
bitcoin nasıl oynanır
binance referans kimliği nedir
btcturk
April 2024 Calendar
btcturk
gate io
telegram en iyi kripto grupları
kripto para nasıl alınır
15D97
ReplyDeletebingx
en iyi kripto para uygulaması
kripto para kanalları telegram
bybit
referans kodu
binance referans
kripto para haram mı
binance
bitrue
05AF9
ReplyDelete----
matadorbet
----
----
----
----
----
----
----
rfgrthtrju
ReplyDeleteصيانة افران الغاز بمكة
fdgbswedewsfrdg
ReplyDeleteشركة تنظيف افران
FGBRHBTBTHG
ReplyDeleteشركة صيانة افران بالاحساء
Nurses Job Vacancy in the UK offers diverse opportunities for healthcare professionals to excel in various specialties. Responsibilities include patient care, medication administration, and collaboration with multidisciplinary teams. Positions available in hospitals, clinics, and community health settings. Competitive salaries and comprehensive benefits provided. Valid nursing qualifications and strong clinical skills required. Excellent communication abilities essential. Continuous professional development and career progression supported. Applications welcome from both local and international candidates.
ReplyDeletehttps://www.dynamichealthstaff.com/nurses-job-vacancy-in-uk
شركة تسليك مجاري بالخبر OVoxdVJdA3
ReplyDeleteصيانة افران جدة dXt62pVzpw
ReplyDelete