Pattern to create polymorphic objects with dependency constraint
<p>First, my structure :</p>

<p>AbstractObject<br>
--ObjectA<br>
--ObjectB</p>

<p>AbstractOtherObject<br>
--OtherObjectA<br>
--OtherObjectB</p>

<p>With polymorphism, I want to create an <code>AbstractOtherObject</code> based on my <code>AbstractObject</code>. More precisely, I want an <code>OtherObjectA</code> if it is an <code>ObjectA</code>, and same for B.<br>
The important point is that I don't want  a dependency to <code>AbstractOtherObject</code> in <code>AbstractObject</code>. I can't make an abstract method<code>AbstractOtherObject toOtherObject()</code> in <code>AbstractObject</code>.  </p>

<p>I can't find a solution that doesn't use <strong>instanceof</strong>.  </p>

<p>Thanks in advance.</p>

<p>Update : pseudo code</p>

<p>If I didn't have the dependency constraint I would do this :</p>

<pre><code>public class Any{
    void do(AbstractObject o){
        AbstractOtherObject otherO = o.toOtherObject();
        doSomething(otherO);
    }
}
</code></pre>

<p>But since I don't want a dependency, I am only able to do something ugly like :</p>

<pre><code>public class Any{
    void do(AbstractObject o){
        AbstractOtherObject otherO;
        if(o instanceof ObjectA){
            otherO = new OtherObjectA(o);
        } else {
            otherO = new OtherObjectB(o);
        }
        doSomething(otherO);
    }
}
</code></pre>
