Design Pattern - Structural - Composite

   

Scenario:

Create a Product which could be individually sold or as a bundle.

Solution:

This pattern composes the objects into tree structures to represent part-whole hierarchies.

Example: Product which can be created singularly or as a composite (bundle).
  1. Product

  2.  1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    using System.Text;
    
    namespace TestProject
    {
        public abstract class Product
        {
            public string Name { get; set; }
            public int Price { get; set; }
    
            protected Product(string name, int price)
            {
                Name = name;
                Price = price;
            }
    
            public abstract void Add(Product product);
            public abstract void Delete(Product product);
            public abstract string GetProductDetails();
        }
    
        public class BundleSku : Product
        {
            private readonly List<Product> _skus = new();
    
            public BundleSku(string name, int price) : base(name, price)
            {
            }
    
            public override void Add(Product product)
            {
                _skus.Add(product);
            }
    
            public override void Delete(Product product)
            {
                _skus.Remove(product);
            }
    
            public override string GetProductDetails()
            {
                var details = new StringBuilder();
                for (var i = 0; i < _skus.Count; i++)
                {
                    details.Append(_skus[i].GetProductDetails());
    
                    details.Append(_skus.Count != i + 1 ? " and " : ".");
    
                    details.Append("\n");
                }
                return details.ToString();
            }
        }
    
        public class Sku : Product
        {
            public Sku(string name, int price) : base(name, price)
            {
            }
    
            public override void Add(Product product)
            {
                throw new NotImplementedException();
            }
    
            public override void Delete(Product product)
            {
                throw new NotImplementedException();
            }
    
            public override string GetProductDetails()
            {
                return $"{Name} @ ${Price}";
            }
        }
    }

  3. Create skus and then add them to bundle.
  4.  

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    var sku1 = new Sku("IPhone 14", 10000);
    var sku2 = new Sku("Screen Guard", 200);
    var sku3 = new Sku("Case", 1000);
    var bundleSku = new BundleSku("Iphone 14 bundle", 11200);
    
    bundleSku.Add(sku1);
    bundleSku.Add(sku2);
    bundleSku.Add(sku3);
    
    Console.WriteLine($"{bundleSku.Name} comes with: " +
                      $"{bundleSku.GetProductDetails()}The total price of bundle is: ${bundleSku.Price}.");
    Console.ReadLine();

No comments:

Post a Comment

Move Github Sub Repository back to main repo

 -- delete .gitmodules git rm --cached MyProject/Core git commit -m 'Remove myproject_core submodule' rm -rf MyProject/Core git remo...