In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.