Prove
Prove
1. What is abstraction?
Abstraction is the process through which something complex is simplified. It involves
capturing the most important and essential aspects while discarding unnecessary
details. With abstraction, only the required elements are shown, while background
details are hidden.
The Video class abstracts the concept of a video, encapsulating attributes such as
the title, the author, the length, and the associated comments. Internally, it manages
the list of comments, the number of comments, and the information about the video.
1.2. How did using these principles help your final project become more flexible
for future changes?
Abstraction allows code to be more flexible. The Comment and Video classes can be
reused in different parts of the project or even in other projects. If there is a project
that handles comments and videos, these classes can be reused without needing to
reimplement them.
In the YouTube Videos code, we have the Comment class, and since the code
collects comments, it could also collect "likes". The Comment class can be adapted
accordingly:
FROM:
class Comment
{
public string CommenterName { get; private set; }
public string Text { get; private set; }
TO:
class Comment
{
public string CommenterName { get; private set; }
public string Text { get; private set; }
public int Likes { get; private set; } = 0;
** public int Likes { get; private set; } = 0; and public void Like() { Likes++; }
can be added to have the option to aggregate “likes”.
2. What is encapsulation?
Encapsulation means hiding or wrapping of data and methods into a single unit.
2.1. How did I use encapsulation in the final project - Online Ordering?
In the Online Ordering project, there are: Class Address, customer, product, and
order.
In the class Class Customer, there are encapsulated attributes like _name and
_address. These classes are private. On the other hand, Name and Address are
public properties and they give controlled access to the previous attributes (_name
and _address). There is a method: IsInUSA. Ths method encapsulates the logic for
determining if the customer is in the USA, using the address information.
2.2. How did using these principles help your final project become more flexible
for future changes?
This project is about sales, so, I think that is possible to add a discount feature to the
Order class. First must be added a private _discount attribute and a method to set
the discount. Then, in the class Program, specifically in the Main method, would be
possible to set the discount for an order.
FROM:
class Order
{
private List<Product> _products = new List<Product>();
private Customer _customer;
TO:
class Order
{
private List<Product> _products = new List<Product>();
private Customer _customer;
private decimal _discount = 0;
3. What is inheritance?
Inheritance establishes a parent-child relationship between two classes. Thanks to
inheritance, child classes automatically inherit the methods and properties of the parent
class.
Inheritance aids in the reusability and abstraction of code. It allows a method to be
reused in multiple places.
3.2. How did using these principles help your final project become more flexible
for future changes?
If I want to add a new event type. It is possible without modifying the existing Event
class.It is possible to add the new class and add it to the list of events.
FROM:
class Program
{
static void Main(string[] args)
{
Address address1 = new Address("123 Main St", "Anytown", "NY", "USA");
Address address2 = new Address("456 Maple Ave", "Othertown", "CA", "USA");
Address address3 = new Address("789 Oak Blvd", "Thistown", "TX", "USA");
TO:
class Program
{
static void Main(string[] args)
{
Address address1 = new Address("123 Main St", "Anytown", "NY", "USA");
Address address2 = new Address("456 Maple Ave", "Othertown", "CA", "USA");
Address address3 = new Address("789 Oak Blvd", "Thistown", "TX", "USA");
In this example, the New event class integrates seamlessly with the existing code,
demonstrating the flexibility by inheritance.
4. What is polymorphism?
The loop, activity.GetSummary() calls the appropriate GetSummary method for each
specific activity type, demonstrating polymorphic behavior.
4.2. How did using these principles help your final project become more flexible
for future changes?
It is possible to create a new activity type, such as Hiking, by creating a new class
that inherits from Activity and overrides the necessary methods. The existing code
will handle the new activity type without modification, like this: