Path: csiph.com!usenet.pasdenom.info!gegeweb.org!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Steven Simpson Newsgroups: comp.lang.java.programmer Subject: Re: use case for extending enum, but this is not possible in java Date: Sun, 15 Jun 2014 01:04:37 +0100 Organization: A noiseless patient Spider Lines: 74 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Info: mx05.eternal-september.org; posting-host="4fccbded9ef9b91404b6029a15ac5cd3"; logging-data="9097"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19jSXcM7mdR0NSL9g0wUmRSO2IoYOEsGfQ=" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.5.0 In-Reply-To: Cancel-Lock: sha1:gghEDSB+ZxJAcUWjAyPEHFRvXbQ= Xref: csiph.com comp.lang.java.programmer:28736 On 15/06/14 00:24, Laura Schmidt wrote: > In an application, there is an enum ListCommand that enumerates the > commands a user may execute on list entries: > > public enum ListCommand > { > OPEN, > EDIT, > DELETE; > } > > There is an interface that uses this enum: > > public interface ListCommandProcessor > { > void onListCommand (ListCommand cmd); > } > > And there is a generic Listing class that is used to show customized > lists in the GUI and which uses the above interface: > > public class Listing > { > ... > ListCommandProcessor processor; > ... > } > > So far, so good. > > Now I am making a cut: The generic class Listing should be moved > into a generic java library, so that it can be used by different > applications. So I also need to move the interface ListCommandProcesor > into this library, and this would implicate that I also move the enum > ListCommand into the library. But the enum ListCommand ist application > specific. If I move it to the library, it must be extendable somehow. Does your Listing class demand anything of ListCommand? Does it even require it to be an enum? If not, does this help?: // In library public interface Processor { void onListCommand(E cmd); // inappropriate name now? } public class Listing { Processor processor; public void setProcessor(Processor processor) { this.processor = processor; } } // In application public enum AppListCommand { OPEN, CLOSE, STUFF; } public class MyApp extends Processor { { Listing listing = ...; listing.setProcessor(this); } public void onListCommand(AppListCommand cmd) { switch (cmd) { ... } } } Depending on unspecified factors, you might be able to use Processor, or have to use Listing>. -- ss at comp dot lancs dot ac dot uk