Inheritance and type flags

I did a few searches (maybe I'm not describing my problem well enough), but could not find an answer for this

Let's say you have the following POCO:

public class StandardObject
{
   public string A {get;set;}
   public string B {get;set;}
}

Elsewhere in the program, there is some logic for handling StandardObjects. There is a case where sometimes a standard object can be handled differently. We know this when we create a StandardObject, however, none of the current properties can be used to determine this further down the chain. One approach would be to add and set a flag or enumeration type in a standard object and verify this when handling objects. For instance.

if(standardObject.IsSpecial){...}

if(standardObject.ObjectType == StandardObjectTypeEnum.Special){...}

This does not seem to be the best approach.

Another option is to create a derived class:

public class SpecialObject : StandardObject { }

Now, instead of checking the property, we can check the type. For instance.

if(standardObject.GetType() == typeof(SpecialObject)){...}

( , , -)

, SpecialObject StandardObject. . , (E.G. SpecialObject), . .

. , . , , , , , StandardObject SpecialObject , ? , ?

:

:

, , , ,

EDIT:

, -, . , , :

  • Object - , , DTO
  • StandardObject.
  • , StandardObject
  • - .
  • , StandardObject ""

, "" , StandardObject, , - StandardObject

+5
3

. , - , - , ( , ).

+3

, , . .

public abstract class Vehicle
{
    private MovementStrategy movementStrategy;

    protected Vehicle(MovementStrategy strategy)
    {
        this.movementStrategy = strategy;
    }

    public void Move()
    {
        movementStrategy.Move();
    }

    public virtual void CommonAlert()
    {
        Console.WriteLine("Base generic vehicle alert");
    }
}

public class Car : Vehicle
{
    public Car(MovementStrategy movementStrategy)
        : base(movementStrategy)
    {
    }

    public override void CommonAlert()
    {
        Console.WriteLine("Car says 'Honk!'");
    }
}

public class Elevator : Vehicle
{
    public Elevator(MovementStrategy movementStrategy)
        : base(movementStrategy)
    {
    }

    public override void CommonAlert()
    {
        Console.WriteLine("Elevator says 'Ding!'");
        base.CommonAlert();
    }
}

public abstract class MovementStrategy
{
    public abstract void Move();
}

public class CarMovementStrategy : MovementStrategy
{
    public override void Move()
    {
        Console.WriteLine("Car moved");
    }
}

public class ElevatorMovementStrategy : MovementStrategy
{
    public override void Move()
    {
        Console.WriteLine("Elevator moved");
    }
}

:

class Program
{
    static void Main(string[] args)
    {
        Vehicle elevator = new Elevator(new ElevatorMovementStrategy());
        Vehicle car = new Car(new CarMovementStrategy());

        elevator.Move();
        car.Move();

        elevator.CommonAlert();
        car.CommonAlert();

        Console.Read();

    }
}
+1

- , . , .

To sketch it out, let's say we have an element that will need to be drawn in a certain way depending on some attribute.

If the class just has processing in it, you just need to do:

MyElement.Draw();

Instead:

if(MyElement.Flag)
{
     DrawBlue(MyElement);
}
else
{
    DrawRed(MyElement);
}

In the calling code, there is no need to disclose a method for determining a specific behavior.

0
source

All Articles