Will point mode, to have bigger picture ~ ~ ~

Source: Internet
Author: User

The general principle is that, like transformers, each component is split into exactly the right business.

It's too thin to be lost.

The coupling is too strong, will not adapt to change ~ ~

Miniducksimulator.java

Abstract classDuck {Flybehavior flybehavior;    Quackbehavior Quackbehavior;  Public Abstract voiddisplay ();  Public voidPerformfly () {flybehavior.fly (); }         Public voidPerformquack () {quackbehavior.quack (); }     Public voidswim () {System.out.println ("All duck float, even decoys!"); }         Public voidSetflybehavior (Flybehavior fb) {Flybehavior=FB; }     Public voidSetquackbehavior (Quackbehavior QB) {Quackbehavior=QB; }}Interfaceflybehavior{ Public voidfly ();}classFlywithwingsImplementsFlybehavior { Public voidFly () {System.out.println ("I ' m flying!"); }}classFlynowayImplementsFlybehavior { Public voidFly () {System.out.println ("I can ' t fly!"); }}classFlyrocketpoweredImplementsFlybehavior { Public voidFly () {System.out.println ("I ' m flying with a rocket!"); }}InterfaceQuackbehavior { Public voidquack ();}classQuackImplementsQuackbehavior { Public voidQuack () {System.out.println ("Quack"); }}classMutequackImplementsQuackbehavior { Public voidQuack () {System.out.println ("<< Silence >>"); }}classSqueakImplementsQuackbehavior { Public voidQuack () {System.out.println ("Squeak"); }}classMallardduckextendsDuck { PublicMallardduck () {Quackbehavior=NewQuack (); Flybehavior=Newflywithwings (); }     Public voiddisplay () {System.out.println ("I ' m a real mallard duck"); }}classModelduckextendsDuck { PublicModelduck () {Flybehavior=NewFlynoway (); Quackbehavior=NewQuack (); }     Public voiddisplay () {System.out.println ("I ' m a model duck!"); }} Public classMiniducksimulator { Public Static voidMain (string[] args) {//TODO auto-generated Method StubDuck Mallard =NewMallardduck ();        Mallard.performquack ();        Mallard.performfly ();                Mallard.swim (); Duck Model=NewModelduck ();        Model.performfly (); Model.setflybehavior (Newflyrocketpowered ());    Model.performfly (); }}

Quack
I ' m flying!
All duck float, even decoys!
I can ' t fly!
I ' m flying with a rocket!

Will point mode, to have bigger picture ~ ~ ~

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.