Intuitive Combined Structures for Efficient Package Handling

composite n.w
1 / 30
Embed
Share

Dive into the world of composite and tree structures with Stanislav Dochynets. Explore the basics of combined structures, motivation behind using tree structures, and unravel the process of unpacking and pricing. Learn about classes, items, and boxes in a simple and intuitive manner. Discover how to implement practical order handling using abstract classes and witness the efficiency of handling packages with ease.

  • Composite Structures
  • Combined Structures
  • Tree Structures
  • Package Handling
  • Intuitive Learning

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Composite Stanislav Dochynets Stanislav Dochynets

  2. Zkladn vlastnosti Slo en /kombinovan Struktur ln Velmi intuitivn

  3. Motivace Pr ce se stromovou strukturou Vybalit zbo , spo tat cenu

  4. Pojme si to rozbalit a spotat

  5. Prvn co by ns mon napadlo public class Item { public int price; public string name; public class Box { public List<Item> itemsInBox; public List<Box> boxesInBox; Public Item(int price, string name) { this.price = price; this.name = name; } public Box() { itemsInBox = new ArrayList<>(); boxesInBox = new ArrayList<>(); } } }

  6. public static void Main(string[] args) { // Assuming getOrder() is implemented elsewhere Box order = GetOrder(); List<Item> unpacked = new List<Item>(); int total = 0; Queue<Box> packed = new Queue<Box>(); packed.Enqueue(order); while (packed.Count > 0) { var current = packed.Dequeue(); foreach (var box in current.BoxesInBox) { packed.Enqueue(box); } foreach (var item in current.ItemsInBox) { unpacked.Add(item); total += item.Price; } } }

  7. 2. een public abstract class Packable { } public class Box : Packable { public List<Packable> ContentsOfBox; public Box() { ContentsOfBox = new List<Packable>(); } public class Item : Packable { public int Price; public string Name; public Item(int price, string name) { Price = price; public void Insert(Packable p) { ContentsOfBox.Add(p); } Name = name; } } }

  8. public static void Main(string[] args) { // Assuming getOrder() is implemented elsewhere Box order = GetOrder(); List<Item> unpacked = new List<Item>(); int total = 0; Queue<Box> packed = new Queue<Box>(); packed.Enqueue(order); while (packed.Count > 0) { var current = packed.Dequeue(); foreach (var content in current.ContentsOfBox) { if (content is Item i) { unpacked.Add(i); total += i.Price; } else if (content is Box b) { packed.Enqueue(b); } else { throw new NotSupportedException("Unknown type in structure"); } } } }

  9. 3. een public abstract class Packable { public abstract int GetPrice(); public abstract void UnpackTo(List<Item> list); } public class Item : Packable { public int Price; public string Name; public Item(int price, string name) { Price = price; Name = name; } public override int GetPrice() { return Price; } public override void UnpackTo(List<Item> list) { list.Add(this); } }

  10. public class Box : Packable { public List<Packable> ContentsOfBox; public Box() { ContentsOfBox = new List<Packable>(); } public void Insert(Packable p) { ContentsOfBox.Add(p); } public override int GetPrice() { int result = 0; foreach (var content in ContentsOfBox) { result += content.GetPrice(); } return result; } public override void UnpackTo(List<Item> list) { foreach (var content in ContentsOfBox) { content.UnpackTo(list); } } }

  11. public static void Main(string[] args) { // Assuming getOrder() is implemented elsewhere Box order = GetOrder(); List<Item> unpacked = new List<Item>(); int totalPrice = order.GetPrice(); order.UnpackTo(unpacked); }

  12. 4. een public abstract class Packable { public abstract int GetPrice(); public abstract void UnpackTo(List<Item> list); public virtual void Insert(Packable p) { throw new NotSupportedException("Object is a leaf."); } } public class Item : Packable { public int Price; public string Name; public Item(int price, string name) { Price = price; Name = name; } public override int GetPrice() { return Price; } public override void UnpackTo(List<Item> list) { list.Add(this); } }

  13. public class Box : Packable { public List<Packable> ContentsOfBox; public Box() { ContentsOfBox = new List<Packable>(); } public override void Insert(Packable p) { ContentsOfBox.Add(p); } public override int GetPrice() { int result = 0; foreach (var content in ContentsOfBox) { result += content.GetPrice(); } return result; } public override void UnpackTo(List<Item> list) { foreach (var content in ContentsOfBox) { content.UnpackTo(list); } } }

  14. Component : C lov komponenta kompozitn struktury Leaf - List : Koncov uzel s b zovou verz operace Composite : Vnit n uzel s agrega n verz operace

  15. + o lep pr ce se slo it mi stromov mi strukturami o dodr ov n Open/Closed principu o rovnom rn zozlo en v po t o budov n datov struktury o roz i itelnost o najdeme spoustu uplatn n o zjednodu en k du

  16. - - o o o slo itostspole n ho rozhran vytv en p li obecn ch struktur poru en Interface Segregation principu

  17. Rozhodli jsme se pro composite

  18. public abstract class Graphics { public abstract void Draw(); public abstract void Add(Graphics g); public abstract void Remove(Graphics g); public abstract Graphics GetChild(int index); } public class Canvas : Graphics { private List<Graphics> children = new List<Graphics>(); public override void Draw() { foreach (var child in children) { child.Draw(); } } public override void Add(Graphics g) => children.Add(g); public override void Remove(Graphics g) => children.Remove(g); public override Graphics GetChild(int index) => children[index]; }

  19. public abstract class Graphics { public abstract void Draw(); public abstract void Add(Graphics g); public abstract void Remove(Graphics g); public abstract Graphics GetChild(int index); } public class Primitive : Graphics { public override void Draw() { /* Implementation specific to primitive */ } public override void Add(Graphics g) { throw new NotImplementedException(); } public override void Remove(Graphics g) { throw new NotImplementedException();} public override Graphics GetChild(int index) { throw new NotImplementedException();} } public class Line : Primitive { /* Specific drawing implementation */ } public class Circle : Primitive { /* Specific drawing implementation */ }

  20. Zlepme:

  21. public interface IGraphic { void Draw(); } public interface ICompositeGraphic : IGraphic { void Add(IGraphic graphic); void Remove(IGraphic graphic); IGraphic GetChild(int index); } public class Canvas : ICompositeGraphic { private List<IGraphic> children = new List<IGraphic>(); public void Draw() { foreach (var child in children) { child.Draw(); } } public void Add(IGraphic graphic) => children.Add(graphic); public void Remove(IGraphic graphic) => children.Remove(graphic); public IGraphic GetChild(int index) => children[index]; } public class Line : IGraphic { public void Draw() { /* Implementation for drawing a line */ } } public class Circle : IGraphic { public void Draw() { /* Implementation for drawing a circle */ } }

  22. Problm, transparence 1. e en :

  23. public interface IGraphic { void Draw(); bool IsComposite(); } class Canvas : IGraphic { ... public bool IsComposite() => true; } public class Line : IGraphic { public void Draw() { /* Implementation for drawing a line */ } public bool IsComposite() => false; } public class Circle : IGraphic { public void Draw() { /* Implementation for drawing a circle */ } public bool IsComposite() => false; }

  24. Problm, transparence 2. e en :

  25. public interface IGraphic { void Draw(); // Default implementation of GetComposite that returns null IGraphic GetComposite() => null; } public class Canvas : IGraphic { ... public IGraphic GetComposite() => this; } public class Line : IGraphic { public void Draw() { /* Implementation for drawing a line */ } } public class Circle : IGraphic { public void Draw() { /* Implementation for drawing a circle */ } }

  26. Composite + Other Patterns

  27. Builder: - p ivytv en slo it ch kompozitn ch strom - jeho konstruk n kroky m ete naprogramovat tak, aby pracovaly rekurzivn Iterator: - pro pr chod composite stromem bez znalosti vnit n reprezentace Visitor: - pro proveden operac nad composite stromem

  28. Decorator: tak spoleh na rekurzivn kompozici pro uspo d n neomezen ho po tu objekt pouze jedna pod zen komponenta Decorator p id v zabalen mu objektu dal povinnosti, zat mco Composite pouze "s t " v sledky sv ch d t . Spolupr ce - pomoc Decoratoru m ete roz it chov n konkr tn ho objektu ve stromu Composite.

  29. Dky za pozornost

Related


More Related Content