I am an architect-design model-factory model-simple factory Model

Source: Internet
Author: User

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

 
 
  1. public interface Fruit{  
  2.  
  3. void grow();  
  4.  
  5. void harvest();  
  6.  
  7. void plant();  
  8.  
  9. }  
  10.  

Apple implementation class

 
 
    /*** Apple
  1. */
  2. Public class Apple implements Fruit {
  3. Private int treeAge;
  4. Public void grow (){
  5. Log ("apple grow ");
  6. }
  7. Public void harvest (){
  8. Log ("apple harvest ");
  9. }
  10. Public void plant (){
  11. Log ("apple plant ");
  12. }
  13. Public void log (String msg ){
  14. System. out. println (msg );
  15. }
  16.  
  17. }

Then there are grapes and strawberries.

Below we write the factory class

 
 
  1. Pulibc class FruitFactory {
  2.  
  3. Public static Fruit factory (String which ){
  4. If (which. equalsIgnoreCase ("apple ")){
  5. Return new Apple ();
  6. } Else if (which. equalsIgnoreCase ("strawberry ")){
  7. Return new Strawberry ()
  8. } Else if (which. equalsIgnoreCase ("grape ")){
  9. Return new Grape ();
  10. }
  11. Else {
  12. Throw new BadFrutException ("Bad fruit request! ")
  13. // If you write your own components, try to define your own exception base class
  14. }
  15. }
  16. }
  17.  

Check what the client needs to do:

 
 
  1. FruitFactory.factory("apple"); 

If you do not want to do this, the client needs:

 
 
  1. new Apple() 

Then try again:

 
 
  1. 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

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.