views:

112

answers:

3

Hello

When creating inheritance hierarchy, i get confused .Abstract Base class should contain only commonly used stuffs for inherited classes.But knowing which is commonly used methods,fields and properties is not possible while designing.So i feel free myself to grouping them with abstract classes which inherits hierarchicaly. This is what i am doing without care about which my real classes where will be in hierarchy because i will find easily a place in comprehensive hierarchy.And also it is highly possible to find some unnecessary abstract classes in middle of hierarchy.But i don't know is this right way of designing.And which disadvantages can occur in none-perfect abstract hieararchy.I have to give clear example for it.Think chess and pieces.If i have to define pieces like pawn,queen,king...etc.look at below uml. alt text

I divided hieararcy to 2 slided and none-slided pieces after Piece abstract class.Because i believe that sliding and none-sliding will require different commonly used members.But i can also divide them directional because some pieces can go 8 direction ,some of can 4 direction and some of can go to 2 or 3 three direction. Grouping them directional cause some new questions.Where they will be in hierarchy ? they will come after sliding classes ? If so possible to find four directional grouping under sliding and none-sliding groups and we know that it is not possible to inherited by 2 classes.So for such situations i have to choose interfaces ? if directions will never be under both so it is possible to use abstract classes.Thats ok then if i found again new common grouping which will not require inherited by 2 classes so i can define it under directions.In the end of design ,all of my pieces can find perfect leaf nodes in hierarchy and this will be really good for future to have enough comprehensive building which i will not need change something in hierarchy.

But what can be disadvantage to creating too large comprehensive hieararchy ? Can be that in auto-complete of IDE can show many unnecessary and strange named abstract base classes which confuse others ? what can be other disadvantages ?

+2  A: 

Way, way too complicated for the task. You're overdoing the OO. This means you will have to fill 20-odd classes with code. You should really simplify this.

Consider thinking about it in terms of attributes (color, isUnderAttack) and behavior (moving). In this case, you only need one class class ChessPiece, which has properties for color and isUnderAttack. You can then use some form of dependency injection. Consider how this code would look:

public class ChessPiece
{
    public enum ChessPieceColor{White, Black}
    private IChessMove behavior;
    public ChessPiece(IChessMove behavior, ChessPieceColor color)
    {
        this.behavior = behavior;
    }
    public void Move()
    {
        behavior.Move();
    }
}

public interface IChessMove
{
    void Move();
}

public class KnightMove : IChessMove
{
    public void Move()
    {
        // code to perform the moving.
    }
}

public class Program
{
    static void Main()
    {
        ChessPiece knight = new ChessPiece(new KnightMove(), ChessPiece.ChessPieceColor.White);
    }
}

Then you would simply code a different IChessMove for each type of piece. Obviously you would need to add more information to the methods here to make it actually work, but it should show you the pattern you should be using here. A class for every possible piece is going way too far, when many have common behaviors.

drharris
Yes,we can use field which holds color ,instead of seperate classes for white and blacks.This is what i have also thought but in my application simplifying is more important for me .So seperate them will be really more helpfull.Yes ,it is important to define methods and even its important what i will do in method bodies.These can cause changing in hiearcy.Putting color member into classes will cause checks color of piece in methods and this is what i didn't prefer.
Freshblood
I don't think you really understand the purpose of OOP. Classes are used to separate behavior, not properties of the object. A white pawn does not move any differently than a black pawn. It's simply a condition they have to check when they are trying to capture a piece. Making 2 classes for every piece is NOT simplifying things. This means you have to write movement code twice for every piece. Plus, it's a slippery slope. Who's to say that you shouldn't then extend WhitePawn into classes called WhitePawn1, WhitePawn2, WhitePawn3, etc? The current diagram is a nightmare in terms of maintenance.
drharris
I don't think that i am far away to understand purpose of it.White and black pawns have different moving behaviour on board.But u are a little right that not all pieces has different behaviours.For example bishops,queens,knights and rooks have same behaviours .But Evaluation methods which i will add later will require different methods and method bodies.So movegeneration methods not will be written twice if not require.Base class of them will have common movegeneration for them.But pawns have different behaviour .
Freshblood
As u said extend them again whitePawn1,whitePawn2 ...etc i wouldn't do this clear mistake .Because There is no difference between whitePawn1 and whitePawn2.Still current diagram doesn't look like a nightmare for me.Still i really didn't understand what is wrong on it.I will test this hierarchy and see results .
Freshblood
+1  A: 

Ok... The problem is, that inheritacne hierarchies are fragile - they can be easily useless, for example in cases you describe as your concerns. You can design the hierarchy in many ways, as you suggest, but keep in mind the Liskov Substitution Principle and most importantly, that you should not overuse inheritance. Use it only if necessary. You should not use abstract class and inheritance just because you can. I am not good at chess, but do pieces of different colors have different behaviours? There is a famous example of problem with abstraction when creating the inheritance hierarchy. Take a circle and ellipse, which is more abstract? When you try to make any of them superclass of the other, you will end up with inflexible design. Try to understand more about object oriented programming and try to inherit only when no other choice is better.

Gabriel Ščerbák
color of pieces cause different behaviors.we can overcame it by define member which holds color of piece .But this is not what i prefer .Still i don't know what is disadvantage of such way .Even it looks it will more simplify my app.Think we will only define pieces WhitePawn or BlackPawn etc it has no any importance to what they are inherited and how was hierarchy because all hierarchy is abstract except real pieces classes .Am i right ?
Freshblood
i will test either ways and will have experience what is bad and good but asking before try it can be helpfull and save time.Already my chess engine play chess but pieces is not even a class they are just defined by integer.I want to going more deep into OOP and simplify codes more and have codes which is not too much in methods
Freshblood
+1  A: 

But what can be disadvantage to creating too large comprehensive hieararchy ?

The disadvantage of having an overly complex model is that it makes the code harder to write, read, and maintain.

It happens though. Its very hard to design thing right on the first shot. Its better not to try.

This is what test driven development is for. Rather then hypothesize on what you need, you use tests to flesh out your requirements which in turns flesh out an implementation. TDD ftw.

Frank Schwieterman