lundi 14 décembre 2020

How to solve this problem in an OOP fashion [closed]

I'm developing a program where various Items can be produced by Machines. Items have certain properties which Machines must consider to decide of they can produce that Item or not. I wish for both Machines and Items to be able to be added to the program easily by the end user by dll (without recompilation).

My current implementation is as follows:

class Item{
   List<Profile> Profiles{get;set;}
}
abstract class Profile{
}
class ProfileA:Profile{
   //some properties
}
class ProfileB:Profile{
   //some properties
}

abstract class Machine{
   void Produce(List<Item> items);
}
class MachineA:Machine{
   void Produce(List<Item> items){
      //loop through items and check their profiles 
      if (item.Profiles.Any(p => p is ProfileA>){
      ...
      }
   }
}
class MachineB:Machine{
   void Produce(List<Item> items){
     //loop through items and check their profiles 
     if (item.Profiles.Any(p => p is ProfileB> &&
         item.Profiles.Any(p => p is ProfileB>){
       ....
    }
   }
}

Each Item is composed of Profiles. These Profiles can be extended by the end user with dlls after release. Machines can also be extended in this form.

This is flexible and understandable, but makes me switch on Profile types within the produce methods of the machines, resulting in many casts attempts to concrete types.

Is there a way to solve this problem, keeping the flexibility of post release addition of types, without excessive casting, in an OOP way, or is this a clean way to solve this problem?

Aucun commentaire:

Enregistrer un commentaire