();Private method of constructionPrivate SingletonDome2 () {}//public static SingletonDome2 GetSgd2 () {if (SGD2 = = null) {SGD2 = new SingletonDome2 ();}return SGD2;}}5> Factory mode (is the method pattern for instantiating objects, i.e. a pattern of replacing the new operation with a factory method, for example: Here is a simple example)/** Static Factory mode*/InterfacePublic interface Alphabet { Alphabetfun (Object ... params);}Implementation of the interface Class Apublic class A impleme
check lock mechanism uses the keyword volatile, which means that the value of a volatile variable is not cached by the local thread, and that all read and write to the variable is directly operating shared memory, ensuring that the variable is handled correctly by multiple threads. Public Static Pet GetInfo () { if(pet==null) { synchronized(pet.class { if(pet==null) { Pet=new Pet (); } } } return
code1.//Product interface2. Public interface Product {3.4.}(2) Product Interface implementation classJava code1.//washing machine2. Public class Washer implements PRODUCT {3.4. Public washer () {5. SYSTEM.OUT.PRINTLN ("The washing machine was made!" ");6.}7.}Java code1.//Air Conditioning2. Public class Aircondition implements Product {3.4. Public aircondition () {5. SYSTEM.OUT.PRINTLN ("Air conditioner is manufactured! ");6.}7.}(3) Factory interfaceJ
In-depth research on java design patterns (reflection)1. The reflection Class in JDK contains:-1, Class, representing a Class.-2, Constructor, representing the class Constructor.-3, Field, representing class members-4, Method, representing the Method.2. Unified Call form: An Example of basic reflection usage is as follows:
package com.use;import java.lang.reflect
template method pattern:1, good package 2, good reuse, 3, shielding details 4, easy to maintainAs for the disadvantage, it is the inheritance problem, in Java can only inherit a parent class.Process: Analyze scenes---step----Refactor code--important, complex algorithms, core algorithms designed as templatesNote the point:Template method needs to be declared as public finalThe private method is the basic logicProtect abstract method is an extensible m
://www.2cto.com/kf/201204/125917.html)3. Two kinds of differences and advantages:A hungry man, because the class is loaded when the instance is created, it will reduce the running time relatively;Lazy, because of the getinstance () method to get the instance plus synchroized (lock) so it is thread-safe, so when the execution of multiple threads, it will be executed one at a time, so it is relatively slow on the run speed, but also because of the time when the thread enters the instance will be c
the agent { SYSTEM.OUT.PRINTLN ("Business logic output------------completed before--------------Agent");} Custom method: The agent finishes after the logical private void Postrequest () {System.out.println ("--------------the agent completes the business logic output-------------");} Main method public static void main (string[] args) {Proxysubject proxysubject = new Proxysubject ();p roxysubject.request ();}} Copyright NOTICE: This article for Bo Master original article, without Bo Master perm
Feel the code is awkward, turn back to the Java 23 design patterns.Today, well, it's so late, sleepy. Let's get a simple singleton pattern.Single-case mode: Commonly known as single dogPackage Singleton;public class SingleTon {Private static Final class Singletonbuilder {private static SingleTon SingleTon = new SingleTon ();}Private SingleTon () {}public static SingleTon getinstance () {return Singletonbuil
1 //application of the interface: the design pattern of the factory method2 public classTestfactorymethod {3 public Static voidmain (string[] Args) {4Iworkfactory i =Newstudentworkfactory ();5 i.getwork (). doWork ();6 7Iworkfactory I1 =Newteacherworkfactory ();8 i1.getwork (). doWork ();9 }Ten } one a Interfaceiworkfactory{ - work getwork (); - } the classStudentworkfactoryImplementsiworkfactory{ - - @Override - publicwork ge
makedifferentfunction ();Define abstract methods to complete different production details (phones of different colors)protectedAbstract void makedifferentcolor ();Define abstract methods to complete different production details (different types of phones)protectedAbstract void makedifferenttype ();}/*** Create real implementation classes to achieve specific production details**/class Concreteclass extends abstractclass{Methods to implement abstractions in the parent classprotected void Makediff
Combinatorial mode is a common design pattern for maintainability-oriented programming. Simply put, a class contains a set of collection of the current class as attributes of the class. This is a certain similarity to recursion. His goal is to establish a tree hierarchy between objects of the same type, and an upper object can contain multiple downlevel objects. For example, a menu can have many options, and these options may contain the next level of
Common Java class libraries-Observer design patterns (Observable class Observer Interface) and observableobserver
Link: http://www.2cto.com/kf/201310/253013.html
To implement the Observer mode, you must rely on the Observable class and Observer interface provided in the java. util package.
1234567891011121314
"); Customer Lisi = new Customer ("John Doe"),/** * Join Customer */food.addobserver (Zhangsan); Food.addobserver (Lisi);/** * update Price */ Food.setprice (1.5f);}}third, the application scenarioGUI frame, meteorological observation, etc.Iv. SummaryBe sure to call the Setchanged () method before notifying all observers to set the state of the observer to be changed so that notifyobservers () will not be updated with the Observer Update method. The above assumes that there are different views o
actually maintains an instance of a set of singleton classes that are stored in a map (the register), returned directly from the map for an instance that has already been registered, and then registered and returned, if not registered. A hungry man and lazy-type differenceThese two look very similar at first glance, in fact there is a difference, the main two points1. Thread Safety:A hungry man-type is thread-safe, can be used directly for multithreading without problems, lazy-style is not, it
race is what race, burn bar for (int i=0;ipublic class Classutils {//to an interface that returns all implementation class public static listThe factory method model also has a very important application, that is, deferred initiation (lazy initialization), what is deferred initialization?When an object is initialized, it is not released, and when it is used again, it is not initialized again, directly from the memory to get it,How to achieve it, very simple, see examples:public class Humanfacto
constructor Method */private Singleton () {};/** * is thread-safe, but reduces the speed of the entire access * @return */PUBL IC Static synchronized Singleton getinstance () {if (uniqueinstance = = null) {uniqueinstance = new Singleton ();} return uniqueinstance;} =================================================================/** * "Double check plus lock": both thread-safe and performance-not-to-be-impact */// Add a volatile modifier to a variable that holds an instance private volatile sta
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.