Today's mission involves using multiple Object-Oriented Programming (OOP) principles to tackle complex tasks. When principles like Encapsulation, Abstraction, Polymorphism, and Composition are blended, the resulting code becomes streamlined and easier to manage.
Our goal is to dissect two real-world examples, gaining insights into how these principles can seamlessly orchestrate solutions.
Let's design an online library system, as we aim to reinforce our understanding of Encapsulation
and Polymorphism
. Encapsulation
will help us protect the attributes of books, members, and transactions, ensuring they are accessible in a controlled manner. Polymorphism
will demonstrate its power by enabling a single interface to represent different underlying forms, such as digital and print versions of books.
php1<?php 2 3// Base class for different types of library users 4class Member { 5 private $name; 6 7 public function __construct($name) { 8 $this->name = $name; 9 } 10 11 public function checkOutBook($book) { 12 echo "{$this->name} checked out {$book->getBookType()} book {$book->getTitle()}.\n"; 13 } 14} 15 16// Base class for different types of books 17abstract class Book { 18 private $title; 19 20 public function __construct($title) { 21 $this->title = $title; 22 } 23 24 public function getTitle() { 25 return $this->title; 26 } 27 28 public abstract function getBookType(); 29} 30 31// Inherits from Book, represents a digital book 32class DigitalBook extends Book { 33 public function getBookType() { 34 return "Digital"; 35 } 36} 37 38// Inherits from Book, represents a physical book 39class PhysicalBook extends Book { 40 public function getBookType() { 41 return "Physical"; 42 } 43} 44 45// Library class that manages members and books 46class Library { 47 private $members = []; 48 private $books = []; 49 50 public function addMember($member) { 51 $this->members[] = $member; 52 } 53 54 public function addBook($book) { 55 $this->books[] = $book; 56 } 57} 58 59$myLibrary = new Library(); 60 61$alice = new Member("Alice"); 62$bob = new Member("Bob"); 63 64$myLibrary->addMember($alice); 65$myLibrary->addMember($bob); 66 67$digitalBook = new DigitalBook("The PHP Handbook"); 68$physicalBook = new PhysicalBook("Learning PHP Design Patterns"); 69 70$myLibrary->addBook($digitalBook); 71$myLibrary->addBook($physicalBook); 72 73$alice->checkOutBook($digitalBook); // Outputs: Alice checked out Digital book The PHP Handbook. 74$bob->checkOutBook($physicalBook); // Outputs: Bob checked out Physical book Learning PHP Design Patterns. 75 76?>
In this code snippet, Encapsulation
is observed clearly through the class structures and the controlled access to their properties. Polymorphism
is vividly illustrated by how both DigitalBook
and PhysicalBook
classes inherit from the Book
class but provide their own implementations of the getBookType
method. This setup allows objects of DigitalBook
and PhysicalBook
to be used interchangeably when a book’s type needs to be identified, demonstrating Polymorphism
's capability to work with objects of different classes through a common interface.
Encapsulation
ensures that details about members and books are well-contained within their respective classes.Polymorphism
showcases flexibility by treating different book types uniformly, making the system more adaptive and scalable.
Next, we'll develop a shape-drawing application capable of drawing various shapes. For this, we'll employ the principles of Abstraction
and Composition
.
Abstraction
simplifies the complexity associated with drawing different shapes.Composition
takes care of composite shapes.
Here's how we translate these principles into our shape-drawing application:
php1<?php 2 3// Define the basic Shape class 4abstract class Shape { 5 // Abstract method that will be implemented in each subclass 6 public abstract function draw(); 7} 8 9// Define the Circle class 10class Circle extends Shape { 11 // Implement the draw method for circle 12 public function draw() { 13 echo "Drawing a circle.\n"; 14 } 15} 16 17// Define the Square class 18class Square extends Shape { 19 // Implement the draw method for square 20 public function draw() { 21 echo "Drawing a square.\n"; 22 } 23} 24 25// Define the ShapeComposite class 26class ShapeComposite extends Shape { 27 // Initialize with an empty array of shapes 28 private $shapes = []; 29 30 // Add a new shape to the composite 31 public function addShape($shape) { 32 $this->shapes[] = $shape; 33 } 34 35 // Implement the draw method to draw each shape in the composite 36 public function draw() { 37 foreach ($this->shapes as $shape) { 38 $shape->draw(); 39 } 40 } 41} 42 43$circle = new Circle(); 44$square = new Square(); 45 46// Drawing individual shapes 47$circle->draw(); // Output: Drawing a circle. 48$square->draw(); // Output: Drawing a square. 49 50// Create a ShapeComposite instance for composite shapes 51$compositeShape = new ShapeComposite(); 52 53// Add individual shapes to the composite 54$compositeShape->addShape($circle); 55$compositeShape->addShape($square); 56 57// Drawing the composite shape 58$compositeShape->draw(); 59// Output: 60// Drawing a circle. 61// Drawing a square. 62 63?>
Abstraction: In this example, the Shape
class is abstract. We don’t care about the specific details of how each shape is drawn here, but we know that each shape must have a draw()
method. The abstract class helps us define this rule for all shapes.
Composition: The ShapeComposite
class demonstrates composition by combining multiple shapes. It can hold and draw multiple shapes together. Composition is used when one object (a composite shape) contains other objects (individual shapes).
Well done! You combined multiple OOP principles to respond to complex tasks. By dissecting real-world examples, we understood how these principles found their applications. Now, it's time to put this knowledge to work. Practice fortifies concepts, transforming knowledge into expertise. So, let's get coding!