Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!news.glorb.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: Fri, 2 Dec 2011 07:27:49 -0800 (PST) Organization: http://groups.google.com Lines: 21 Message-ID: <8815515.407.1322839669883.JavaMail.geo-discussion-forums@preu18> References: <4cb57cb9-f87c-4409-9e35-184bdc661f48@l24g2000yqm.googlegroups.com> Reply-To: comp.lang.java.programmer@googlegroups.com NNTP-Posting-Host: 216.239.45.130 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 X-Trace: posting.google.com 1322841354 18788 127.0.0.1 (2 Dec 2011 15:55:54 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Fri, 2 Dec 2011 15:55:54 +0000 (UTC) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=216.239.45.130; posting-account=CP-lKQoAAAAGtB5diOuGlDQk0jIwmH0T User-Agent: G2/1.0 X-Google-Web-Client: true Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:10427 Roedy Green wrote: > John Goche wrote, quoted or indirectly quoted someone who said : >> 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. > > There are two main reasons to use inner classes: > > 1. When the inner classes need to intimately access the fields of a > particular mother object they are attached to. > > 2. When you want scope to partly shield the inner classes from the > outside. They treated somewhat as if they were part of the mother > class. This second point applies to static nested classes as well. Obviously if the needed "fields of a ... mother object" are not static, then only an inner class will do for point #1. If they are static, then the nested class can be as well. -- Lew