Abstract Factory Mode (example)

Source: Internet
Author: User

According to the above requirements to use the Abstract factory model design program. I put the program on the Https://github.com/RoyalBob/Abstract-Factory-Mode-Practice.git welcome visit.

Here is the UML diagram and code for this program:

1. Create a large food factory interface Ifoodfactory

1  Public Interface ifoodfactory {2      Public Burger Makeburger (); 3      Public Chicken Makechicken (); 4      Public Drink Makedrink (); 5 }

2, the two shops have their own burgers, fried chicken, drink, so create a large food interface Ifood, there is a way to do food

1  Public Interface Ifood {2      Public Abstract void Make (); 3 }

3, Hamburg burger realization Ifood Food interface

1 Abstract class Implements ifood{23      Public void Make () {4         5     }6 }

4, fried chicken chicken realize ifood Food interface

1 Abstract class Implements ifood{23      Public void Make () {4         5     }6 }

5, beverage drink Realization Ifood Food interface

1 Abstract class Implements ifood{23      Public void Make () {4         5     }6 }

6. KFC's burger and McDonald's burger all inherit burger to produce their own burgers.

KFC's Burger:

1  Public class extends burger{2      Public void Make () {3         System.out.println ("This is KFC ' s burger!" ); 4     }5 }

McDonald ' s Burger:

1  Public class extends burger{2      Public void Make () {3         System.out.println ("This is mcdonal ' s burger!" ); 4     }5 }

7. KFC's fried chicken and McDonald's fried chicken all inherit chicken to produce their own fried chicken.

KFC's Fried Chicken:

1  Public class extends chicken{2      Public void Make () {3         System.out.println ("This is KFC ' s chicken!" ); 4     }5 }

Fried chicken with McDonald ' s:

1  Public class extends chicken{2      Public void Make () {3         System.out.println ("This is McDonald ' s chicken!" ); 4     }5 }

8. KFC drinks and McDonald's drinks are inherited drink to produce their own drinks.

KFC's Drinks:

 Public class extends drink{    publicvoid make () {        System.out.println ("This is KFC ' s drink!" );    }}

McDonald ' s drinks:

1  Public class extends drink{2      Public void Make () {3         System.out.println ("This is McDonald ' s drink!" ); 4     }5 }

9, KFC and McDonald ' s factory respectively to achieve the interface of this food factory

KFC Factory:

1  Public classKfcfactoryImplementsifoodfactory{2     3          PublicBurger Makeburger () {4         return NewKfcburger ();5     }6 7      PublicChicken Makechicken () {8         return NewKfcchicken ();9     }Ten  One      PublicDrink Makedrink () { A         return NewKfcdrink (); -     } -}

McDonald ' s factory:

1  Public classMcdonaldfactoryImplementsifoodfactory{2 3      PublicMcdonaldburger Makeburger () {4         return NewMcdonaldburger ();5     }6 7      PublicMcdonaldchicken Makechicken () {8         return NewMcdonaldchicken ();9     }Ten  One      PublicMcdonalddrink Makedrink () { A         return NewMcdonalddrink (); -     } -  the}

10. Write the main function main to create the KFC and McDonald ' s instances, respectively, to make their own burgers, fried chicken and drinks. Output results to see if they meet the requirements:

Abstract Factory Mode (example)

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.