• 【java设计模式】【创建模式Creational Pattern】建造模式Builder Pattern


     1 package com.tn.pattern;
     2 
     3 public class Client {
     4     public static void main(String[] args) {
     5         Director director=Director.getInstance();
     6         director.construct(new ConcreteBuilder1());        
     7         director.construct(new ConcreteBuilder2());
     8     }
     9 }
    10 
    11 class Director{
    12     static Director director;
    13     private Director(){}
    14     public static Director getInstance(){
    15         if(director==null)
    16             director=new Director();
    17         return director;
    18     }
    19     public void construct(Builder builder){
    20         builder.buildPart1();
    21         builder.buildPart2();
    22     }
    23 }
    24 
    25 interface Builder{
    26     void buildPart1();
    27     void buildPart2();
    28     Product retrieveResult();
    29 }
    30 
    31 class ConcreteBuilder1 implements Builder{
    32     Product product;
    33     @Override
    34     public void buildPart1() {
    35         System.out.println("ConcreteBuilder1.buildPart1()");
    36         product=new Product1();
    37     }
    38     
    39     @Override
    40     public void buildPart2() {}
    41     
    42     @Override
    43     public Product retrieveResult() {
    44         return product;
    45     }
    46 }
    47 
    48 class ConcreteBuilder2 implements Builder{
    49     Product product;
    50     @Override
    51     public void buildPart1() {}
    52     
    53     @Override
    54     public void buildPart2() {
    55         System.out.println("ConcreteBuilder2.buildPart2()");
    56         product=new Product2();
    57     }
    58     
    59     @Override
    60     public Product retrieveResult() {
    61         return product;
    62     }
    63 }
    64 
    65 interface Product{}
    66 
    67 class Product1 implements Product{
    68     public Product1(){
    69         System.out.println("Product1.Product1()");
    70     }
    71 }
    72 
    73 class Product2 implements Product{
    74     public Product2(){
    75         System.out.println("Product2.Product2()");
    76     }
    77 }
  • 相关阅读:
    [译]《Sphinx权威指南》
    sphinx 配置文件全解析
    利用 crontab 來做 Linux 固定排程
    http协议中用于上传多个文件的 multipart 字段
    Python 代码覆盖率统计工具 coverage.py
    Disruptor 基础篇
    NPM:常用命令的生命周期脚本
    十大经典排序算法(动图演示)【转】
    TypeScript Jest 调试
    NPM: 日常开发环境配置
  • 原文地址:https://www.cnblogs.com/xiongjiawei/p/6909888.html
Copyright © 2020-2023  润新知