oop - OO design and mirrored/duplicated methods -


I'm having an issue with OO design, where I end up with duplicate code in 2 different classes. What's going on about:

In this example, I want to find out the confrontation between game objects.

I have a base CollisionObject and CollisionObjectBox, CollisionObjectCircle, CollisionObjectPolygon that have the common ways to expand the base class (such as checkForCollisionWith).

This part of the design looks fine, but here is what is bothering me: calling

  aCircle checkForCollisionWith: aBox  

Inside a circle versus circle subclasses, the box will test the collision. In reverse,

  aBox checkForCollisionWith: aCircle  

The box will check the cycle collision inside the box subclass

To release here. Circle vs box collision code is duplicate, because it is both in box and circle classes. Is there any way to avoid this, or am I getting this problem wrong? For now, I'm leaning towards having a supportive class with all duplicate codes and calling it with acrylic and object objects to avoid duplicate. I'm curious, so it's more elegant OO solution to do, though.

itemprop = "text">

I was having the same problem (in Objective-C), and for an alternative solution I found this to solve the collision when I already know the type for both objects to define an external function.

For example, if I apply the rectangle and circle to both a protocol (like the interface for this language).

  @protocol size - ( Bool): (another size of the & lt; size & gt;); - (bool) intersectsWithCircle: (Circle *) aCircle; - Dissection with cutting (BOOL): (rectangle *) a reference;  

This intersects for the circle for the circle likeWithCircle, and intersectsWithRectangle defined

  - (bool) intersectsWithCircle: (Circle *) aCircle {Return CircleAndRectangleCollision (ACircle, Self); }  

and ...

  - (bool) intersectsWithRectangle: (rectangle *) aRectangle {return CircleAndRectangleCollision (self, aRectangle); }  

Of course this is not a double dispatch coupling problem, but at least this code avoids duplication


Comments

Popular posts from this blog

c# - How to capture HTTP packet with SharpPcap -

php - Multiple Select with Explode: only returns the word "Array" -

jquery - SimpleModal Confirm fails to submit form -