Simple factory Model--a simple factory model based on a case study

Source: Internet
Author: User

  before explaining the simple factory pattern, it is necessary to understand some of the principles of OO
 1.OCP (opening and closing principles, open-closed principle): A software entity should be open to extension and closed to modifications. That is, for an existing software, if it needs to be extended, it should be done on the basis that there is no need to modify the
     existing code.
  2.DIP (relying on the reverse principle, dependence inversion principle): To programming against the interface, do not program for implementation. To put it simply, for different levels of programming, high-level exposure to low-level
      should be just an interface, not its specific class.
  3.LoD (Dimitri Law of Demeter): only communicate with your direct friends, and avoid communication with strangers.
 &NBSP
  Take a simple example to learn about simple factory classes:
   There's an orchard with apples and bananas planted in it, a store that buys some apple cores and bananas from the orchard. Now, the store is the client, Apple and banana is the product, now the store sells fruit, do not use the simple factory model to achieve the following example:  
              

Client classes public class
Store {public
static void Main (string[] args) {
Apple apple=new apple ();
System.out.print (Apple.tell ());
Banana banana=new Banana ();
System.out.print (Banana.tell ()); 
}
}
Apple class
apple{public
String tell () {
"I am Apple";
}
} Banana Class class
banana{public
String tell () {return
"I am Banana";
}
}


  From this example, it is easy to see that the store class relies on the apple and banana classes, which means that the client relies on the product,
  If the orchard does not have an apple, the store cannot buy it, let alone sell it, and the store will also remove the instantiation of the class.
   in short, is to violate the OCP principle, the client directly depends on the product
   each time need to go to new, completely expose the product class to the client
   
&NBSP
If you use simple Factory mode

  because the Apple class and the banana class all have the same tell () method, you can define an interface, declare the method;
  Then let the client depend on the factory by defining the factory class , rather than relying directly on the product
 

public class Store {public
	static void Main (string[] args) {
        frute a=factory.getfrute ("Apple");   The Product Object    
        if (a!=null)
           System.out.println (A.tell ()) is obtained;
        Frute B=factory.getfrute ("banana");   Get Product Object    
        if (a!=null)
           System.out.println (A.tell ());
	}
Factory class
factory{
	//is responsible only for generating objects public
	static Frute Getfrute (String name) {
		if name.equals ("Apple ) {return
			new Apple ();
		} else if (name.equals ("banana")) {return
			new banana ();
		}
		return null;
	}
}
Interface frute{public
	String tell ();
}
Apple class class Apple
implements frute{public
	String tell () {
		"I am Apple";
	}
} Banana class
Banana implements frute{public
	String tell () {return
		"I am Banana";
	}


What are the advantages of a simple factory model? As the above example, the client has no direct connection to the product, that is, the caller does not depend directly on the call, and when the Apple class banana class is modified, it does not need to be modified
And the use of Frute interface embodies the dip principle.

Disadvantage: When the system is extended, such as adding a grape class, there is no need to modify other product classes, such as Apple and banana, seemingly conform to the OCP principle, but in fact still need to modify the factory class, so
    The simple factory model does not conform to the OCP principle.                                                                

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.