Merge Object

A rare sort of refactoring which results from finding that two or more classes don't do enough and that their instances are normally found in pairs. Often marked by MutualHasaRelationship.

For example, a change from this:

 class Foo {
   int mydata;
   Bar myBar;
   Foo(int initial) { mydata = initial; }
   void acquaint(Bar b) { if (myBar) return; myBar = b; b.acquaint(this); }
   void act() { if (!myBar) return; doSomethingWithMydata(); myBar.notify(); }
   void notify() {} // don't care
 }

class Bar { int mydata; Foo myFoo; Bar(int initial) { mydata = initial; } void acquaint(Foo f) { if (myFoo) return; myFoo = f; f.acquaint(this); } void act() { if (!myFoo) return; doSomethingWithMydata(); myFoo.notify(); } void notify() { System.out.println("Foo did something!"); } }

to this:

 class FooBar {
   int foodata, bardata;
   FooBar(int f, int b) { foodata = f; bardata = b; }
   void fooAct() { 
     doSomethingWithFoodata();
     System.out.println("Foo did something!");
   }
   void barAct() { doSomethingWithBardata(); }
 }

-- KarlKnechtel


EditText of this page (last edited August 25, 2003) or FindPage with title or text search