Hello, welcome back! Today, we will decode the fundamentals of Revising Basic Design Patterns — Composition! A vital component of software design patterns, composition aids us in creating complex classes using simpler ones. Our journey today includes understanding the concept of composition, its value in software development, and how to practically implement it in C#
.
To kick-start our exploration, let's understand Composition
. In object-oriented programming (OOP), composition allows a class to include other classes, paving the way for the creation of complex systems out of simpler components. For instance, when building a car, we bring together independent pieces like the Engine
, Wheels
, and Seats
— a perfect reflection of composition in everyday life. Note that in composition, should the parent object (the car) be destroyed, the child objects (the components) also cease to exist.
Now, let's translate the theory into a C#
code application. Transforming the previously mentioned car example, a Car
class in C#
is created by making objects of the Engine
, Wheels
, and Seats
classes. The Car
class owns these child objects; their existence is dependent on the Car
.
C#1using System; 2 3class Engine 4{ 5 public void Start() 6 { 7 Console.WriteLine("Engine starts"); // Engine start message 8 } 9 10 public void Stop() 11 { 12 Console.WriteLine("Engine stops"); // Engine stop message 13 } 14} 15 16class Wheels 17{ 18 public void Rotate() 19 { 20 Console.WriteLine("Wheels rotate"); // Wheel rotation message 21 } 22} 23 24class Seats 25{ 26 public void Adjust(string position) 27 { 28 Console.WriteLine("Seats adjusted to position " + position); // Seat adjustment message 29 } 30} 31 32class Car 33{ 34 private readonly Engine _engine; 35 private readonly Wheels _wheels; 36 private readonly Seats _seats; 37 38 public Car() 39 { 40 _engine = new Engine(); 41 _wheels = new Wheels(); 42 _seats = new Seats(); 43 } 44 45 public void Start() 46 { 47 _engine.Start(); // Call to start engine 48 _seats.Adjust("upright"); // Adjust seat position 49 _wheels.Rotate(); // Get wheels rolling 50 } 51 52 static void Main(string[] args) 53 { 54 Car myCar = new Car(); 55 myCar.Start(); // Begin car functions 56 } 57} 58 59// Prints: 60// Engine starts 61// Seats adjusted to position upright 62// Wheels rotate
In the above code, the Car
class encapsulates Engine
, Wheels
, and Seats
objects, which are independent but part of the Car
class, forming a composition pattern.
In the composition model, components like Engine
, Wheels
, and Seats
can exist independently. These classes are not subclasses of Car
but are simply used by the Car
to achieve its functionality. This is an important feature of composition because it allows for reuse of these components in other contexts. For instance, the Engine
class could be used in other vehicles like Truck
or Boat
without needing to inherit from a specific Car
class.
In OOP, Composition
and Inheritance
are two significant ways to express relationships between classes. While inheritance implies an "is-a" relationship, composition suggests a "has-a" relationship. For instance, a Car
is a Vehicle
(inheritance), but a Car
has an Engine
(composition).
Superb job! You've now decoded the composition and even implemented it in C#
! Next, you'll encounter stimulating exercises where you'll gain hands-on experience with composition in C#
. Stay curious, and keep practicing to fortify your concepts!