简体   繁体   English

接口>抽象类>具体类模式

[英]Interface > Abstract Class > Concrete Class pattern

I have found a reference architecture where all domain classes (POJOs) inherit an abstract class and, in turn, the abstract class implements a interface. 我找到了一个参考架构 ,其中所有域类(POJO)都继承了一个抽象类,反过来,抽象类实现了一个接口。 For instance: 例如:

public interface User {  
    public abstract operation1();  
    public abstract operation2();  
    ...  
}  

public abstract class AbstractUser implements User {  
    String name;  
    // only attributes
    ...
}  

public abstract class XyzUser extends AbstractUser {
    ...
}  

Do you guys know if this design is some sort of pattern? 你们知道这个设计是不是某种模式? Can you explain why the architecture was designed like that (Interface --> Abstract Class --> Concrete Class)? 你能解释为什么架构是这样设计的(Interface - > Abstract Class - > Concrete Class)?

First understand what is need of interface, abstract class and concrete class. 首先要了解接口,抽象类和具体类的需求。

Let us take example here: 让我们举个例子:

  public interface Vehicle{
     public void startEngine();
     public void run();
  }


 public abstract class Bus implements Vehicle{
      public void startEngine(){
        System.out.println("Engine Starting of bus");
      }
 }

 public abstract class Plane implements Vehicle{
      public void startEngine(){
        System.out.println("Engine Starting of plane");
      }
 }

 public class VolvoBus extends Bus{

      public void run(){
        System.out.println("Running at 100kmp/h");
      }



 }


 public class NonACBus extends Bus{

      public void run(){
        System.out.println("Running at 50kmp/h");
      }
  }


public class Test{

 public static void main(String[] args){
        VolvoBus volvoBus=new VolvoBus();
        NonACBus nonAcbus=new NonACBus();
        volvoBus.startEngine();
        volvoBus.run();
        nonAcBus.startEngine();
        nonAcBus.run();
      }
  }

See in above example we have code which is common for bus whether its AC bus or Volvo so it is written in Bus class but run() is not common for all so instead of implementing in Bus class it is kept as abstract so its child class will implement that base on there requirement. 在上面的例子中我们看到了总线常用的代码,无论是AC总线还是Volvo,所以它是用Bus类编写的,但是run()并不是所有人共同的,所以不是在Bus类中实现它而是保持抽象,所以它的子类将根据那里的要求实施该基础。

My code will explain you better :) Thanks 我的代码会更好地解释你:)谢谢

When designing code that is meant to be extended, it is standard practice to rely only on interfaces. 在设计要扩展的代码时,标准做法是仅依赖于接口。 To make life easier to extenders, boilerplate code is added to abstract classes extending those interfaces. 为了使扩展器更容易生命,将样板代码添加到扩展这些接口的抽象类中。

This is standard OO practice , rather than a "pattern". 这是标准的OO实践 ,而不是“模式”。 A good example can be found in just about all Java Swing widget models. 几乎所有Java Swing小部件模型都可以找到一个很好的例子。 For example TableModel is an interface meant to provide access to a table's data, AbstractTableModel is an abstract class that provides implementations for some simple accounting methods in the interface, and DefaultTableModel is a concrete class that uses, as data storage, an ArrayList . 例如, TableModel是一个用于提供对表数据的访问的接口, AbstractTableModel是一个抽象类,它为接口中的一些简单的计费方法提供实现,而DefaultTableModel是一个使用ArrayList作为数据存储的具体类。

Here the interface User defines a type: 这里接口User定义了一个类型:

public interface User{  
public abstract operation1();  
public abstract operation2();  
...  
}

So implementations of this interface are to be known as of type User . 因此,该接口的实现将被称为User类型。

Now you can provide implementation assistance to the implementers of this interface by using Abstract classes, as interfaces are not permitted to have method implementations. 现在,您可以使用Abstract类为此接口的实现者提供实现帮助,因为不允许接口具有方法实现。 You can provide a skeletal implementation class to go with User interface, which will have default implementations of some of its methods. 您可以提供一个骨架实现类来使用User接口,该接口将具有其某些方法的默认实现。 Here AbstractUser is the skeletal implementation of User : 这里AbstractUserUser的骨架实现:

public abstract class AbstractUser extends IUser  
{ 
public abstract operation1();  
public operation2(){
...
}  
}

You can now write concrete User implementations with the help of AbstractUser : 您现在可以在AbstractUser的帮助下编写具体的User实现:

public class UserImpl extends AbstractUser implements User {
...
}

The Java Collections Framework has a number of skeletal implementation to go along with the main collection interfaces, to minimize the effort required to implement the collection interfaces: AbstractSet , AbstractList , etc. Java Collections Framework具有许多与主集合接口一起使用的骨架实现,以最大限度地减少实现集合接口所需的工作: AbstractSetAbstractList等。

This might be the decorator design pattern. 这可能是装饰设计模式。 U have an interface, an abstract class that implements the interface. 你有一个接口,一个实现接口的抽象类。 Concrete classes extending the abstract class (decorators). 扩展抽象类(装饰器)的具体类。 and another concrete class that only implements the interface (the object which you decorate). 和另一个只实现接口(你装饰的对象)的具体类。 I am not sure. 我不确定。 I cannot tell anything without looking at the entire code. 如果不看整个代码我就说不出来。 But still, have a look at this link. 但是,还是看看这个链接。 It might help you. 它可能对你有帮助。

http://javapapers.com/design-patterns/decorator-pattern/ http://javapapers.com/design-patterns/decorator-pattern/

I have had this kind of architecture implementation. 我有过这种架构实现。 For example here is snippet of code 例如,这里是代码片段

public class Category extends CategoryBase implements ICategory

Another is 另一个是

public class Functional extends CategoryBase implements ICategory

Here interesting thing is CategoryBase abstract class where You are keeping your common properties and functionalities and You are just reusing them with inheritance 这里有趣的是CategoryBase抽象类,你保留了你的公共属性和功能,你只是重用它们继承

Abstract class is a generalized class for example animal is generalized because abstract method and properties are mentioned here. 抽象类是一个广义类,例如动物被概括,因为这里提到了抽象方法和属性。 As many animal has common behaviors and properties. 由于许多动物具有共同的行为和特性。

Concrete Class is a specilized class for a dog which is inherited from animal class (generalized class) to do more specilized for dog only. 混凝土类是狗的特定类,它继承自动物类(广义类),仅为狗做更多的特定。 here we can add some properties and method and even override it's behaviour (from generlized class) 在这里我们可以添加一些属性和方法,甚至覆盖它的行为(来自generlized类)

Interface is a task based class which have only abstract methods which can be implemented in abstract class or/and in concrete class to specilized behaviours 接口是一个基于任务的类,它只有抽象方法,可以在抽象类中实现,或者在具体类中实现为特定行为

Example code here... 这里的示例代码......

    abstract class Animal {
      private String leg;

      abstract void run();
    }

    class Dog extends Animal implements Ibehaviors {

      @Override
      void run() { 
       System.out.println("dog run"); 
      }

      @Override
      void eat() { 
       System.out.println("dog eat"); 
      }

    }


    interface Ibehaviors { 
      void eat(); 
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM