num13---外观模式/过程模式

num13---外观模式/过程模式

 

 num13---外观模式/过程模式

 

 num13---外观模式/过程模式

 

 假设家庭影院有一系列设备,每个设备都有各种开闭等功能性方法,使用家庭影院功能的时候,需要进行各个设备的一系列操作,繁琐麻烦。

现在提供一个外观类,在里面定义操作流程,客户端只需要和外观类进行接口交互即可,不用管 具体的操作流程。

代码如下:

定义多个影院设备类:

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

public class DVDPlayer {

     

    private static DVDPlayer instance = new DVDPlayer();

     

    public static DVDPlayer getInstanc() {

        return instance;

    }

     

    public void on() {

        System.out.println(" dvd on ");

    }

    public void off() {

        System.out.println(" dvd off ");

    }

     

    public void play() {

        System.out.println(" dvd is playing ");

    }

     

    //....

    public void pause() {

        System.out.println(" dvd pause ..");

    }

}

  

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

public class Popcorn {

     

    private static Popcorn instance = new Popcorn();

     

    public static Popcorn getInstance() {

        return instance;

    }

     

    public void on() {

        System.out.println(" popcorn on ");

    }

     

    public void off() {

        System.out.println(" popcorn ff ");

    }

     

    public void pop() {

        System.out.println(" popcorn is poping  ");

    }

}

  

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

public class Projector {

 

    private static Projector instance = new Projector();

     

    public static Projector getInstance() {

        return instance;

    }

     

    public void on() {

        System.out.println(" Projector on ");

    }

     

    public void off() {

        System.out.println(" Projector ff ");

    }

     

    public void focus() {

        System.out.println(" Projector is Projector  ");

    }

     

    //...

}

  

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

public class Screen {

 

    private static Screen instance = new Screen();

     

    public static Screen getInstance() {

        return instance;

    }

     

     

     

     

    public void up() {

        System.out.println(" Screen up ");

    }

     

    public void down() {

        System.out.println(" Screen down ");

    }

     

 

}

  

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

public class Stereo {

 

    private static Stereo instance = new Stereo();

     

    public static Stereo getInstance() {

        return instance;

    }

     

    public void on() {

        System.out.println(" Stereo on ");

    }

     

    public void off() {

        System.out.println(" Screen off ");

    }

     

    public void up() {

        System.out.println(" Screen up.. ");

    }

     

    //...

}

  

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

public class TheaterLight {

 

    private static TheaterLight instance = new TheaterLight();

 

    public static TheaterLight getInstance() {

        return instance;

    }

 

    public void on() {

        System.out.println(" TheaterLight on ");

    }

 

    public void off() {

        System.out.println(" TheaterLight off ");

    }

 

    public void dim() {

        System.out.println(" TheaterLight dim.. ");

    }

 

    public void bright() {

        System.out.println(" TheaterLight bright.. ");

    }

}

  

创建外观类:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

public class HomeTheaterFacade {

     

    //定义影院系统 各个子系统模块对象

    private TheaterLight theaterLight;

    private Popcorn popcorn;

    private Stereo stereo;

    private Projector projector;

    private Screen screen;

    private DVDPlayer dVDPlayer;

    //构造器

    public HomeTheaterFacade() {

        super();

        this.theaterLight = TheaterLight.getInstance();

        this.popcorn = Popcorn.getInstance();

        this.stereo = Stereo.getInstance();

        this.projector = Projector.getInstance();

        this.screen = Screen.getInstance();

        this.dVDPlayer = DVDPlayer.getInstanc();

    }

    //操作分为四部

 

    public void ready() {

        popcorn.on();

        popcorn.pop();

        screen.down();

        projector.on();

        stereo.on();

        dVDPlayer.on();

        theaterLight.dim();

    }

    public void play() {

        dVDPlayer.play();

    }

    public void pause() {

        dVDPlayer.pause();

    }

    public void end() {

        popcorn.off();

        theaterLight.bright();

        screen.up();

        projector.off();

        stereo.off();

        dVDPlayer.off();

    }

}

  测试:

1

2

3

4

5

6

7

public static void main(String[] args) {

    HomeTheaterFacade homeTheaterFacade = new HomeTheaterFacade();

    homeTheaterFacade.ready();

    homeTheaterFacade.play();

     

    homeTheaterFacade.end();

}

====================================================

num13---外观模式/过程模式

 

 num13---外观模式/过程模式

 

 num13---外观模式/过程模式

 

 

  ----------------------------------------------------------------------------------------------------------------------------------------

num13---外观模式/过程模式

相关文章:

猜你喜欢
  • 2021-07-08
  • 2022-03-06
  • 2021-07-20
  • 2022-12-23
  • 2021-05-30
相关资源
相似解决方案