Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!nx02.iad01.newshosting.com!newshosting.com!69.16.185.21.MISMATCH!npeer03.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail From: Lew Newsgroups: comp.lang.java.programmer Subject: Re: state design pattern: question: inner or outer class: which is better? Date: Wed, 30 Nov 2011 11:22:47 -0800 (PST) Organization: http://groups.google.com Lines: 21 Message-ID: <4652620.1006.1322680967944.JavaMail.geo-discussion-forums@prfi36> References: <4cb57cb9-f87c-4409-9e35-184bdc661f48@l24g2000yqm.googlegroups.com> Reply-To: comp.lang.java.programmer@googlegroups.com NNTP-Posting-Host: 2620:0:1000:fd2b:224:d7ff:fe69:5838 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 X-Trace: posting.google.com 1322681048 24297 127.0.0.1 (30 Nov 2011 19:24:08 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Wed, 30 Nov 2011 19:24:08 +0000 (UTC) In-Reply-To: <4cb57cb9-f87c-4409-9e35-184bdc661f48@l24g2000yqm.googlegroups.com> Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=2620:0:1000:fd2b:224:d7ff:fe69:5838; posting-account=CP-lKQoAAAAGtB5diOuGlDQk0jIwmH0T User-Agent: G2/1.0 X-Google-Web-Client: true Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:10370 John Goche wrote: > I am implementing the state design pattern to manage a set of > sprites in a game. I wonder if anyone could tell me whether it > is better to implement the state classes as inner classes of the > object they are a state of, or as outer classes each being passed > a reference to the sprite object they are being a state for. It all depends. What do you mean by "better"? What is the use case for the sprite type? If the sprite state depends on the outer class's state, an inner class can be a convenient shortcut. As with non-inner nested classes, if the type is needed by any other class and its semantics are not tightly bound to those of the proposed outer class, a standalone class is probably more appropriate. If the semantics are tightly bound to the proposed outer class, and the sprite state does not depend on the outer class instance's state, then a nested class might be appropriate. -- Lew