Single-Case mode:1: Lazy TypePackage com.design;/* 1: Lazy type */public class Singleton {/*** Private, do not allow instances to be obtained externally through singleton.instance* Static method can only access static variables.*/private static Singleton instance;Private, not allowed to instantiate externallyPrivate Singleton () {}/*** Multithreading calls this method, it is possible to create more than one instance, need to synchronize, but pay attention to the efficiency problem* 1.instance is
, - //so the wake-up process is blocked, and if the consumer thread wakes up, the consumer thread starts to work) theSystem.out.println (Thread.CurrentThread (). GetName () + "production of a product current surplus quantity:" +curnum); the Try { theThread.CurrentThread (). Sleep (250);//control the speed of production the -}Catch(interruptedexception e) { the e.printstacktrace (); the } the}Else{94 the Try { t
Package Creation _ Singleton mode _ registration; Import Java.util.hashmap;import java.util.map;/** * Registered Singleton actually maintains an instance of a set of singleton classes that store these instances in a MAP (register) *, For a single case that has already been registered, it is returned directly from the factory and, for non-registered, first registered, then * returned * @author PP * */public class Regsingleton {/** * register, to hold all registered instances */ private stat
that replaces the new operation with a factory method . (Decide which instance to create based on incoming parameters)Chestnuts:Abstract product role public interface Car {public void drive (); }//Specific product role one public class Benz implements Car {public void Drive () {System.out.println ("Driving B Enz "); }}//Specific product role two public class BMW implements Car {public void Drive () {System.out.println ("Drivi ng BMW "); }}//factory class Role public class Driver {
First, the builder model(Builder)The factory class pattern provides the pattern of creating a single class, while the builder pattern is to centralize the various products to manage, to create composite objects, so-called composite objects that refer to a class that has different properties, in fact the builder pattern is the combination of the previous abstract factory pattern and the last Test. Let's look at the code:As before, a sender interface, two implementation classes MailSender and Smss
Method () {
System.out.println ("This is the second sub!");
}
}
Define a bridge that holds an instance of sourceable:
publicabstractclassbridge{
private sourceablesource;
public Voidmethod () {
source.method ();
}
publicsourceablegetsource () {
returnsource;
}
publicvoidsetsource (Sourceablesource) {
this.source=source;
}
}
public class Mybridge extends Bridge {
The Observer pattern is one of the software design patterns, and is more common, especially in GUI programming. On the design model of the article, the network wrote more, and a lot of writing is good, although there is a repetition of the early wheel of suspicion, but this wheel is not that wheel, the focus is different, the idea is different, the way is not nea
mode, one part is the component construction and the product assembly, the other part is the overall built algorithm . It is important to recognize this because in the construction model, the emphasis is on fixing the overall built algorithm, while flexibly extending and switching the specific construction of the part and the way the product is assembled.To put it bluntly, the focus of the construction model is to separate the construction algorithm and the concrete construction implementation,
); Colleague2 colleague2=new Colleague2 (mediator); Mediator.setcolleague1 (colleague1); Mediator.setcolleague2 (colleague2); Colleague1.send ("How are you doing?" "); Colleague2.send ("Not bad"); } }The intermediary mode applicable scenario
A set of objects communicates in a well-defined but complex way. The resulting interdependence structure is confusing and difficult to understand.
An object that references many other objects and communic
Application Scenarios for Adorner mode:1, need to extend the functionality of a class.2, dynamic for an object to add functionality, but also can be dynamically revoked. (Inheritance cannot do this, the inherited functionality is static and cannot be dynamically deleted.) )Source interface:1 Public Interface sourceable {23 void method (); 4 }SOURCE class:1 Public class Implements sourceable {2 @Override3public void Method () {4 System.out.println ("Source.method () ..."); 5
=Newproductor (product);5 Pool.submit (proc);6Productor PROC2 =Newproductor (product);7 Pool.submit (PROC2);8ListNewArraylist();9 for(intI =0;i){TenCustomer cus =NewCustomer (product); One Cusgroup.add (cus); A pool.submit (cus); - } -Thread.Sleep (1000*1); theProc.stopproc ();//Termination of production -Proc2.stopproc ();//Termination of production -Cusgroup.foreach (Cus-cus.stop ());//Close Customer -Proc.stop ();//Close the producer, (Close the consumer first, or the consum
house. By them choose the house they want */@Overridepublic void Recommendhouse () {zhangsan.selecthouse (); Lisi.selecthouse ();}} /** * Client Test class * * @author Leo */public class Test {public static void main (string[] args) {Mediator mediator = new Myme Diator (); Mediator.createmediator (); Mediator.recommendhouse ();}}Iii. SummaryThe mediator pattern only needs to concern with the Mediator class relationship, the relationship between the detailed class and the class and the call to m
coupled, interdependent, and thus less reusable. The observer pattern is loosely coupled, so reusability can be improved.When do I use observer mode?
When an object model has two facets, one aspect depends on the other. The two are encapsulated in separate objects so that they can be independently changed and reused.
When a change to an object needs to change other objects at the same time, it is not known how many objects need to be changed.
When an object must notify other ob
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.