Java object-oriented interface application--strategy mode

Source: Internet
Author: User

Concept: defines a series of algorithms that encapsulate each algorithm and can be used to replace each other, and the policy pattern makes the algorithm independent of the client application that uses it.

The interface abstraction is a method.

oo Design principles : 1. Interface-oriented Programming 2. Package changes 3. Multi-use combination, less inheritance

1  Public classPractice14 {2     3      Public Static voidMain (string[] args) {4         //TODO auto-generated Method Stub5         //to point to a subclass object with a reference to the parent class6Bird d=NewBigbird ("Little white");7D.SETF (NewFly ());8 d.fly ();9Bird d1=NewBigbird ("Little Black");TenD1.SETF (Newnofly ()); One d1.fly (); A     } - } - //Interface the Interfaceflytype{ -     voidfly (); - } -  + classFlyImplementsflytype{ -      Public voidFly () { +System.out.println ("I Can Fly"); A     } at } - classNoflyImplementsflytype{ -      Public voidFly () { -System.out.println ("I Can't Fly"); -     } - } in //Abstract class - Abstract classbird{ to     PrivateString name; +     PrivateFlytype F;//Combination -      PublicBird (String name) { the          This. name=name; *     } $      PublicFlytype getf () {Panax Notoginseng         returnF; -     } the      Public voidsetf (Flytype f) { +          This. F =F; A     } the      Public voidFly () { +System.out.println ("My name is called" +name); - f.fly (); $     } $      - } -  the classBigbirdextendsbird{ -      PublicBigbird (String name) {Wuyi         Super(name); the     } - } Wu classModelbirdextendsbird{ -      PublicModelbird (String name) { About         Super(name); $     } -}

Java object-oriented interface application--strategy 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.