一、抽象类和抽象方法
Java提供一种叫做抽象方法的机制,这种方法是不完整的,仅有方法声明而没有方法定义,如下:
abstract void f();
包含抽象方法的类叫做抽象类,建立抽象类的目的是希望提供一个通用接口,以此表示所有导出类的共同部分,不同的子类可以用不同的方式表示此接口 。如果一个类包含一个或多个抽象方法,该类必须被限定为抽象的,如下:
public abstract class AbstractClassExample {
//类体
}
如果从一个抽象类继承,并想创建该新类的对象,那么就必须为基类中的所有抽象方法提供方法定义;如果不提供的话,那么导出类也应该声明为抽象类,示例如下:
public abstract class AbstractClassExample {
public abstract void fun1();
public void fun2(){
System.out.println("AbstractClassExample fun2()");
}
}
class AbstractClassExampleChild extends AbstractClassExample{
@Override
public void fun1() {
System.out.println("AbstractClassExampleChild fun1()");
}
public void fun2(){
System.out.println("AbstractClassExampleChild fun2()");
super.fun2();
}
}
class AbstractClassExampleTest{
public static void main(String[] args) {
AbstractClassExample example = new AbstractClassExampleChild();
example.fun1();
example.fun2();
}
}
二、接口
使用interface关键字将会产生一个完全抽象的类,它根本就没有提供任何具体的实现,它只能包含方法声明,且方法默认为public的。接口中也可以包含域,但所有的域都是隐式的static和final的。一个类如果要实现接口中的方法,可以使用implements关键字,示例如下:
/**
* author Alex
* date 2018/12/9
* description 一个示例接口
*/
public interface InterfaceExample {
public int NUM = 10;
public void fun1();
}
class InterfaceExampleImpl implements InterfaceExample{
@Override
public void fun1() {
System.out.println("InterfaceExampleImpl fun1()");
}
}
class InterfaceExampleTest{
public static void main(String[] args) {
InterfaceExample example = new InterfaceExampleImpl();
System.out.println(InterfaceExample.NUM);
example.fun1();
}
}
三、完全解耦
1、策略设计模式
先看一下耦和比较比较紧的示例:
/**
* author Alex
* date 2018/12/9
* description 对处理器进行扩展
*/
public class Processor {
public String name(){
return this.getClass().getSimpleName();
}
public Object process(Object input){
return input;
}
}
class Upcase extends Processor{
public String process(Object input){
return ((String)input).toUpperCase();
}
}
class Downcase extends Processor{
public String process(Object input){
return ((String)input).toLowerCase();
}
}
class Apply{
public static void process(Processor p,Object s){
System.out.println(p.name());
System.out.println(p.process(s));
}
}
class ProcessorTest{
public static void main(String[] args) {
String str = "This is test text";
Apply.process(new Upcase(),str);
Apply.process(new Downcase(),str);
//打印结果如下:
//Upcase
//THIS IS TEST TEXT
//Downcase
//this is test text
}
}
像上例中,能够根据所传递的参数对象的不同而具有不同行为的方法,被称为策略设计模式。这类方法包含所要执行的算法中固定不变的部分,而策略包含变化的部分,策略就是传递进去的参数对象,它包含要执行的代码。在上例中,每一个Processor对象就是一个策略。
在上例中,Apply.process()方法跟Processor类耦和过紧,导致本来可以复用的Apply.process()方法无法被复用。这时我们对上例进行简单的改造,将接口与实现进行分离。松耦和的示例如下:
/**
* author Alex
* date 2018/12/9
* description 对处理器接口进行扩展
*/
public interface Processor1 {
String name();
Object process(Object input);
}
class Apply1{
public static void process(Processor1 p,Object s){
System.out.println(p.name());
System.out.println(p.process(s));
}
}
//由客户端程序员遵循接口标准来编写他们自己的实现类
abstract class StringProcessor implements Processor1{
public String name(){
return this.getClass().getSimpleName();
}
public abstract String process(Object input);//此抽象方法由导出类实现
}
class Upcase1 extends StringProcessor{
public String process(Object input){
return ((String)input).toUpperCase();
}
}
class Downcase1 extends StringProcessor{
public String process(Object input){
return ((String)input).toLowerCase();
}
}
class StringProcessorTest{
public static void main(String[] args) {
String str = "This is test text";
Apply1.process(new Upcase1(),str);
Apply1.process(new Downcase1(),str);
//打印结果如下:
//Upcase
//THIS IS TEST TEXT
//Downcase
//this is test text
}
}
2、适配模式和代理模式的结合
在实际工作中,我们经常会发现无法修改你想要使用的类,这时就可以使用适配器模式来利用已有的接口产生新的接口,如下示例:
public interface Processor1 {
String name();
Object process(Object input);
}
class Apply1{
public static void process(Processor1 p,Object s){
System.out.println(p.name());
System.out.println(p.process(s));
}
}
class WaveForm {
private static long counter = 0;
private final long id = ++counter;
public String toString(){
return this.getClass().getSimpleName() + counter;
}
}
class FilterNew{
public String name(){
return this.getClass().getSimpleName();
}
public WaveForm process(WaveForm input){
return input;
}
}
class LowPass extends FilterNew{
double cutoff;
LowPass(double cutoff){
this.cutoff = cutoff;
}
public WaveForm process(WaveForm input){
return input;
}
}
class HighPass extends FilterNew{
double cutoff;
HighPass(double cutoff){
this.cutoff = cutoff;
}
public WaveForm process(WaveForm input){
return input;
}
}
//进行接口适配
class FilterAdapter implements Processor1{
FilterNew filterNew;
FilterAdapter(FilterNew filterNew){
this.filterNew = filterNew;
}
@Override
public String name() {
return filterNew.name();
}
@Override
public Object process(Object input) {
return filterNew.process((WaveForm) input);//进行向下转型
}
}
class FilterAdapterTest{
public static void main(String[] args) {
WaveForm w = new WaveForm();
Apply1.process(new FilterAdapter(new LowPass(5.0)),w);
Apply1.process(new FilterAdapter(new HighPass(10.0)),w);
//打印结果如下:
//LowPass
//WaveForm1
//HighPass
//WaveForm1
}
}
四、Java中的多重继承
1、继承一个类,实现多个接口
如果要从一个非接口的类继承,那么只能继承一个类,其余的类都必须是接口,需要将这些接口置于implements关键字之后,用逗号将它们隔开。示例如下:
public class Father {
void drinkWine(){
System.out.println("Father喝酒");
}
}
interface Person{
void work();
}
interface Student{
void study();
}
class Son extends Father implements Person,Student{
@Override
public void work() {
System.out.println("Son工作");
}
@Override
public void study() {
System.out.println("Son学习");
}
}
class FatherTest{
public static void main(String[] args) {
Son son = new Son();
son.drinkWine();//导出类调用基类的方法
son.work();//方法重写
son.work();//方法重写
//打印结果如下:
//Father喝酒
//Son工作
//Son工作
}
}
2、通过继承来扩展接口,继承多个接口
继承多个接口仅限于接口本身,普通的类不能多重继承,只能实现多个接口。示例如下:
public interface Interface1 extends Interface2,Interface3{
void fun1();
}
interface Interface2{
void fun2();
}
interface Interface3{
void fun3();
}
class InterfaceUser implements Interface1{
@Override
public void fun3() {
System.out.println("fun3()");
}
@Override
public void fun2() {
System.out.println("fun2()");
}
@Override
public void fun1() {
System.out.println("fun1()");
}
}
class InterfaceTest{
public static void main(String[] args) {
InterfaceUser user = new InterfaceUser();
user.fun1();
user.fun2();
user.fun3();
//打印结果:
//fun1()
//fun2()
//fun3()
}
}
五、接口与工厂
接口是实现多重继承的途径,而生成遵循某个接口的对象的典型方式是工厂方法设计模式。这与直接调用构造器不同,我们在工厂对象上调用的是创建方法,而该工厂对象将生成接口的某个实现的对象。通过这种方式,我们的代码将完全与接口的实现分离,这就使得我们可以透明地将某个实现替换为另一个实现。简单示例如下:
public interface Service {
void fun();
}
interface ServiceFactory{
Service getService();
}
class Implementation implements Service{
@Override
public void fun() {
System.out.println("Implementation fun()");
}
}
class ImplementationFactory implements ServiceFactory{
@Override
public Service getService() {
return new Implementation();
}
}
class ServiceTest{
public static void service(ServiceFactory factory){
Service service = factory.getService();
service.fun();
}
public static void main(String[] args) {
ServiceTest.service(new ImplementationFactory());
//打印结果:Implementation fun()
}
}
总结:
对于创建类,几乎在任何时刻,都可以替代为创建一个接口和一个工厂。但任何抽象性都应该是由真正的需求产生的,当必需时,你应该重构接口,而不是到处添加额外级别的间接性,并由此带来额外的复杂性。恰当的原则应该是优先选择类而不是接口。