Constant interface mode
1. There are two definitions of interfaces:
(1) API for external.
(2) Statement of interface.
About the design pattern of the interface:
(1) Custom Service mode: By setting each service as an interface, each service can be granular.
If you need a combination of a particular service, you only need to extend the fine-grained interface directly.
Java does not allow multiple inheritance, that is, a subclass can have only one parent class, son extends Fathera,fatherb is wrong in order to compensate for this deficiency, Java allows the implementation of multiple interfaces, the interface is to give some no content method, similar to the virtual class in C + +. To the specific use of the method to define their own content, to be aware of the interface must be implemented to implement all methods of the interface.
In fact, because Java does not support multiple inheritance to produce an interface, interface is used to standardize the class of it can avoid class in the design of inconsistencies, which in the development of multi-human cooperation is still important, such as the interface has a method, then the implementation of this interface must implement a method, which forms a specification, that is, a inherits B, But I want to use a method of C, but can not inherit, so we use the C interface. Java interfaces and Java abstract classes represent abstract types, which are the concrete manifestations of the abstraction layer we need to propose. OOP object-oriented programming, if you want to increase the reuse rate of programs, increase the maintainability of the program, extensibility, it must be interface-oriented programming, oriented to abstract programming, the correct use of interfaces, abstract classes, these too useful abstract types as the top layer of your hierarchy.
- Interface internetservice{
- public Void Connect ();
- public void Disconnect ();
- }
- Interface mailservice{
- public Void Send ();
- public void receive ();
- }
- Interface virusservice{
- public Void Begin ();
- public void End ();
- }
- Class Internetserviceimpl implements internetservice{
- public Void Connect () {
- System.out.println ("Start connecting network ...");
- }
- public void Disconnect () {
- System.out.println ("Start off network ...");
- }
- }
- Class Mailserviceimpl implements mailservice{
- public Void Send () {
- System.out.println ("Start sending mail ...");
- }
- public void receive () {
- System.out.println ("Start receiving mail ...");
- }
- }
- Class Virusserviceimpl implements virusservice{
- public Void Begin () {
- System.out.println ("Start check for viruses ...");
- }
- public void End () {
- System.out.println ("Check virus complete ...");
- }
- }
- Interface Menu1 extends internetservice,mailservice{
- }
- Interface Menu2 extends internetservice,virusservice{
- }
- Class Menu1impl implements menu1{
- private Internetservice is;
- private Mailservice MS;
- Public Menu1impl (Internetservice is,mailservice ms) {
- this.is = is;
- this.ms = ms;
- }
- public Void Connect () {
- Is.connect ();
- }
- public void Disconnect () {
- Is.disconnect ();
- }
- public Void Send () {
- Ms.send ();
- }
- public void receive () {
- Ms.receive ();
- }
- }
- Class Menu2impl implements menu2{
- private Internetservice is;
- private Virusservice vs;
- Public Menu2impl (Internetservice is,virusservice vs) {
- this.is = is;
- This.vs = vs;
- }
- public Void Connect () {
- Is.connect ();
- }
- public void Disconnect () {
- Is.disconnect ();
- }
- public Void Begin () {
- Vs.begin ();
- }
- public void End () {
- Vs.end ();
- }
- }
- Public class servicedemo{
- public static void Main (String args[]) {
- Internetservice is = new Internetserviceimpl ();
- Mailservice ms = new Mailserviceimpl ();
- Virusservice vs = new Virusserviceimpl ();
- Menu1impl m1 = new Menu1impl (IS,MS);
- Menu2impl m2 = new Menu2impl (IS,VS);
- System.out.println ("Detect menu1 Function ...");
- M1.connect ();
- M1.disconnect ();
- M1.send ();
- M1.receive ();
- System.out.println ("Detect menu2 Function ...");
- M2.connect ();
- M2.disconnect ();
- M2.begin ();
- M2.end ();
- }
- }
Constant interface mode