Interface MaterialComponent

All Known Implementing Classes:
MaterialBundle, MaterialLeaf

public interface MaterialComponent
Component interface for the Composite pattern.

Represents both individual materials and composite bundles uniformly. The Composite Pattern is a structural design pattern that allows developers to treat individual objects and groups of objects uniformly. This is particularly useful when working with tree-like structures, where both leaf objects (e.g., individual books) and composite objects (e.g., bundles of books) need to share the same interface.

The pattern promotes the principle of "compose objects into tree structures to represent part-whole hierarchies." With Composite, clients can operate on single objects and entire compositions through a common interface, simplifying the design and improving extensibility.

This interface allows clients to treat individual materials and material bundles uniformly, enabling recursive composition and polymorphic operations.

Example usage:


 MaterialComponent book = new MaterialLeaf(new PrintedBook(...));
 MaterialComponent bundle = new MaterialBundle("Back-to-School Pack", 0.1);
 bundle.addComponent(book);
 
 // Both can be treated uniformly
 double price = component.getPrice();
 String info = component.getDisplayInfo();
 
Since:
2024-09-15
Version:
3.0
Author:
Navid Mohaghegh
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    Gets a description of this component.
    double
    Gets the discounted price of this component.
    double
    Gets the discount rate applied to this component.
    int
    Gets the total number of items in this component.
    Gets all materials contained in this component.
    double
    Gets the total price of this component.
    Gets the title of this component.
    default boolean
    Checks if this component is a composite (bundle).
    boolean
    Checks if this component is a leaf (individual material).
  • Method Details

    • getTitle

      String getTitle()
      Gets the title of this component. For individual materials, this is the material title. For bundles, this is the bundle name.
      Returns:
      the component title
    • getPrice

      double getPrice()
      Gets the total price of this component. For individual materials, this is the material price. For bundles, this is the sum of all contained materials.
      Returns:
      the total price
    • getDiscountedPrice

      double getDiscountedPrice()
      Gets the discounted price of this component. For individual materials, this applies the material's discount. For bundles, this applies the bundle discount to the total price.
      Returns:
      the discounted price
    • getDescription

      String getDescription()
      Gets a description of this component. Provides detailed information about the component and its contents.
      Returns:
      the component description
    • getMaterials

      List<Material> getMaterials()
      Gets all materials contained in this component. For individual materials, returns a list containing only itself. For bundles, returns all materials in the bundle (recursively).
      Returns:
      list of all contained materials
    • getItemCount

      int getItemCount()
      Gets the total number of items in this component. For individual materials, returns 1. For bundles, returns the sum of all contained items.
      Returns:
      the total item count
    • getDiscountRate

      double getDiscountRate()
      Gets the discount rate applied to this component.
      Returns:
      the discount rate (0.0 to 1.0)
    • isLeaf

      boolean isLeaf()
      Checks if this component is a leaf (individual material).
      Returns:
      true if this is a leaf component
    • isComposite

      default boolean isComposite()
      Checks if this component is a composite (bundle).
      Returns:
      true if this is a composite component