zoukankan      html  css  js  c++  java
  • Java学习笔记——设计模式之九.建造者模式

     建造者模式(Builder),将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。

    Product类:

     1 package cn.happy.design_pattern._09builder;
     2 
     3 import java.util.ArrayList;
     4 import java.util.List;
     5 
     6 public class Product {
     7 
     8     List<String> parts = new ArrayList<>();
     9     
    10     public void add(String part){
    11         parts.add(part);
    12     }
    13     
    14     public void show(){
    15         System.out.println("创建产品...");
    16         for (String item : parts) {
    17             System.out.println(item);
    18         }
    19     }
    20 }

    Builder类:

    1 package cn.happy.design_pattern._09builder;
    2 
    3 public abstract class Builder {
    4 
    5     public abstract void BuildPartA();
    6     public abstract void BuildPartB();
    7     public abstract Product GetResult();
    8 }

    Builder子类:

     1 package cn.happy.design_pattern._09builder;
     2 
     3 public class ConcreteBuilder1 extends Builder {
     4 
     5     private Product product = new Product();
     6     @Override
     7     public void BuildPartA() {
     8         product.add("部件A");
     9 
    10     }
    11 
    12     @Override
    13     public void BuildPartB() {
    14         product.add("部件B");
    15 
    16     }
    17 
    18     @Override
    19     public Product GetResult() {
    20         // TODO Auto-generated method stub
    21         return product;
    22     }
    23 
    24 }
    25 
    26 public class ConcreteBuilder2 extends Builder {
    27 
    28     private Product product = new Product();
    29     @Override
    30     public void BuildPartA() {
    31         product.add("部件X");
    32 
    33     }
    34 
    35     @Override
    36     public void BuildPartB() {
    37         product.add("部件Y");
    38 
    39     }
    40 
    41     @Override
    42     public Product GetResult() {
    43         // TODO Auto-generated method stub
    44         return product;
    45     }
    46 
    47 }

    Director类:

    1 package cn.happy.design_pattern._09builder;
    2 
    3 public class Director {
    4 
    5     public void Construct(Builder builder){
    6         builder.BuildPartA();
    7         builder.BuildPartB();
    8     }
    9 }

    测试类:

     1 package cn.happy.design_pattern._09builder;
     2 
     3 public class Mmain {
     4     public static void main(String[] args) {
     5         Director director = new Director();
     6         Builder b1 = new ConcreteBuilder1();
     7         Builder b2 = new ConcreteBuilder2();
     8         director.Construct(b1);
     9         b1.GetResult().show();
    10         director.Construct(b2);
    11         b2.GetResult().show();
    12     }
    13 }
  • 相关阅读:
    P2788 数学1(math1)- 加减算式
    数据库第三章-学习笔记
    字典序
    P1739 表达式括号匹配
    P3742 umi的函数
    P1765 手机
    P2192 HXY玩卡片
    全排函数c++ next_permutation()
    11.css定义下拉菜单
    10.php引用(&)详解及注意事项
  • 原文地址:https://www.cnblogs.com/tomasman/p/7044543.html
Copyright © 2011-2022 走看看