Abstract

class Application { List docs; protected abstract new Document(); public void newDocument() { // Handles the File|New menu option doc = new Document(); docs.add(doc); doc.open(); } // ... rest of the class omitted } (a) The abstract Application class class MyApplication extends Application { protected new Document() { return new MyDocumentType(); // A concrete subtype } // ... rest of the class omitted } (b) One possible concrete subclass Figure 7.4: Implementing pattern FACTORY METHOD with dynamically bound factories. newDocument method uses ordinary construction syntax (implemented using our notion of a factory) rather than the nonstandard “factory method” dictated by the pattern. Syntactically, the invocation of a dynamically-bound factory defined in class C for objects of class S is written as c.new S(· · · ), where c is an instance of class C. The prefix “c.” can be dropped for code inside class C (so it is replaced with this). It is not a coincidence that this looks very much like the JAVA syntax for creating an instance of a dynamic inner class: c.new I(· · · ), where c is an instance of the containing class (possibly this) and I is the inner class’s name. The constructor of a (non-static) inner class in JAVA is a method of the containing class, and not of the class it constructs— just like a client-side factory is a member of the containing class, and not of its target class. In fact, Nystrom, Chong and Myers [16] have shown that if the concept of inner classes is extended (using nested inheritance), most of the need for the FACTORY METHOD design pattern disappears. But while nested inheritance has many distinct advantages with regard to code modularity and the creation of extensible software systems, it only solves the VOL 6, NO. 6 JOURNAL OF OBJECT TECHNOLOGY 125 BETTER CONSTRUCTION WITH FACTORIES need for factory methods for classes defined inside the same module as their client. Also, it does not remove the need for instance-management patterns like INSTANCE POOL or FLYWEIGHT.

Full Text
Paper version not known

Talk to us

Join us for a 30 min session where you can share your feedback and ask us any queries you have

Schedule a call

Disclaimer: All third-party content on this website/platform is and will remain the property of their respective owners and is provided on "as is" basis without any warranties, express or implied. Use of third-party content does not indicate any affiliation, sponsorship with or endorsement by them. Any references to third-party content is to identify the corresponding services and shall be considered fair use under The CopyrightLaw.