Package com.ebao.java.innerclass;
Public interface Service {
void Method1 ();
void Method2 ();
}
Public interface Servicefactory {
Service GetService ();
}
public class Implementation1 implements Service {
Private Implementation1 () {}
@Override
public void Method1 () {
System.out.println ("Implementation1 method1");
}
@Override
public void Method2 () {
System.out.println ("Implementation1 method2");
}
public static Servicefactory factory = new Servicefactory () {
Public Service GetService () {
return new Implementation1 ();
}
};
}
public class Implementation2 implements Service {
Private Implementation2 () {}
@Override
public void Method1 () {
System.out.println ("Implementation2 method1");
}
@Override
public void Method2 () {
System.out.println ("Implementation2 method2");
}
public static Servicefactory factory = new Servicefactory () {
Public Service GetService () {
return new Implementation2 ();
}
};
}
public class Factorys {
public static void Serviceconsumer (servicefactory fact) {
Service s = fact.getservice ();
S.method1 ();
S.METHOD2 ();
}
public static void Main (string[] args) {
Serviceconsumer (implementation1.factory);
Serviceconsumer (implementation2.factory);
}
}
Operation Result:
Implementation1 method1
Implementation1 METHOD2
Implementation2 method1
Implementation2 METHOD2
The constructors for Implementation1 and Implementation2 are private, and there is no need to create a named class as a factory. In addition, a single factory object is often required, so in this case it is created as a static domain in the service implementation. The resulting syntax is also more practical.
The following is an exercise that is implemented with an inner class and an anonymous inner class, respectively.
Inner class:
Package com.ebao.java.innerclass;
public class Chapter10practice7 {
Private String field;
private void Method () {
System.out.println ("Chapter10practice7.method ()");
System.out.println ("Chapter10practice7.field:" +field);
}
Class inner{
void Innermethod () {
field = "Updated by Innermethod ()";
Method ();
}
}
Inner Getinner () {
return new Inner ();
}
public static void Main (string[] arg) {
Chapter10practice7 cp = new Chapter10practice7 ();
Inner ir = Cp.getinner ();
Ir.innermethod ();
}
}
Operation Result:
Chapter10practice7.method ()
Chapter10Practice7.field:updated by Innermethod ()
Anonymous inner class:
Package com.ebao.java.innerclass;
Abstract class Chapter10practice12inner {
abstract void Innermethod ();
}
public class Chapter10practice12 {
Private String field;
private void Method () {
System.out.println ("Chapter10practice12inner.method ()");
System.out.println ("Chapter10practice12inner.field:" +field);
}
Chapter10practice12inner Getinner () {
return new Chapter10practice12inner () {
void Innermethod () {
field = "Updated by Innermethod ()";
Method ();
}
};
}
public static void Main (string[] arg) {
Chapter10practice12 cp = new Chapter10practice12 ();
Chapter10practice12inner ir = Cp.getinner ();
Ir.innermethod ();
}
}
Operation Result:
Chapter10practice12inner.method ()
Chapter10Practice12Inner.field:updated by Innermethod ()
Why internal classes are required:
In general, an inner class inherits from a class or implements an interface, and the inner class's code action creates an object of its outer class. So it can be thought that the inner class provides some kind of window into its outer class.
One question that the inner class must answer is: if you just need a reference to an interface, why not implement that interface through the perimeter class? The answer is: "If this satisfies the demand, then it should be done." "So what's the difference between an internal class implementing an interface and enclosing a class implementing an interface?" The answer is: the latter does not always enjoy the convenience of the interface, sometimes need to use the implementation of the interface, so the most attractive reason to use the inner class is that each inner class can be independently inherited from a (interface) implementation, so whether the peripheral class has inherited a (interface) implementation, for the inner class has no effect.
Using internal classes, there are also the following features:
- An inner class can have more than one instance, and each instance has its own state information. And the enclosing information is independent of each other.
- In a single perimeter class, you can have multiple inner classes implement the same interface in different ways, or inherit the same class.
- The time to create an inner class object does not depend on the creation of the perimeter class object.
- The inner class does not have a confusing "is-a" relationship; it is a separate entity.
For example, if the Sequence.java in the following example does not use an inner class, you must declare "sequence is a selector" and there can be only one selector for a particular sequence. However, using an inner class can easily have another method Reversseselector (), which is used to generate a selector that is traversed in the opposite direction. Only internal classes have this flexibility.
Package com.ebao.java.innerclass;
public class Sequence {
Private object[] items;
private int next = 0;
Private String field;
public Sequence (int size, String field) {
Items =new Object[size];
This.field = field;
}
public void Add (Object x) {
if (Next < Items.length)
items[next++] = x;
}
Private class Sequenceselector implements selector{
private int i = 0;
public Boolean end () {
return i = = Items.length;
}
Public Object current () {
return items[i];
}
public void Next () {
if (i < items.length) i++;
}
}
Selector Reverseselector () {
return new Selector () {
private int i = items.length-1;
public Boolean end () {
return i = =-1;
}
Public Object current () {
return items[i];
}
public void Next () {
if (i >= 0) i--;
}
};
}
Public Selector Selector () {
return new Sequenceselector ();
}
public static void Main (string[] args) {
Sequence Sequence = new Sequence ("Test1");
for (int i=0;i<10;i++) {
Sequence.add (integer.tostring (i));
}
Selector Selector = Sequence.selector ();
while (!selector.end ()) {
System.out.print (selector.current () + "");
Selector.next ();
}
System.out.println ();
System.out.println ("reverseselector================================================================");
Selector reverseselector = Sequence.reverseselector ();
while (!reverseselector.end ()) {
System.out.print (reverseselector.current () + "");
Reverseselector.next ();
}
}
}
Operation Result:
0 1 2 3 4 5 6 7 8 9
reverseselector================================================================
9 8 7 6 5 4 3 2 1 0
Java Anonymous class factory method [Java programming thought 10.6.1]