Discover best design patterns book, include the articles, news, trends, analysis and practical advice about best design patterns book on alibabacloud.com
, filterinputstream,filteroutputstream
Concretedecorate Specific Decorative roles: responsible for adding new responsibilities to component objects, Bufferedinputstream,bufferedoutputstream, etc.
Advantages
Extended object functionality is more flexible than inheritance and does not result in a dramatic increase in the number of classes.
You can decorate an object multiple times, creating a combination of different behaviors and getting more powerful objects.
The concre
observer;/** * Specific observer classes, such as LV Zeng in the example, Zhang Wei is an example of the Observer * @author WL * */public class Concretewatcher implements watcher { String Watcheredname; String Name;public concretewatcher (String name,string watcheredname) {this.name=name;this.watcheredname= Watcheredname;} @Overridepublic void Updata (String str) {System.out.println (name+ "Heart secretly happy" Hey!!! The opportunity came, my "+watcheredname+" said she ' "+str+" ' "");}Package
);/** * update Price */ Food.setprice (1.5f);}}third, the application scenarioGUI frame, meteorological observation, etc.Iv. SummaryBe sure to call the Setchanged () method before notifying all observers to set the state of the observer to be changed so that notifyobservers () will not be updated with the Observer Update method. If there are different opinions or omissions in the above content, please give us some valuable suggestions or comments.Finally, you can try to use the following UML mod
();
}
}
}
public class Mysubject extends Abstractsubject {
@Override
public void operation () {
System.out.println ("Update self!");
Notifyobservers ();
}
}
Test class:
public class Observertest {
public static void Main (string[] args) {
Subject sub = new Mysubject ();
Sub.add (New Observer1 ());
Sub.add (New Observer2 ());
Sub.operation ();
}
}
Output:Update self!Observer1 has rece
There are many similarities between structural patterns because there are several relationships in the object model structure: inheritance and implementation between classes, plus object combinations, which are the main purposes of the PHP design pattern.
The greatest advantage of the combination of objects is that you can build a relationship based on configuration at run time without modifying the actual
implementation is just a common Java class, as long as the basic needs of the singleton, you can do in the arbitrary implementation of some other functions, but static class does not. From the above summary, the basic can see the difference between the two, but, on the other hand, we finally realized that the singleton pattern, the interior is a static class to achieve, so, the two have a great correlation, but we consider the level of the problem is different. The combination of two ideas to c
A learning journey based on design patterns
-----Appearance Mode
1,
Initial appearance Mode
Stock:EachInvestors directly purchase various types of stocks (common)
Fund: Investors deal with experts to allow expertsAgree to selectStock to be purchased (appearance)
2,
What is appearance mode?
Provides a consistent interface for a group of interfaces in the subsystem,F A C A D EMode defines a hi
A learning journey based on design patterns
-----
Observer
Mode
1,
Initial observer Mode
In a time when the network was not so developed, many people wanted to know that the weather forecast for the next day had to watch live TV at the specified time, but sometimes they wanted to watch the weather and found that live TV had passed. This is really an unpleasant thing.
Based on this situation, M
object simplifies the design of the aggregation object. The client interfaces to interface programming, which conforms to the "open and close principle".
Disadvantages:
The implementation process of the iterator pattern requires a lot of interface,class support. Adding or deleting an aggregation type requires a new set of iterator implementations. The implementation process may be more complex. Complete iterative function is more complex, in the
object is often subject to drastic changes, however, the algorithms that combine sub-objects in each part are relatively stable. To put it simply, sub-objects change frequently and are relatively stable for the combined sub-object algorithm.This is a solution to the creation of a complex object. The changed part and the relatively stable part have been clarified. What we need to do is to isolate the change and how to isolate the sub-objects from the algorithm is a problem to be solved.In
Bridge)The bridge mode is used to solve the problem of multi-dimensional changes of the system. It separates the abstract part from the implementation part so that they can all change independently. (See the understanding of the Bridge Mode)Structure:Examples in life:
Composite)
Intent:Combine objects into a tree structure to represent the "part-whole" hierarchy. The composite mode ensures consistency between the use of a single object and a composite object. [Gof
Looking at "C + + Design new thinking", found in the beginning of a large length of the introduction of the Strategy Model (policy), the strategy model is not a classic design pattern, but in fact, in our day-to-day development is essential. Policy, strategy, policy, the meaning here is that the complex function of the class as far as possible to split into a single function of a simple class combination, s
):def __init__(self): Self.name='Processserver'self.state=state.newdefboot (self):Print('booting the {}'. Format ( self) self.state=state.runningdefKill (Self, restart=True):Print('killing {}'. Format ( self) self.state= Stae.restartifRestartElseState.zobmiedefcreate_process (self, user, name):Print("trying to create process ' {} ' for user ' {} '". Format (name, user)classWindowsServer:PassclassNetworkserver:PassclassOperatingSystem:def __init__(self): Self.fs=fileserver () self.ps=Processserve
(String Person: This. Getfriends ()) {List.add (person); } //frend.setfriends (list); returnfrend; } Catch(clonenotsupportedexception e) {e.printstacktrace (); return NULL; } }} PackageChenyang;Importjava.util.ArrayList;Importjava.util.List;/*** Text of the test class * *@author65245 **/ Public classText { Public Static voidMain (string[] args) {//1. Create a direct assignment to the collectionPerson person =NewPerson (); ListNewArraylist(); List.add ("Hello"); List.add
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 Smssender. Finally, the Builder class
"Design Mode" (GOF): Not recommended for beginners to see. These four are PhD levels, they are the first to propose design patterns, high theoretical level, although a thin one, but the text is relatively obscure, but also lack of easy to understand examples. When you get to a certain level, if you are interested, you can look at it."Head First
program does not need to know (or care) that it obtains the specific types of objects from these internal factory methods because the client program uses only the generic interfaces of those objects. Abstract Factory mode separates the implementation details of a set of objects from their general usage.
A "factory" is where a product (object) is created to separate the creation of the product from the use of the product. The purpose of abstract Factory mode is to separate the interface of seve
Introduction to Design Patterns
Each pattern describes a recurring problem around us, as well as the core of the solution to the problem.--Christopher Alexander
The design pattern describes a general solution to a common problem in the software design process. Object-oriented des
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.