The Singleton mode is too hasty. Today, the factory mode is written. By the way, UML class diagrams have four relationships: dependency, association, generalization (inheritance) and implementation.
Differences between dependency and association
Dependency:
650) this. width = 650; "style =" border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px "title =" 4 "border =" 0 "alt =" 4 "height =" 118 "src =" http://www.bkjia.com/uploads/allimg/131228/12224V247-0.jpg "/>
Association:
650) this. width = 650; "style =" border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px "title =" 1 "border =" 0 "alt =" 1 "height =" 129 "src =" http://www.bkjia.com/uploads/allimg/131228/12224WB8-1.jpg "/>
Note the arrows. Association is more like an extension of dependency.
Factory model:
The factory model usually refers to three models:Simple Factory,Abstract Factory,Factory methodIn fact, I don't really like the names of the three models. At the beginning, I didn't think of them as model names, especially factory methods ).Simple FactoryMode is sometimes not called a design mode, you only need to know, this is not important), we follow the mainstream principles, the simple factory as a design mode, and first introduce.
Look at the UML diagram:
650) this. width = 650; "style =" border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px "title =" simple factory "border =" 0 "alt =" simple factory "height =" 158 "src =" http://www.bkjia.com/uploads/allimg/131228/12224W911-2.jpg "/>
In this figure, the Factory class is used as a Factory to produce all classes that implement the Per interface. The timer acts as the actual implementer. The following is an example of Java and pattern.Simple factory design mode.
650) this. width = 650; "style =" border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px "title =" simple factory instance "border =" 0 "alt =" simple factory instance "height =" 170 "src =" http://www.bkjia.com/uploads/allimg/131228/12224SG5-3.jpg "/>
If a farm is responsible for selling various fruits, as a customer, you only need to say to the farm sales staff: If I want an apple, the farm will give you an apple.
Convert to software language: the farm is a server, and its external interface is the sales staff, and the customer is the client. The client only needs to know the Fruit and factory classes to get the expected Fruit, such as Apple, without the need to know other Apple or grape implementation classes.
Fruit Interface
- public interface Fruit{
-
- void grow();
-
- void harvest();
-
- void plant();
-
- }
-
Apple implementation class
/*** Apple
- */
- Public class Apple implements Fruit {
- Private int treeAge;
- Public void grow (){
- Log ("apple grow ");
- }
- Public void harvest (){
- Log ("apple harvest ");
- }
- Public void plant (){
- Log ("apple plant ");
- }
- Public void log (String msg ){
- System. out. println (msg );
- }
-
- }
Then there are grapes and strawberries.
Below we write the factory class
- Pulibc class FruitFactory {
-
- Public static Fruit factory (String which ){
- If (which. equalsIgnoreCase ("apple ")){
- Return new Apple ();
- } Else if (which. equalsIgnoreCase ("strawberry ")){
- Return new Strawberry ()
- } Else if (which. equalsIgnoreCase ("grape ")){
- Return new Grape ();
- }
- Else {
- Throw new BadFrutException ("Bad fruit request! ")
- // If you write your own components, try to define your own exception base class
- }
- }
- }
-
Check what the client needs to do:
- FruitFactory.factory("apple");
If you do not want to do this, the client needs:
- new Apple()
Then try again:
- new Strawberry)
Okay, I'm a server. I suddenly wanted to upgrade it. I think the name of Apple should be changed to AppleRed. so I added a new class and was compatible with the original version. I didn't have a good design on the server, so I had to keep the Apple class.
Client: I want to change the original code because of your upgrade. This is terrible.
Let's look at the benefits of using the factory model on the server.
Server: Smooth upgrade. You do not need to publish new APIs to inform the customer of what I have done.
Client: No need to do anything.
When to use the simple factory model:
The preceding example shows how to use a simple factory method in the form of a client or server, or when writing a plug-in.
Practical application:
1. Used in springSimple factory Mode
2.Abstract Factory ModelFrequently UsedSimple factory Mode
It's a bit late. Abstract Factory and factory methods are relatively complicated and easy to mix up. I will introduce them in the next article.
This article is from the "scholar" blog, please be sure to keep this source http://yjplxq.blog.51cto.com/4081353/947267