changes of the person being observed4. The actual observer classConcretobserver inherits Observer interface, implements update ()Observer instance:Publicinterfacestudentsubject{publicvoidaddobserver (ParentObserverone); Publicvoidmoveobserver (Parentobserverone);p ublicvoidnotifyobserver ();} packagesuperhakcetoolspackage;importjava.util.arraylist;publicclassstudent Implementsstudentsubject{privatefloatgrades;privatearraylistThis article is from the "Superhakce" blog, make sure to keep this sou
JAVA design patterns ---------- Proxy)
Here is a brief introduction to the use of the most basic proxy.
Agency: Simply put, one person or one institution takes action on behalf of another person or another institution. In some cases, a customer does not want or cannot directly reference an object, but the proxy object can play a mediation role before the client a
The disadvantage of creating an object with the New keyword is that the coupling is too high, and the factory design pattern uses the Java reflection mechanism to instantiate the object to reduce the degree of couplingPackageCn.buaa;Importjava.util.Date;Interfacefruit{ Public voideat ();}classAppleImplementsfruit{@Override Public voideat () {System.out.println ("Eat apples"); }}classOrangeImplementsfruit{@O
PackageShipeiqidemo;/** A common design pattern in Java: Adapter * Source: A subclass to implement the interface, then you have to implement the interface of all the abstract methods, sometimes the abstract method * a lot, but do not need to implement all, so at this time define an abstract class to implement this interface, and then through the subclass to inherit * In this abstract class, this abstract cl
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
for(inti =0; I 5; i++) {Fansobserver F =New Fansobserver("Name"+ (i +1)); Fans.Add(f); Vs.Addfans(f); }//This tweet, 5 fans have received pushVs.Publish("My girlfriend is so nice!" ");//Incredibly show love, the first 2 fans, can not look down, take off! for(inti =0; I 2; i++) {vs.Removefans(fans.)Get(i)); }//This time the Micro Bo, those two take off the person will not seeVs.Publish("I want to send all my fans an iphone X"); }}Run results大V发布微博:我的女朋友真好看!name1 收到大V的微博发
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
5 practical application of design patterns: Factory method patterns, 5 of design patternsDefinition of factory method modeThe factory method model is widely used. The factory method mode is widely used in Java APIs: java. util. th
(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
: 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
();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
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:
[email protected]
and provide relevant evidence. A staff member will contact you within 5 working days.