Design Model (1)-factory Model

Source: Internet
Author: User

Simple factory Mode

1. Purpose
The factory mode is specifically responsible for instantiating a large number of classes with common interfaces, and you do not have to know which class to instantiate each time. It defines an interface used to create objects, and the subclass determines which class to instantiate.
2. Structure of the simple factory Model

3. A simple example of Java code
  1. // Product Interface
  2. Public interface product {
  3. Public void getname ();
  4. }
  5. // Product
  6. Public class producta implements product {
  7. Public void getname (){
  8. System. Out. println ("I am producta ");
  9. }
  10. }
  11. // Product B
  12. Public class productb implements product {
  13. Public void getname (){
  14. System. Out. println ("I am productb ");
  15. }
  16. }
  17. // Factory type
  18. Public class productcreator {
  19. Public Product createproduct (string type ){
  20. If ("A". Equals (type )){
  21. Return new producta ();
  22. }
  23. If ("B". Equals (type )){
  24. Return new productb ();
  25. } Else
  26. Return NULL;
  27. }
  28. Public static void main (string [] ARGs ){
  29. Productcreator creator = new productcreator ();
  30. Creator. createproduct ("A"). getname ();
  31. Creator. createproduct ("B"). getname ();
  32. }
  33. }
4. Summary applicability of the factory Model
? You cannot predict the type of instance to be created during encoding.
? A class uses its subclass to create objects.
? Developers do not want to create an instance of any class or expose the information about how to create an instance to external programs.

 

Abstract Factory Model

1. abstract factory models can be said to be extensions of simple factory models. The main difference between them is the complexity of object creation.
In the abstract factory model, abstract products may be one or more to form one or more product families. In the case of only one product family, the abstract factory mode actually degrades to the factory method mode.
2. Abstract The structure of the factory Model

 

3. A simple example

Java code
  1. // Product plant interface
  2. Public interface plant {
  3. }
  4. // Specific products: planta and plantb
  5. Public class planta implements plant {
  6. Public planta (){
  7. System. Out. println ("create planta! ");
  8. }
  9. Public void dosomething (){
  10. System. Out. println ("planta do something ");
  11. }
  12. }
  13. Public class plantb implements plant {
  14. Public plantb (){
  15. System. Out. println ("create plantb! ");
  16. }
  17. Public void dosomething (){
  18. System. Out. println ("plantb do something ");
  19. }
  20. }
  21. // Product fruit Interface
  22. Public interface fruit {
  23. }
  24. // Specific products: Fruita and fruitb
  25. Public class Fruita implements fruit {
  26. Public Fruita (){
  27. System. Out. println ("create Fruita! ");
  28. }
  29. Public void dosomething (){
  30. System. Out. println ("Fruita do something ");
  31. }
  32. }
  33. Public class fruitb implements fruit {
  34. Public fruitb (){
  35. System. Out. println ("create fruitb! ");
  36. }
  37. Public void dosomething (){
  38. System. Out. println ("fruitb do something ");
  39. }
  40. }
  41. // Abstract Factory method
  42. Public interface abstractfactory {
  43. Public plant createplant ();
  44. Public fruit createfruit ();
  45. }
  46. // Specific factory Method
  47. Public class factorya implements actfactory {
  48. Public plant createplant (){
  49. Return new planta ();
  50. }
  51. Public fruit createfruit (){
  52. Return new Fruita ();
  53. }
  54. }
  55. Public class factoryb implements actfactory {
  56. Public plant createplant (){
  57. Return new plantb ();
  58. }
  59. Public fruit createfruit (){
  60. Return new fruitb ();
  61. }
  62. }
4. Summary
The abstract factory mode should be considered in the following cases.
First, a system should not depend on the details of product instances being created, formed, and expressed. This is important for all forms of factory models.
Second, there are more than one product family in this system.
Third, products belonging to the same product family are designed to be used together. This constraint must be reflected in the system design.
Finally, different products appear with a series of interfaces, so that the system does not depend on the Interface implementation details.
Among them, the second and third conditions are the key conditions for choosing the abstract factory model rather than other factory models.

Design Model (1)-factory Model

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.