Façade mode-facade pattern (Java implementation)

Source: Internet
Author: User

Façade mode-facade pattern

Provides a simple interface for external access to a complex module or subsystem

Examples of the code in this article are as follows: There are many miners in a mine, and the duties of miners are different. But what is the same? The same is true for every miner who repeats the same thing every day .... Getting up, going to work, working, off duty, sleeping ...

It's not easy to manage so many of these miners (good trouble ...). It is also possible for developers to forget someone, or forget a method, when they call each other.

Another chestnut, such as the operation of the database is the same, to load the driver, establish a connection, operate the database, close the connection. So many steps to forget one would be a problem.

Mineworker Abstract class

This is the uniform and abstract definition of the workers in the mine.

Public abstract class Mineworker {protected void Gotosleep () {System.out.printf ("%s sleeps. \ n", name ());    } protected void WakeUp () {System.out.printf ("%s woke up in the morning. \ n", name ());    } protected void GoHome () {System.out.printf ("%s home. \ n", name ());    } protected void Gotomine () {System.out.printf ("%s to mine. \ n", name ());    } protected abstract void work ();    Protected abstract String name (); public static enum Action {go_to_sleep, wake_up, Go_home, Go_to_mine, work} public void action (action ... ac                    tions) {for (action action:actions) {switch (action) {case GO_TO_SLEEP:                    Gotosleep ();                Break                    Case Wake_up:wakeup ();                Break                    Case Go_home:gohome ();                Break                    Case Go_to_mine:gotomine ();               Break Case Work:work ();                Break                    Default:System.out.println ("Undefined action");            Break }        }    }}
Cartoperator class

Trolley workers.

public class Cartoperator extends Mineworker {    @Override public    void work () {        System.out.printf ("%s shipping ore \ n") , name ());    }    @Override public    String name () {        return "stroller worker";    }}
Golddigger class

Gold Digger workers

public class Golddigger extends Mineworker {    @Override the public    Void work () {        System.out.printf ("%s gold mining. \ n"), Name ());    }    @Override public    String name () {        return "mining gold worker";    }}
Tunneldigger class

Dig the tunnel workers.

public class Tunneldigger extends Mineworker {    the @Override public    void work () {        System.out.printf ("%s" tunneling. \ n ", name ());    }    @Override public    String name () {        return "tunnel-digging worker";    }}
Minefacade class

The façade of this system is used to provide a simple interface to the outside.

import java.util.arraylist;import java.util.collection;import java.util.List    ;p Ublic class Minefacade {private final list<mineworker> mineworkers;        Public Minefacade () {mineworkers = new arraylist<> ();        Mineworkers.add (New Golddigger ());        Mineworkers.add (New Cartoperator ());    Mineworkers.add (New Tunneldigger ());     } public void Startnewday () {makeactions (mineworkers, MineWorker.Action.WAKE_UP, MineWorker.Action.GO_TO_MINE);    } public void Digoutgold () {makeactions (mineworkers, MineWorker.Action.WORK);    } public void Endday () {makeactions (mineworkers, MineWorker.Action.GO_HOME, MineWorker.Action.GO_TO_SLEEP); } private static void Makeactions (collection<mineworker> mineworkers, Minewor Ker.        Action ... actions) {for (Mineworker mineworker:mineworkers) {mineworker.action (actions); }    }}
Main

Run in this

public class Main {public    static void Main (string[] args) {        //instantiate a façade, which is a simple interface provided by the mine        Minefacade Minefaca de = new Minefacade ();        Start a New Day        minefacade.startnewday ();        Everybody get up and work        minefacade.digoutgold ();        The end of the day        Minefacade.endday ();    }}

The results of the operation are as follows:

Façade mode-facade pattern (Java implementation)

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.