This is much better because our leaf object doesn’t need to implement the methods it won’t use. Only our composite object will implement this interface, but the leaf object won’t. What we are going to create instead is a new interface – IGiftOperations: public interface IGiftOperations Now, in many examples, we can see additional operations like add and remove inside the abstract class, but we are not going to add them in this class, because our Leaf class doesn’t need them. These fields and method are going to be used as an interface between the Leaf and the Composite part of our pattern. We can see that our component consists of two protected fields and one abstract method. Public abstract int CalculateTotalPrice() So, let’s start with the Component part: public abstract class GiftBase As we can see, we have a tree structure representing our complex gift so, implementing the Composite design pattern will be the right solution for us. The gift could be a single element (toy) or it can be a complex gift that consists of a box with two toys and another box with maybe one toy and the box with a single toy inside. Let’s imagine that we need to calculate the total price of a gift which we are selling in our shop.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |