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
-safe creation instances synchronized(Singleton.class) { //Check again if the instance exists, and if it does not exist, actually create the instance if(Instance = =NULL) {instance=NewSingleton (); } } } returninstance; }}Lazy Loading Internal implementations:Features: Delayed load and thread safety are implemented PackageSingleton; Public classSingleton {PrivateSingleton () {}/*** Class and the inner class, that is, the static me
Class Girl{public void Showappearance () {System.out.println ("The Girl:face without Make Up");}} Class Takeflower extends Girl{girl girl=null;public takeflower (Girl Girl) {this.girl=girl;} public void Showappearance () {girl.showappearance (); Takeflower ();} public void Takeflower () {System.out.println (' Take Flower ');}} Class Takenecklace extends Girl{girl girl=null;public takenecklace (Girl Girl) {this.girl=girl;} public void Showappearance () {girl.showappearance (); Takenecklace ();} p
first, the concept Concept UML diagram Role Usage Scenarios In fact, we often used abstract class is the pattern to use, so basically belong to a better understanding of a pattern.Second, practice According to the above role, give the corresponding code Abstract templates/*** Abstract Template * *@authorAdministrator **/ Public Abstract classAbstracttemplate {/*** Declaration and implementation of template methods*/ Public voidTemplatemethod () {//calling methods implemented by
I. Overview Concept
Decoupling abstraction from implementation so that abstractions and implementations can run independently
UML diagram Role: role Relationship Second, practice Set up the appropriate classes according to the roles above Abstract roles/**@author Administrator * **/publicabstractclass Abstraction { protected implenmentor impl; Public void operation () { impl.operationimpl (); }} Correcting abstract roles/**@author Administrator *
("Landlord Lee", mediator); Landlordb = new landlord ("Landlord Li", mediator); R Enter = new Renter ("Small Lu", mediator);//Landlord Registration intermediary Mediator.registerlandlord (LANDLORDA); Mediator.registerlandlord ( LANDLORDB)///Mediator.registerrenter (renter);//Ask the tenant to send the rental information renter.sendmessage ("Rent a house near Tianhe Park, the price of about 1000 yuan one months"); System.out.println ("--------------------------");//landlord a Send home rental me
("User1 exe!");
}
}
public class User2 extends User {
Public User2 (mediator mediator) {
Super (mediator);
}
@Override
public void work () {
System.out.println ("User2 exe!");
}
}
Test class:
public class Test {
public static void Main (string[] args) {
Mediator Mediator = new Mymediator ();
Mediator.createmediator ();
Mediator.workall ();
}
}
Output:User1 exe!User2 exe!Reprinted fr
) {
this.num2 = num2;
}
}
public class Test {
public static void Main (string[] args) {
Calculate the value of a 9+2-8
int result = new minus (). Interpret (new Context (New Plus ()
. Interpret (new Context (9, 2)), 8));
SYSTEM.OUT.PRINTLN (result);
}
}
Finally output the correct result: 3.Basically, the interpreter pattern is used to make all kinds of interpreters, such as regular expressions and so on!Reprinted from Https://www.
, and if the encapsulation is not important. --------------------------------------------------------------------------------------------- ----- Seven design principles: 1. Single RESPONSIBILITY PRINCIPLE: one class is responsible for one responsibility.2, the Richter replacement principle "LISKOV SUBSTITUTION PRINCIPLE": Inheritance and derivation of the rules.3, the dependency inversion principle "dependence inversion PRINCIPLE": high-level mo
Six basic guidelines for JAVA design patterns to followa single principle of responsibility: (Responsibility pinciple) A class is responsible for only one responsibility. When more than one responsibility is required, new classes need to be added to take responsibility for new responsibilities, rather than personality codes in the class.If a class takes on too ma
give a code to implement the Registrar. The benefit of creating a singleton pattern with the registrar mechanism is that it is easy to manage and can control multiple different types of singleton instances at the same time.
Summary
1. The singleton model can be easily expanded to produce a specified number of instances.
2. In the design Patterns Java Companio
layer, they have to communicate through the interface, this layered mode in the software design with a lot of, must be paid attention to.3) In each layer, there are many small modules, a small module external should also be a whole, then a module external should also provide interfaces, other places need to use the function of this module, should be through this interface.2. Parameters and return values in the interface definition:1) The interface mu
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.