"Java Programming Ideas - Chapter 9 (Interface)"

interface

Interfaces and inner classes give us a more structured way of separating the interface from the implementation.

9.1 Abstract classes and abstract methods

Abstract method: Only the declaration has no method body.
Abstract Class: A class that contains abstract methods is called an abstract class. A class containing abstract methods must be abstract.

Abstract class features: objects cannot be created for abstract classes; inheriting abstract classes must override all abstract methods of the base class; abstract classes can have no abstract methods.
Abstract method: abstract void f()

Abstract classes make the abstraction of the class explicit, making it easy to move public methods up the inheritance hierarchy when refactoring.

9.2 Interface

The interface keyword produces a fully abstract class. It allows the creator to determine the method name, parameter list and return type, but without any method body. implements implements an interface.

Interfaces can contain fields, which are static and final by default.

9.3 Complete Decoupling

Only methods operate on classes and not interfaces, then you can only use classes and their subclasses. Interfaces can improve code reusability.
1. Strategy Design
Pattern A method that can behave differently depending on the parameter object passed.

/**
 * 策略设计模式
 * @author Administrator
 *
 */
public class Apply {
    public static void process(Processor p,Object o) {
        System.out.println("Useing Processor" +p.name());
        System.out.println(p.process(o));
    }
    public static String  s  = "Disagreement with beliefs is by definition incorrent";
    public static void main(String[] args) {
        process(new Upcase(), s);
        process(new Downcase(), s);
        process(new Splitter(), s);

    }
}
class Processor{
    public String name() {
        return getClass().getSimpleName();
    }
    Object process(Object input){
        return input;
    }
}
class Upcase extends Processor{
    String process(Object input){
        return ((String)input).toUpperCase();
    }
}

class Downcase extends Processor{
    String process(Object input){
        return ((String)input).toLowerCase();
    }
}

class Splitter extends Processor{
    String process(Object input){
        return Arrays.toString(((String)input).split(" "));
    }
}
  1. Adapter pattern The
    adapter takes the interface you have and produces the interface you need.

/**
 * 适配器模式
 * @author Administrator
 *
 */
public class FilterAdapter implements Processor{

    Filter filter; //代理:选择性的返回需要的方法
    public FilterAdapter(Filter filter) {
        this.filter = filter;
    }

    @Override
    public String name() {
        return filter.name();
    }

    @Override
    public Waveform process(Object input) {
        return filter.process((Waveform) input);
    }

}

Decoupling the interface from the concrete implementation allows the interface to be applied to many different concrete implementations. So the code is more reusable.

9.4 Multiple Inheritance

The act of combining interfaces of multiple classes is called multiple inheritance.
The core reason for using an interface: To upcast to multiple base types; to prevent clients from creating objects of that class.

9.5 Extending Interfaces Through Inheritance

An interface can inherit from multiple interfaces.

interface Monster{
    void menace();
}
interface DangerMonster extends Monster{
    void destory();
}
interface Lethal{
    void kill();
}
class DrangoZilla implements DangerMonster{

    @Override
    public void menace() {}

    @Override
    public void destory() {}

}
interface Vampire extends DangerMonster,Lethal{
    void drinkBlood();
}

When combining interfaces, avoid using the same method name in different interfaces.

9.6 Adapter interface

One of the most attractive reasons for interfaces is to allow multiple different implementations of the same interface.
Common usages are strategy pattern and adapter pattern.

9.7 Domains in Interfaces

Any fields in an interface are static and final.

9.8 Nested Interfaces

Interfaces can be nested within classes and other interfaces.
Interfaces nested within interfaces are automatically public.

9.9 Interfaces and Factories

Interfaces are a way to achieve multiple inheritance, and the typical way to generate objects that conform to an interface is the factory method design pattern .
The create method is called on the factory object, which will generate some implementation object of the interface. In this way, the implementation of the code can be completely separated from the interface.

/**
 * 工厂模式
 * @author Administrator
 *
 */

interface Game{ boolean move();}
interface GameFactory{ Game getGame();}

class Checkers implements Game{
    private int moves = 0;
    private static final int MOVES = 3;
    public boolean move() {
        System.out.println("Checkers move"+moves);
        return ++moves !=MOVES;
    }
}
class CheckersFactory implements GameFactory{
    public Game getGame( ) {
        return new Checkers();
    }
}

class Chess implements Game{
    private int moves = 0;
    private static final int MOVES = 4;
    public boolean move() {
        System.out.println("Chess move"+moves);
        return ++moves !=MOVES;
    }
}
class ChessFactory implements GameFactory{
    public Game getGame( ) {
        return new Chess();
    }
}

public class Games {
    public static void playGame(GameFactory factory) {
        Game g = factory.getGame();
        while(g.move());
    }
    public static void main(String[] args) {
        playGame(new CheckersFactory());
        playGame(new ChessFactory());
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325396871&siteId=291194637