Java basis of common design patterns

One: the abstract factory pattern 

  Factory model means that other factories around to create a super plant. The plant is also known as super other factories factory. This type of design pattern belongs create schema, which provides the best way to create objects. In the abstract factory pattern, the interface is responsible for creating a related object factory, do not need to explicitly specify their class. Each generated in accordance with the object factory can provide factory model.

  1 / * 
  2 * Interface painting 
  . 3 * / 
  . 4 public interface Color { 
  . 5 // fill color 
  . 6 void Fill (); 
  . 7 } 
  . 8 
  . 9 
 10 
 . 11 / ** 
 12 is the shape of the interface * 
 13 is * / 
 14 public interface the Shape {  15 / / painting shape  16 void Draw ();  . 17 }. 19 18 is 20 is 21 is the implements GreenColor class public Color Color {// implementation class public void 22 is Fill () 23 is {System.out.println ( "--- green" ); 24 25} } public class 26 is the implements redColor Color Color {// implementation class public void 27 Fill () 28 {System.out.println ( "red ---" ); 29 } 30} 31 32 33 34 35 public class TriangleShape implements Shape {// Shape implementation class public void 36 Draw () 37 [{System.out.println ( "triangle ---" ); 38 is } 39 } public class CircleShape the implements 40 the Shape {// Shape implementation class public void 41 is Draw () 42 is {System.out.println ( "--- round" ); 43 } 44 } 45 46 47 48 / ** 49 * abstract factory, two interfaces configured * 50/51 is public abstract class AbstractFactory {public abstract color 52 is the getColor (string color); // obtained according to the kind of object color string 53 public abstract shape getShape (string shape ); // obtained according to the kind of object shape string 54 } 55 56 57 58 public class ColorFactory extends AbstractFactory {// color plant inherits the abstract factory 59 public color getColor (string color) {// what kind of object 60 if (null == color) obtained according to the color string return null { ;} 61 if ( "red".equalsIgnoreCase (color)) {62 return new RedColor (); // instantiate a target redColor the else} IF 63 is ( "Green" .equalsIgnoreCase (Color)) {64 return new new GreenColor (); // instantiate an object is GreenColor 65 } 66 return null ; 67 } 68 public getShape the shape (shape string) {; // kind of object to obtain the shape 69 return null string ; 70 } 71 is } public class 75 72 73 is 74 {// ShapeFactory the extends AbstractFactory shape plant Inherited abstract factory 76 public color getColor (string color) { 77 return null // which objects obtained according to the color string ; 78 } 79 public getShape the shape (shape string) {; // get what kind of object 80 if the shape of the string (Shape == null) {return null ;} IF 81 ( "Triangle" .equalsIgnoreCase (Shape)) {82 return new new CircleShape (); // instantiate a CircleShape objects 83} else if ( "circle".equalsIgnoreCase (shape)) {84 return new TriangleShape (); // instantiate a TriangleShape objects 85 } 86 return null ; 87 } 88 } 89 90 91 is 92 {// public class FactoryBuilder parent plant, constructing concrete factory object 93 public static AbstractFactory getFactory (String Factory) {IF 94 ( "colorfactory" .equalsIgnoreCase (Factory)) {95 return new new ColorFactory ();} the else IF 96 ( "shapefactory" .equalsIgnoreCase (Factory)) {97 return new new shapeFactory (); 98 } 99 return null ; 100 } 101 } 102 103 104 105/106 ** * test class abstract factory model * 107/108 public class AbstractFactoryPattern 109 {public static void main (String [] args) {// Get color plant 110 111 AbstractFactory af = FactoryBuilder.getFactory ( "colorfactory "); // Create 112 Color Red Red = af.getColor objects 113 ( "Red" ); // 114 output 115 red.fill (); // create red objects 1 16 117 Color green = af.getColor ( "green " ); 118 // output 119 green.fill (); 120 121 System.out.println ( "parting line ------------------------- - " ); 122 = 123 AbstractFactory AbstractFactory FactoryBuilder.getFactory (" shapefactory " ); 124 = abstractFactory.getShape the Shape Circle (" CIRCLE " ); 125 circle.draw (); 126 = the Shape Triangle abstractFactory.getShape (" Triangle " ); 127 triangle.draw (); 128 } 129}

 

II: Proxy design pattern

  Proxy mode refers to an object to provide a proxy object, the proxy object controlled by reference to the original object. Acting can be divided into static and dynamic proxy agent. 
Through a proxy mode, you can use the proxy object to add additional functionality to the proxy object, the proxy object in order to expand functionality. The method can be used to calculate the execution time of a certain, logging and other operations before and after the execution of a method.

 

III: singleton design pattern

  Refers to a single-mode embodiment, only one class creates an instance of the object model is divided into lazy mode and the ruffian.

    Starving type: constructor privatization, external can not produce a new object is instantiated, instantiate an object can only be achieved by static method. There is no thread safety issues.

Public class. 1 TestSingleton { 
 2 // constructor privatization 
 . 3 Private TestSingleton () {} 
 4 // class load, create an instance of the object 
 . 5 Private static testSingleton = Final TestSingleton new new TestSingleton (); 
 . 6 // external provide a method returns the object instance 
 . 7 public static TestSingleton getTestSingleton () {  . 8 return testSingleton;  . 9 } 10}

 

    Lazy man: the need to add genlock, genlock affect the efficiency of program execution.

      1), determining if double

