1. Strategy design modeCreating a method that can vary depending on the parameter object being passed is called a policy design pattern. This type of method contains a fixed portion of the algorithm that is requested to execute, and the "policy" contains the changed parts. A policy is a parameter object that is passed in. In the following code example, the process object is a policy. Applied on the S.code e
combination of objects, it is possible to mix and match the beverage and seasoning more flexibly.Code External details:When implemented in code, the decorator can be passed into the decorator by the constructor, as in the final invocation form as follows:Beverage beverage = new Darkroast ();Beverage = new Mocha (beverage);Beverage = new Whip (beverage);The two-tier wrapper is completed, and the cost () function of the beverage is then called to get the total price.Seven, java.io in the package
... ") ;}}; Class factory{//factory class public static Fruit Getfruit (String className) {Fruit F = null, if ("Apple". Equals (ClassName)) {f = new A Pple ();} if ("Orange". Equals (ClassName)) {f = new orange ();} return f;}; public class Interdemo{public static void Main (String args[]) {Fruit f = factory.getfruit (Args[0]); if (f!=null) {f.eat ();} }}Three, the agent design modeTake debt collection as an exampleInterface give{public void Givemone
) {Mediator Med = new Concretemediator ();The boss is here.Med.notice ("Boss");The client is here.Med.notice ("Client");}}ResultOrdinary employees work hardFront desk attention!Applicability:1. A group of objects communicates in a well-defined but complex way.The resulting interdependence structure is confusing and difficult to understand.2. An object that references very many other objects and communicates directly with those objects makes it difficult to duplicate the object.3. Want to customi
Observer Observableex Public class extends Observable { /** * When the principal changes, the notification method is called @param newstate * /publicvoid change (String newstate) {this . Nodifyobservers (newstate); } }5. Create a clientImportorg.junit.Test; Public classClient {/*** Create three new observers, add them to the list, and call the change method*/@Test Public voidTest () {Observer OBS1=NewObserverimpl (); Observer OBS2=NewObserverimpl (); Observ
complex, there are many properties, and these properties are references to other objects, which may include a lot of object references. By encapsulating these complexities, you can use the build mode.Recommended Link: http://www.cnblogs.com/cbf4life/archive/2010/01/14/1647710.html4. Façade modeThis pattern feels like a replica of the service layer. For example DAO we define a lot of persistence methods, we use the service layer to make the DAO atomic method into business logic, and then provide
corresponding get function. When we call them, we only need to call the Public matching code of the above parent class to obtain the correct data. The following is the code snippet after the facade mode is introduced to the above table structure: ......String name, ID, age;Studentselect student = New Studentselect ();Student.exe cute ();Name = Student. getname ();ID = Student. GETID ();Age = Student. getage ()...... The above mechanism can also be used for SQL statements updated in the
=
new Subscriptionsubject ();
//create user Weixinuser user1=new Weixinuser ("Yolanda Maple"); Weixinuser user2=New Weixinuser ("The Brow of the Moon"); Weixinuser user3=New Weixinuser ("Purple Xuan"); //Subscribe to public number Msubscriptionsubject.attach (user1), Msubscriptionsubject.attach (User2); Msubscriptionsubject.attach ( USER3); //Public number update sends a message to subscribers of the subscription msubscriptionsubject.notify ("Liu Wangshu's Colum
layer. Object-oriented methods can be used to store, update, delete records of a database, and query database information. Persistent layer can also choose Ibatis, etc.
Data layer: Used to store business data for the upper call, mainly refers to some database systems.
The 3 framework features of the classic SSH (Struts, Spring, Hibernate) are different, and depending on the functional hierarchy of the Web application, these 3 frameworks can be applied to various levels, ultimately enabling the
implementation. Because the structure inside the parameters are eat interface, as long as the implementation of the Eat class can be 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 decorat
voidNotifyobserver () {//TODO auto-generated Method Stub for(Observer o:list) {o.update ("Hello, world!."); } }}The following code is specific to the observer implementation: Package Obeserver; Public class Implements Observer { @Override publicvoid update (String str) { // TODO auto-generated method stub System.out.println (str);} }Write a test code below to see: PackageObeserver; Public classObservertest { Public Static voidMain (string[] args) {Subject S1
Design Patterns ----------- Singleton design patterns, Singleton Design PatternsHungry Chinese Style: class Single {// do it in advance! Private static final Single s = new Single (); // private constructor cannot use new to create an object! Private Single () {}// public st
Newwidgets (); - } - } - } - Public classFactoryconstraint { in Public Static voidMain (string[] args) { - NewFoo2Newintegerfactory ()); to NewFoo2Newwidget.factory ()); + } -}View CodeClassTemplate method:Another way is to use the template method design pattern, by implementing abstract class (template) abstract method to create objects, also can get compile time type check, the following is the code:1 PackageC
single-state design patterns in Java2011-09-23 16:38:46|Category: Java |Tags:technology! | Report | Font size Subscription This blog post is a blog post from a blogger called "Junny's Blog". Feel this blog is really good, so take it out here to share! (I just made a few changes to the code.)A concept:Design pattern: The
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
whole pattern of using lambda expressions is greatly simplified because we can completely abolish these classes. Let's see if the macro Class (client) uses a lambda expression instead of the command class's effect.Filereceiver.closefile (), new--- Macro.run (); If you can realize that each lambda expression is executing a separate method call, you can further refine it. Therefore, you can use the method reference directly.New Macro (); Macro.record (filereceiver::openfile); Macro.record (filere
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.