Get the App
SLTechnology News&Howtos  ›  Development  › 

Example Analysis of Abstract Class and template method pattern in java

Shulou Source: shulou.com Published: 2022-06-03 19:22:14 09月18日 Update

This article mainly introduces the java abstract classes and template method pattern example analysis, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian with you to understand.

I. the concept of abstract category

A method without method body can be defined in 1.Java, and the method is implemented by subclasses.

two。 The method without method body is called abstract method, and the class containing abstract method is called abstract class.

Second, the characteristics of abstract methods

1. A method that has only a method header but no method body is called an abstract method.

two。 Abstract methods are decorated with abstract

3. An abstract method represents an uncertain operation or behavior.

4. Abstract methods cannot be called

Third, the characteristics of abstract category

1. Classes that contain abstract methods in their definitions are called abstract classes.

two。 Abstract classes are decorated with abstract

3. An abstract class represents an abstract object type

4. Abstract classes cannot be instantiated

5. There can be concrete methods in abstract classes, and there can be no abstract methods.

Fourth, create abstract classes and abstract methods:

Abstract method:

V. the use of abstract classes and abstract methods

Abstract classes cannot be instantiated, so subclasses can inherit abstract classes for instantiation.

6. Template method mode 1. Define

A template method defines an algorithm with some abstract operations, and subclasses redefine these operations to provide concrete behavior

two。 Intention

The template method pattern defines the steps of an algorithm and allows one or more step subclasses to provide its specific behavior for one or more steps.

Let the subcategory redefine some of the steps in the algorithm without changing the algorithm architecture.

3. Pattern structure

Define abstract classes and provide an algorithmic framework for all subclasses

Specific subclasses that provide the specific implementation of the workflow

Calling class

4. Example

1. Define abstract classes and provide an algorithm framework for all subclasses (of course, multiple algorithm frameworks can also be provided)

two。 Two specific subclasses are defined, each of which is responsible for the specific implementation of the workflow of a platform.

3. The calling class can select the corresponding subclass instantiation according to the platform on which the code runs, and call the algorithm framework.

Package com.demo;abstract class Control {/ / create template method abstract void initUart (); abstract String getCommand (); abstract void openLight (); abstract void closeLight (); / / create workflow 1 public void work () {String cmd; initUart (); cmd = getCommand () / / execute different processes if (cmd = = "open") {openLight ();} else if (cmd = = "close") {closeLight () }} / / N workflows can also be created} class Arduino extends Control {/ / Workflow concrete implementation void initUart () {System.out.println ("Arduino initUart");} String getCommand () {System.out.println ("Arduino getCommand"); return "open" } void openLight () {System.out.println ("Arduino openLight");} void closeLight () {System.out.println ("Arduino closeLight");}} class RaspberryPi extends Control {/ / Workflow concrete implementation void initUart () {System.out.println ("raspberryPi initUart") } String getCommand () {System.out.println ("RaspberryPi getCommand"); return "close";} void openLight () {System.out.println ("raspberryPi openLight");} void closeLight () {System.out.println ("raspberryPi closeLight") }} public class Test {public static void main (String [] args) {/ / Arduino execution workflow Arduino one = new Arduino (); one.work (); / / RaspberryPi execution workflow RaspberryPi two = new RaspberryPi (); two.work ();}}

Running result:

Thank you for reading this article carefully. I hope the article "sample Analysis of Abstract classes and template method patterns in java" shared by the editor will be helpful to you. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Tags: Method subclass process workflow algorithm work template pattern instance framework step article example multiple behavior analysis representative platform feature run Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Linux NVidia Apple Huawei