In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces how many patterns there are in the Java design pattern, which is very detailed and has a certain reference value. Friends who are interested must read it!
There are 23 Java design patterns, and here are only five common design patterns:
1. Singleton mode (it is said in some books that singleton mode is actually the same):
The main purpose of this mode is to keep 1 object in memory.
Take a look at the following example:
Package org.sp.singleton; / / method-public class Singleton {/ / sets its own instance object to a property with the Static and final modifiers private static final Singleton instance = new Singleton (); / / sets the constructor to private form private Singleton () {} / / provides an instance of this class public static Singleton getInstance () {return instance to the outside world through a static method Method 2 class Singleton2 {private static Singleton2 instance2 = null; public static synchronized Singleton2 getInstance () {if (instance2 = = null) instance2 = new Singleton2 (); return instance2;}}
Note: these two Java design patterns achieve the same function, but it is recommended to use the * method.
two。 Factory model
The main function of the Java design pattern is to provide references to instance objects uniformly.
Take a look at the following example:
View plaincopy to clipboardprint? Public class Factory {public ClassesDao getClassesDao () {ClassesDao cd = new ClassesDaoImpl (); return cd;}} interface ClassesDao {public String getClassesName ();} class ClassesDaoImpl implements ClassesDao {public String getClassesName () {System.out.println ("Class A");}} class test {public static void main (String [] args) {Factory f = new Factory () F.getClassesDao (). GetClassesName ();}} public class Factory {public ClassesDao getClassesDao () {ClassesDao cd = new ClassesDaoImpl (); return cd;}} interface ClassesDao {public String getClassesName ();} class ClassesDaoImpl implements ClassesDao {public String getClassesName () {System.out.println ("Class A");}} class test {public static void main (String [] args) {Factory f = new Factory () F.getClassesDao (). GetClassesName ()
This is the simplest example of getting a reference to an object through an interface through a factory method.
3. Construction mode
The pattern actually means that the composition of an object may be made up of many other objects. for example, the implementation of an object is very complex and has many properties, and these properties are references to other objects. maybe the references to these objects include a lot of object references. By encapsulating these complexities, the build pattern can be used.
4. Facade mode
This model personally feels like a replica of the Service layer. For example, Dao we define a lot of persistence methods, we use the Service layer to compose the atomic methods of Dao into business logic, and then provide services to the upper layer through the methods. The facade model is actually the same.
Take a look at this example:
View plaincopy to clipboardprint? Interface ClassesDao {public String getClassesName ();} class ClassesDaoImpl implements ClassesDao {public String getClassesName () {return "Class A";}} interface ClassesDao2 {public String getClassesName ();} class ClassesDaoImpl2 implements ClassesDao {public String getClasses2Name () {return "Class B";}} class ServiceManager {private ClassesDao cd = new ClassesDaoImpl () Private ClassesDao2 cd2 = new ClassesDaoImpl2 (); public void printOut () {System.out.println (cd.getClassesName () + "+ cd2.getClassesName ());}}; interface ClassesDao {public String getClassesName ();} class ClassesDaoImpl implements ClassesDao {public String getClassesName () {return" Class A ";}} interface ClassesDao2 {public String getClassesName ();} class ClassesDaoImpl2 implements ClassesDao {public String getClasses2Name () {return" Class B " }} class ServiceManager {private ClassesDao cd = new ClassesDaoImpl (); private ClassesDao2 cd2 = new ClassesDaoImpl2 (); public void printOut () {System.out.println (cd.getClassesName () + "+ cd2.getClassesName ());}}
Although this example is incomplete, the basic meaning is already clear.
5. Strategy mode
This pattern abstracts the behavior, that is, when several classes have similar methods, extract all the common parts of them, thus making it easier to extend.
Take a specific look at this example:
View plaincopy to clipboardprint? Package org.sp.strategy; / * addition specific strategy class * @ author endless de waltz * * / public class Addition extends Operation {@ Override public float parameter (float a, float b) {return aquib;}} package org.sp.strategy / * * Division specific policy class * @ author endless de waltz * * / public class Division extends Operation {@ Override public float parameter (float a, float b) {return a float b;} package org.sp.strategy / * * multiplication specific strategy class * @ author endless de waltz * * / public class Multiplication extends Operation {@ Override public float parameter (float a, float b) {return aquib;}} package org.sp.strategy / * * subtract specific strategy class * @ author endless de waltz * * / public class Subtration extends Operation {@ Override public float parameter (float a, float b) {return amurb;}} package org.sp.strategy / * Abstract policy classes can also use interfaces instead of * @ author endless de waltz * * / public abstract class Operation {public abstract float parameter (float a, float b);} package org.sp.strategy / * Policy environment class * @ author endless de waltz * * / public class Condition {public static final Addition add = new Addition (); public static final Subtration sub = new Subtration (); public static final Multiplication mul = new Multiplication (); public static final Division div = new Division ();} package org.sp.strategy / * Test client * @ author endless de waltz * * / public class Client {public static void main (String [] args) {float a = 100; float b = 25; System.out.println (Condition.div.parameter (a, b)) }} these are all the contents of the article "how many patterns are there in Java Design patterns". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
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.