"Head First design mode" C # implementation (iii)--appearance mode

Source: Internet
Author: User

Appearance mode-provides a unified interface for accessing a group of interfaces in a subsystem. The appearance defines a high-level interface to make the subsystem easier to use.

  

A look contains a lot of objects and operations on them, making it easier to repeat these operations.

If there is a light in the living room (Lights)

  

class Lights    {        publicvoid on ()        {            Console.WriteLine ("Lights on " );        }          Public void off ()        {            Console.WriteLine ("Lights off");        }    }

Television (TV)

  

class TV    {        publicvoid on ()        {            Console.WriteLine ("TV -On " );        }          Public void off ()        {            Console.WriteLine ("TV off");        }    }

and CD player (Dvdplaer)

  

classDvdplayer { Public voidOn () {Console.WriteLine ("DVD player on."); }         Public voidoff () {Console.WriteLine ("DVD player off"); }         Public voidPlay () {Console.WriteLine ("Start playing"); }         Public voidEnd () {Console.WriteLine ("End End playing"); }    }

You can define an appearance-cinema mode, which provides two ways to start watching a movie and ending a movie, respectively, for a series of actions.

 classfacade {TV TV;        Dvdplayer DVD;        Lights Lights;  Publicfacade (TV TV, Dvdplayer DVD, Lights Lights) { This. TV =TV;  This. DVD =DVD;  This. Lights =lights; }         Public voidWatchmovie () {Console.WriteLine ("Get ready to watch a movie");            Lights.off ();            Tv.on ();            Dvd.on ();        Dvd.play (); }         Public voidEndmovie () {Console.WriteLine ("\nshutting movie theater down.");            Lights.on ();            Dvd.end ();            Dvd.off ();        Tv.off (); }    }

This allows for a convenient one-click Cinema mode without having to ' personally ' operate a variety of devices each time.

class program    {        staticvoid Main (string[] args)        {            new  TV ();             New  dvdplayer ();             New Lights ();             New facade (tv,dvd,lights);            Facade.watchmovie ();            Facade.endmovie ();            Console.readkey ();        }    }

Output Result:

"Head First design mode" C # implementation (iii)--appearance 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.