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 }
  • 相关阅读:
    iOS中Zbar二维码扫描的使用
    SOJ 1135. 飞跃原野
    SOJ 1048.Inverso
    SOJ 1219. 新红黑树
    SOJ 1171. The Game of Efil
    SOJ 1180. Pasting Strings
    1215. 脱离地牢
    1317. Sudoku
    SOJ 1119. Factstone Benchmark
    soj 1099. Packing Passengers
  • 原文地址:https://www.cnblogs.com/xiongjiawei/p/6836688.html
Copyright © 2011-2022 走看看