Template approach Design pattern extension (Hook method) _ Design pattern

Source: Internet
Author: User
Package Template_method_pattern;

Import Java.util.Scanner;  /** * Template Method Mode Extension * Hook method * @author DD * */public class Demo2 {public static void main (string[] args)
        {System.out.println ("--------------BMW 3 Series---------------"); System.out.println ("BMW 3 Series) need not whistle."
        0: no need; 1: Need ");
        Scanner s = new Scanner (system.in);
        String str = s.nextline ();
        Bmwx3model B3 = new Bmwx3model ();
        if (Str.equals ("0")) {B3.setalarm (false);
        } b3.run ();
        System.out.println ("--------------BMW 5 Series-------------------");
        Bmwx5model B5 = new Bmwx5model ();
    B5.run ();

    The abstract class bmwmodel{protected abstract void start ();

    protected abstract void Stop ();

    protected abstract void alarm ();

    protected abstract void engineboom ();
        Final public void Run () {This.start ();
        This.engineboom ();
        if (This.isalarm ()) {this.alarm ();
  } this.stop ();  Protected Boolean isalarm () {return true;

    } class Bmwx3model extends bmwmodel{private Boolean alarmflag = true;
    Protected Boolean isalarm () {return this.alarmflag;
    } public void Setalarm (Boolean isalarm) {this.alarmflag = Isalarm; @Override protected void Start () {//TODO auto-generated Method stub System.out.println ("BMW 3 Series Startup
    "); @Override protected void Stop () {//TODO auto-generated Method stub System.out.println ("BMW 3-Series Parking
    "); @Override protected void Alarm () {//TODO auto-generated Method stub System.out.println ("BMW 3-Series
    Flute "); @Override protected void Engineboom () {//TODO auto-generated Method stub System.out.println ("
    BMW 3 Series engine ring ");  } class Bmwx5model extends bmwmodel{@Override protected void Start () {//TODO auto-generated method
    Stub System.out.println ("BMW 5 Series start"); }

    @Override protected void Stop () {//TODO auto-generated Method stub System.out.println ("BMW 5-Series Stop"); @Override protected void Alarm () {//TODO auto-generated Method stub System.out.println ("BMW 5 Series
    Whistle "); @Override protected void Engineboom () {//TODO auto-generated Method stub System.out.println ("
    BMW 5 Series engine ring ");
    Protected Boolean isalarm () {return false; }

}

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.