Created
November 24, 2015 15:56
-
-
Save daveshah/23b32f00a227dc66238a to your computer and use it in GitHub Desktop.
Scoping Java objects - using package protected scope to ensure abstractions don't bleed out
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.lolz; | |
| public class AwesomeSauce { | |
| { | |
| // Note that the interface is package protected so trying to access these outside the | |
| // package is no-bueno! | |
| Foo.Bar bar = new Foo.Bar(); | |
| Foo.Baz baz = new Foo.Baz(); | |
| //THIS is the weird syntax I was talking about - "declaring 'new' off of an instance" | |
| Foo.Bar.BarBazzer1 bazzer1 = bar.new BarBazzer1(); | |
| Foo.Bar.BarBazzer2 bazzer2 = bar.new BarBazzer2(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.lolz; | |
| //Package level interface within Foo.java - This will provide package only access to the | |
| // classes defined within the interface | |
| interface Foo { | |
| class Baz { | |
| public void doTheBaz() { | |
| } | |
| } | |
| class Bar { | |
| class BarBazzer1 { | |
| void doTheBazzer() { | |
| } | |
| } | |
| class BarBazzer2 { | |
| void doTheBazzer() { | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment