Open/Closed Principle (OCP)

The Open/Closed Principle (OCP) states that software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification. This means that a class should be easily extendable without modifying the class itself.

Bad Example:

A `Report` class has a `GenerateReport` method that contains a type parameter. If a new type of report is needed, the `GenerateReport` method would need to be modified to accommodate the new type.

                
                class Report {
                    public string GenerateReport(string type) {
                        if (type == "pdf") {
                            // Generate PDF report.
                        } else if (type == "html") {
                            // Generate HTML report.
                        }
                    }
                }
                
                

Corrected Example:

A better design would be to have a base `Report` class and different subclasses for each report type. If a new report type is needed, we simply create a new subclass.

                
                abstract class Report {
                    public abstract string GenerateReport();
                }

                class PDFReport : Report {
                    public override string GenerateReport() {
                        // Generate PDF report.
                    }
                }

                class HTMLReport : Report {
                    public override string GenerateReport() {
                        // Generate HTML report.
                    }
                }