zoukankan      html  css  js  c++  java
  • 【java设计模式】【创建模式Creational Pattern】工厂方法模式Factory Method Pattern(多态性工厂模式Polymorphic Factory Pattern、虚拟构造子模式Virtual Constructor Pattern)

     1 public class Test {
     2     public static void main(String[] args){
     3         Creator ca=new ConcreteCreatorA();
     4         ca.create().doSth();
     5         Creator cb=new ConcreteCreatorB();
     6         cb.create().doSth();
     7     }
     8 }
     9 interface Creator{
    10     Product create();
    11 }
    12 class ConcreteCreatorA implements Creator{
    13     @Override
    14     public Product create() {
    15         return new ConcreteProductA();
    16     }
    17 }
    18 class ConcreteCreatorB implements Creator{
    19     @Override
    20     public Product create() {
    21         return new ConcreteProductB();
    22     }
    23 }
    24 interface Product{
    25     void doSth();
    26 }
    27 class ConcreteProductA implements Product{
    28     
    29     public ConcreteProductA(){
    30         System.out.println("Product A has been created.");
    31     }
    32     @Override
    33     public void doSth() {
    34         System.out.println("Product A is working……");
    35     }
    36 }
    37 class ConcreteProductB implements Product{
    38     public ConcreteProductB(){
    39         System.out.println("Product B has been created.");
    40     }
    41     @Override
    42     public void doSth() {
    43         System.out.println("Product B is working……");
    44     }
    45 }
  • 相关阅读:
    【成本管理】成本核算
    CW23:Work Log
    SQLSERVER数据库连接
    Oracle 创建用户 修改用户密码 授权命令
    CW24:WORK LOG
    ORA12560: TNS: 协议适配器错误的解决方法
    需求工程概述
    日语学习1:送气音和不送气音
    junit测试框架简单应用
    Java之Socket编程
  • 原文地址:https://www.cnblogs.com/xiongjiawei/p/6836688.html
Copyright © 2011-2022 走看看