Public class. 1 TestSingleton { 
 2 
 . 3 / ** 
 . 4 * volatile key role as a command, to ensure that this directive is not omitted by optimizing compiler, 
 5 * and each requires a direct reading. 
 6 * volatile variable so that each time I use are taken from the main memory. Rather than "working memory" of each thread. 
 Declare an object variable * 7 
 8 * volatile disable command is mainly due to rearrangements new TestSingleton (); problem may occur 
 . 9 * / 
10 = Private testSingleton TestSingleton volatile static null ; 
. 11 
12 is privatized // constructor 
13 is Private TestSingleton () {} 
14 
15 / ** 
16 * External provide a method returns an object instance 
17 * double check if locked (synchronized) to ensure thread safety. 
* 18 is / 
. 19 public staticTestSingleton getTestSingleton () {  20 is // IF determination, the object has not been instantiated  IF 21 is (testSingleton == null) {  22 // lock  23 is the synchronized (TestSingleton.class ) {// IF 24 determines that the object has not been instantiated IF 25 (testSingleton == null ) {= 26 is testSingleton new new TestSingleton (); 27 } 28 } 29 } 30 return testSingleton; // 31} returns an instance of an object

 

      2), the internal class

Public class. 1 TestSingleton { 
 2 
 . 3 // constructor privatization 
 . 4 Private TestSingleton () {} 
 . 5 
 . 6 // static inner class, in which instantiated private static external object 
 . 7 Private static class InternalClass { 
 . 8 // instantiate external object 
 . 9 Private static TESTSINGLETON = Final TestSingleton new new TestSingleton ();  10  }  . 11  12 is provided external // method  13 is public static TestSingleton getTestSingleton () {14 return InternalClass.TESTSINGLETON; 15 } 16}

 

Four: Builder design pattern

  Builder mode means that the management of a variety of products together, to create composite objects, so-called composite objects refers to a class have different properties.

Five: Adapter design pattern

  An adapter pattern is converted into an interface, the client interface represents a further desirable, since the goal is to eliminate compatibility problems caused by unmatched interface class. Three main types: type adapter, the adapter is an object mode, mode of the interface adapter.

   Adapter: When too Method A interface, but to achieve his B sub-class need only implement wherein twelve, then a New Class C implement this interface, class C, the method implemented methods are empty, and B sub-class inherit the class C, methods will need rewriting.

 

Six: MVC design pattern

  An adapter pattern is converted into an interface, the client interface represents a further desirable, since the goal is to eliminate compatibility problems caused by unmatched interface class. Three main types: type adapter, the adapter is an object mode, mode of the interface adapter.

    

  

Guess you like

Origin www.cnblogs.com/in-the-game-of-thrones/p/11299210.html