Basic Principles of java Design Patterns
Two principles have been introduced earlier.Open and closed PrincipleSingle Responsibility PrincipleLet's take a look at another principle: the Dependency inversion principle, which includes two aspects:A. High-level modules should not depend on low-level modules. They should all rely on abstraction.B. abstraction should n
class Sendmailfactory implements Provider {
@Override
Public Sender Produce () {
return new MailSender ();
}
}
public class Sendsmsfactory implements provider{
@Override
Public Sender Produce () {
return new Smssender ();
}
}
In providing an interface:
Public interface Provider {
Public Sender produce ();
}
Test class:
public class Test {
public static void Main (string[] args) {
Pr
Decorative (Decorator) design pattern
Dynamically adding some extra functionality to an object, the decorative pattern is more flexible than generating subclasses for added functionality
The ability to dynamically extend an object without changing the original class file and using inheritance (this is the benefit of the decorative design pattern)
The decorative
A singleton pattern (Singleton pattern) is a very common design pattern. The core is to ensure that there is only one instance of the Singleton class in the system. There are a number of situations in the system that involve configuration data and the generation of unique sequence IDs.Refer to the definition of "Zen in design mode":Ensure a class have only one instance,and provide a global point of access t
passed in, that is to show Likeeat likeeat = new Endeat (New Eatfoot (New Inrestaurant (New Gotorestaurant (New Findinmap (person)))))The look of this structure. So when calling Likeeat.eat () , and because of each specific decorator class, first call super.eat (); method, and the Super has been passed by the construction and pointed to a specific decorator class (this can be replaced according to the order), then called the Decoration class method, and then call their own decorative m
value:A.ceshi1 ();B.ceshi2 ();}}Test the function of a single caseClass a{int num1;Test passed a single-case pass valuepublic void Ceshi1 () {SingleInstance1 S1 = singleinstance1.getinstance (); s1.num = NUM1;//pass NUM1 to Num}}Class b{int num2;Assigning values by passing parameterspublic void Test (a a) {num2 = A.NUM1;}Test passed a single-case pass valuepublic void Ceshi2 () {SingleInstance1 s2 = singleinstance1.getinstance (); num2 = s2.num;//Pass num to num2}}Article original, re
scalabilityBecause the bridging mode separates the abstract part from the implementation part, and thus defines the interface separately, the abstract part and the implementation part can be independently expanded, without affecting each other, and greatly provides the expansibility of the system.(3) can be dynamically switched to achieveBecause the bridging mode realizes the separation of abstraction and implementation, when the bridge mode is implemented, it can realize dynamic selection and
= "Http://img.my.csdn.net/uploads/201309/01/1378037235_7476.jpg"=new Webpimageloader (); Loader.downloadimage (IMAGEURL,200,200);Pattern mode Advantages and disadvantages: 1.) Advantages The template method pattern removes duplicate code from subclasses by moving the invariant behavior to the superclass. Subclasses implement some of the details of the algorithm, which facilitates the expansion of the algorithm.By invoking the subclass implementation of a parent class, the new behavior is adde
Userwang=NewUser ("Wangwu"); Server.registerobserver (Userzhang); Server.registerobserver (Userli); Server.registerobserver (Userwang); Server.setinfomation ("PHP is the best language in the world!" "); System.out.println ("----------------------------------------------"); Server.removeobserver (Userzhang); Server.setinfomation ("Java is the best language in the world!" "); }}Test results:6. Summary
This pattern i
JAVA design patterns-Singleton and singletonReprinted please indicate the source: http://blog.csdn.net/l1028386804/article/details/45441169I,Overview
Ensure that a class has only one instance and provides a global access point to it.II,
Applicability
1. When the class can only have one instance and the customer can access it from a well-known access point.
2. Whe
Jobseeker ("Mike"));
Hh.registerobserver (New Jobseeker ("Chris"));
Hh.registerobserver (New Jobseeker ("Jeff"));
Every time you add a job, all job-seeking people are notified.
hh.addjob ("Google Job");
Hh.addjob ("Yahoo Job");
}
The advantages of the Observer pattern The Observer and the observed are mildly correlated and abstract coupled so that It's easier for both to expand. The Observer pattern is a commonly used trigger mechanism that forms a chain o
packagecom.doctor.java.design_pattern;importorg.slf4j.logger;importorg.slf4j.loggerfactory;/ Several implementations of *** Singleton mode--"Java Development technology-experience the beauty of design patterns and algorithms in architecture" ** @author doctor* * @time 2015 April 24 PM 11:11:03*/publicclasssingletonpattern{/*** @param args*/publicstaticvoidmain
Have not been very familiar with design patterns, and do not know how to use. Now slowly 1.1 to understand.Create Pattern: Factory method mode, abstract Factory mode, singleton mode, builder mode, prototype mode structured mode: Adapter mode, decorator mode, proxy mode, appearance mode, bridging mode, combined mode, and enjoy meta mode behavioral mode: Policy mode, template method mode, observer pattern, it
(Singleton2.class) { if(Instance = =NULL) {instance=NewSingleton2 (); } } } returninstance; } }Output: PackageCom.sun.singleton; Public classMain { Public Static voidMain (string[] args) {//instantiate two times for comparison testing//a hungry man typeSingleton s1=singleton.getinstance (); Singleton S2=singleton.getinstance (); if(s1==12) {System.out.println ("A hungry man type is the same instance"); }Else{System.out.println ("A hungry man type is
product family. Summary:whether it's a simple factory model, a factory method model, or an abstract factory model, they all belong to the factory model and are very similar in form and feature, and their ultimate goal is to understand the decoupling. In use, we don't have to care whether this pattern is a factory method or an abstract factory pattern, because the evolution between them is often elusive. Often you will find that the factory method is clearly used, when the new requirements come,
"Singleton Mode"The primary purpose of the singleton design pattern is to have only one instance of the class present in the entire system. such as global configuration information, a factory, or a master control class Public class Singleton { privatestaticnew Singleton; Private Singleton () {} Public Static Singleton getinstance () { return Singleton; } }"Factory mode"Three elements: interfaces, classes that implement i
{public double createoperation (double Numbera, Double numberb,string oper);}2, create a function of the implementation of the interface, we put this subtraction algorithm is written in this implementation class, the class can be loaded when the initialization, this way, you need to use the direct call./** * Created by LK on 2016/5/8. */public class Operationfunction {private static mapNow, the difference with non-functional is that we don't need to create a lot of implementations and create
: Rewrite, their relationship is a parent-child relationship, must be a method name, the type, number, order of parameters, and the return value must be the same as the parent class. * Overload: Overloads, their relationship of the same kind of different methods, method names are the same as the rest can be different*/@Override String process (Object input) {return(String) (input). toUpperCase (); }}/**the subclass of process, with the ability to convert lowercase letters **/classDowncaseextends
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.