• design patterns: factory and abstractFactory


    Factory:

    适合单个一对一 工厂产品。


    public class Test {
        public static void main(String[] args) {
            // interface -- could not new interface
    //        Factory[] f = {new FactoryA(), new FactoryB()};
    //        for (Factory fa : f) {
    //            fa.Create();
    //        }
           
            // abstract
            FactoryAbstract[] f = new FactoryAbstract[2];
           
            f[0] = new FactoryA();
            f[1] = new FactoryB();
           
            for (FactoryAbstract fa : f) {
                Product p = fa.FactoryMethod();
                System.out.println("Product: " + p.getClass().getSimpleName());
            }
        }

    }


    public interface Factory {
        Product Create();
    }
    public abstract class FactoryAbstract {
        abstract Product FactoryMethod();
    }
    public class FactoryA extends FactoryAbstract implements Factory {

        @Override
        public Product Create() {
            return new ProductA();
        }

        @Override
        Product FactoryMethod() {
            return new ProductA();
        }
    }
    public class FactoryB extends FactoryAbstract implements Factory {

        @Override
        public Product Create() {
            return new ProductB();
        }

        @Override
        Product FactoryMethod() {
            return new ProductB();
        }
    }


    public interface Product {

    }
    public class ProductA implements Product{
    }
    public class ProductB implements Product{
    }

    AbstractFactory

    适合多个工厂 系列产品。


    public class Test {
        public static void main(String[] args) {
           
            Client c1 = new Client(new ConcreteFactory1());
            c1.run();
           
            Client c2 = new Client(new ConcreteFactory2());
            c2.run();
        }
    }

    public class Client {

        private AbstractProductA productA;
        private AbstractProductB productB;
       
        public Client(AbstractFactory factory) {
            productA = factory.createProductA();
            productB = factory.createProductB();
        }
       
        public void run() {
            productB.Interact(productA);
        }
    }

    public interface AbstractFactory {
       
        AbstractProductA createProductA();
        AbstractProductB createProductB();
    }
    public class ConcreteFactory1 implements AbstractFactory {

        @Override
        public AbstractProductA createProductA() {
            return new ProductA1();
        }

        @Override
        public AbstractProductB createProductB() {
            return new ProductB1();
        }   
    }
    public class ConcreteFactory2 implements AbstractFactory {

        @Override
        public AbstractProductA createProductA() {
            return new ProductA2();
        }

        @Override
        public AbstractProductB createProductB() {
            return new ProductB2();
        }   
    }

    public interface AbstractProductA {

    }
    public interface AbstractProductB {
        void Interact(AbstractProductA productA);
    }

    public class ProductA1 implements AbstractProductA {

    }
    public class ProductA2 implements AbstractProductA {

    }

    public class ProductB1 implements AbstractProductB {

        @Override
        public void Interact(AbstractProductA productA) {
            System.out.println(this.getClass().getSimpleName() + " interact " +
                    productA.getClass().getSimpleName());
        }   
    }
    public class ProductB2 implements AbstractProductB {

        @Override
        public void Interact(AbstractProductA productA) {
            System.out.println(this.getClass().getSimpleName() + " interact " +
                    productA.getClass().getSimpleName());       
        }
    }

  • 相关阅读:
    吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons)
    Logical partitioning and virtualization in a heterogeneous architecture
    十条实用的jQuery代码片段
    十条实用的jQuery代码片段
    十条实用的jQuery代码片段
    C#比较dynamic和Dictionary性能
    C#比较dynamic和Dictionary性能
    C#比较dynamic和Dictionary性能
    分别使用 XHR、jQuery 和 Fetch 实现 AJAX
    分别使用 XHR、jQuery 和 Fetch 实现 AJAX
  • 原文地址:https://www.cnblogs.com/tiechui/p/1905381.html
Copyright © 2020-2023  润新知