<< Chapter < Page Chapter >> Page >
  • Pass such a concrete factory to all client code that need to make use of the abstract factory to manufacture concrete IList instances.

Below is an example of a unit test for the InsertInOrderWithFactory algorithm.

Test_InsertInOrderWithFactory.java
package listFW.visitor.test; import listFW.*;import listFW.factory.*; import listFW.visitor.*;import junit.framework.TestCase; /*** A JUnit test case class. * Every method starting with the word "test" will be called when running* the test with JUnit. */public class Test_InsertInOrderWithFactory extends TestCase {public void test_ordered_insert() { IListFactory fac = CompositeListFactory.Singleton;IListAlgo algo = new InsertInOrderWithFactory(fac);IList list0 = fac.makeEmptyList(); assertEquals("Empty list", "()", list0.toString());IList list1 = (IList) list0.execute(algo, 55);assertEquals("55->()", "(55)", list1.toString()); IList list2 = (IList) list1.execute(algo, 30);assertEquals("30->(55)", "(30, 55)", list2.toString());IList list3 = (IList) list2.execute(algo, 100); assertEquals("100 ->(30, 55)", "(30, 55, 100)", list3.toString());IList list4 = (IList) list3.execute(algo, 45); assertEquals("45->(30, 55, 100)", "(30, 45, 55, 100)", list4.toString());IList list5 = (IList) list4.execute(algo, 60); assertEquals("60 ->(30, 45, 55, 100)", "(30, 45, 55, 60, 100)", list5.toString()); }}

The above design process is an example of what is called the Abstract Factory Design Pattern .  The intent of this pattern is to provide an abstract specification for manufacturing a family of related objects (for examples, the empty and non-empty IList ) without specifying their actual concrete classes thus hiding all details of implementation from the user.

Our example of the list structure framework successfully delineates specification from implementation and faithfully adheres to the principle of information hiding.

  • IList , IMTList , INEList , IListAlgo , and IListFactory provide a minimal and complete abstract specification.
  • InsertInOrderWithFactory is a concrete implementation of IListAlgo that performs a concrete operation on the host list.  Yet this algorithm need only communicate with the list structure and the list factory via their public interface.  It will work with any implementation of IList and IListFactory .
  • CompositeListFactory is a concrete implementation of IListFactory .  It uses the composite pattern and the visitor pattern to implement IList .  It only communicates with IListAlgo at and knows nothing about any of its concrete implementation.  The private static attributes provide the proper mechanism to hide all implementation details from all code external to the class.

 

Click here to access the complete javadoc documentation and UML class diagram of the list component described in the above.

Click here to download the complete source code and documentation of the list component described in the above.

4. frameworks

The following is a direct quote from the Design Patterns book by Gamma, Helm, Johnson, and Vlissides (the Gang of Four - GoF).

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Principles of object-oriented programming. OpenStax CNX. May 10, 2013 Download for free at http://legacy.cnx.org/content/col10213/1.37
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Principles of object-oriented programming' conversation and receive update notifications?

Ask