java--Object-oriented design mode--Factory mode

Source: Internet
Author: User

I've been known for the first time today to touch the Java Factory design model.

See below:

Java code
  1. Interface Fruit
  2. {
  3. public void Eat ();
  4. }
  5. Class Apple implements Fruit
  6. {
  7. public void Eat ()
  8. {
  9. System.out.println ("eat apples");
  10. }
  11. }
  12. Class Orange implements Fruit
  13. {
  14. public void Eat ()
  15. {
  16. System.out.println ("eat oranges");
  17. }
  18. }
  19. Class Factory
  20. {
  21. public static Fruit getins (String classname) {
  22. Fruit F;
  23. f = null;
  24. if ("Apple". Equals (classname))
  25. {
  26. f = new Apple ();
  27. }
  28. if ("Orange". Equals (classname))
  29. {
  30. f = new Orange ();
  31. }
  32. return F;
  33. }
  34. }
  35. Public class Demo6
  36. {
  37. public static void Main (string[] args)
  38. {
  39. Fruit F;
  40. if (args[0]!=null) {
  41. F=factory.getins (args[0]);
  42. F.eat ();
  43. }
  44. else {
  45. System.out.println ("Please input parameters");
  46. }
  47. }
  48. }



The program joins a transition segment between the subclass and the interface, which is called the factory by the transition segment to get an instance of the interface.

So is it possible to understand that the factory is processing the things provided by the main method into standards-compliant products (instanced objects) for subsequent programs to process?

java--Object-oriented design mode--Factory mode

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.