Common patterns in Java learning---

Source: Internet
Author: User

common patterns for Java

Adapter mode

1  PackageCom.huawei;2 3 ImportJava.io.BufferedReader;4 Importjava.io.IOException;5 ImportJava.io.InputStreamReader;6 7 Importjava.util.Observable;8 ImportJava.util.Observer;9 Ten InterfaceWindow One { A      Public  voidopen (); -      Public  voidclose (); -      Public  voidactive (); the } -  - Abstract classWindowadapterImplementsWindow - { +      Public  voidopen () {} -      Public  voidclose () {} +      Public  voidactive () {} A } at  - classWindowimplextendsWindowadapter - { -      Public  voidOpen () -     { -System.out.println ("Open ..."); in     } -      Public  voidClose () to     { +System.out.println ("Close ..."); -     } the      Public  voidActive () *     { $System.out.println ("Active ...");Panax Notoginseng     } - } the  +  A  Public classFornumber the { +      Public Static voidMain (String args[]) -     { $Window win =NewWindowimpl (); $ Win.open (); - win.close (); -     } the}
View Code

Factory mode

1  PackageCom.huawei;2 3 ImportJava.io.BufferedReader;4 Importjava.io.IOException;5 ImportJava.io.InputStreamReader;6 7 Importjava.util.Observable;8 ImportJava.util.Observer;9 Ten InterfaceFruit One { A      Public voideat (); - } -  the classAppleImplementsFruit - { -  -      Public voidEat () +     { -System.out.println ("Eat Apple"); +     } A      at } -  -  - classOrangeImplementsFruit - { -      Public voidEat () in     { -System.out.println ("Eat Orange"); to     } + } - //Define Factory the classFactory * { $      Public StaticFruit getinstance (String className)Panax Notoginseng     {    -Fruit f =NULL; the         if("Apple". Equals (className)) +         { Af =NewApple ();  the         } +          -         if("Orange". Equals (className)) $         { $f =NewOrange ();  -         } -          the         returnF; -     }Wuyi } the  -  Public classFornumber Wu { -      Public Static voidMain (String args[]) About     { $Fruit f =NULL;//Defining Interface Objects -f =NewFactory (). getinstance ("Apple")); - f.eat (); -     } A}
View Code

Common patterns in Java learning---

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.