ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Abstract factory
    Modeling/DesignPattern 2019. 8. 23. 06:38

    1. Overview

    The abstract factory pattern provides a way to encapsulate a group of individual factories that have a common theme without specifying their concrete classes. In normal usage, the client software creates a concrete implementation of the abstract factory and then uses the generic interface of the factory to create the concrete objects that are part of the theme. The client doesn't know (or care) which concrete objects it gets from each of these internal factories since it uses only the generic interfaces of their products. This pattern separates the details of the implementation of a set of objects from their general usage and relies on object composition, as object creation is implemented in methods exposed in the factory interface.

    2. Description

    2.1 Motivation

    • The abstract factory is used when we have two or more objects which work together forming a kit or set and there can be multiple sets or kits that can be created by client code.
    • So we separate client code from concrete objects forming such a set and also from the code which creates these sets.

    2.2 Consideration

    Adding a new product type requires changes to the base factory as well as all implementations fo factory

    2.2.1 Comparison between Abstract Factory and Factory Method

    Abstract Factory Factory Method
    Hides factories as well as concrete objects used from the client code Hides the concrete objects which are used from the client code
    Suitable when multiple objects are designed to work together & the client must use products from a single family at a time Concerned with one product and It's subclasses. Collaboration of product itself with other objects is irrelevant

    2.3 Pitfalls

    • Adding a new product type requires changes to the base factory as well as all implementations fo factory
    • Abstract factory design pattern is very specific to the problem of product families

    3. Example

    interface Instance {
        enum Capacity{micro, small, large}
        void start();
        void attachStorage(Storage storage);
        void stop();
    }
    
    interface Storage {
        String getId();
    }
    
    class S3Storage implements Storage {
        public S3Storage(int capacityInMib) {
            System.out.println("Allocated " + capacityInMib + " on S3");
        }
        @Override
        public String getId() {
            return "S31";
        }
        @Override
        public String toString() {
            return "S3 Storage";
        }
    }
    
    class GoogleCloudStorage implements Storage {
        public GoogleCloudStorage(int capacityInMib) {
            System.out.println("Allocated " + capacityInMib + " on Google Cloud Storage");
        }
        @Override
        public String getId() {
            return "gcpcs1";
        }
        @Override
        public String toString() {
            return "Google cloud storage";
        }
    }
    
    class Ec2Instance implements Instance {
        public Ec2Instance(Capacity capacity) {
            System.out.println("Created Ec2Instance");
        }
        @Override
        public void start() {
            System.out.println("Ec2Instance started");
        }
        @Override
        public void attachStorage(Storage storage) {
            System.out.println("attached " +storage +" to Ec2Instance");
        }
        @Override
        public void stop() {
            System.out.println("Ec2Instance stopped");
        }
    }
    
    class GoogleComputeEngineInstance implements Instance {
        public GoogleComputeEngineInstance(Capacity capacity) {
            System.out.println("Created Google Compute Engine instance");
        }
        @Override
        public void start() {
            System.out.println("Compute engine instance started");
        }
        @Override
        public void attachStorage(Storage storage) {
            System.out.println("Attached " + storage + "to compute engine instance");
        }
        @Override
        public void stop() {
            System.out.println("Compute engine instance stopped");
        }
    }
    interface ResourceFactory {
        Instance createInstance(Instance.Capacity capacity);
        Storage createStorage(int capMib);
    }
    class AwsResourceFactory implements ResourceFactory {
        @Override
        public Instance createInstance(Instance.Capacity capacity) {
            return new Ec2Instance(capacity);
        }
    
        @Override
        public Storage createStorage(int capMib) {
            return new S3Storage(capMib);
        }
    }
    class GoogleResourceFactory implements ResourceFactory {
        @Override
        public Instance createInstance(Instance.Capacity capacity) {
            return new GoogleComputeEngineInstance(capacity);
        }
    
        @Override
        public Storage createStorage(int capMib) {
            return new GoogleCloudStorage(capMib);
        }
    }
    
    public class DemoClient {
        private ResourceFactory factory;
        public DemoClient(ResourceFactory factory) {
            this.factory = factory;
        }
    
        public Instance createServer(Instance.Capacity cap, int storageMib) {
            Instance instance = factory.createInstance(cap);
            Storage storage = factory.createStorage(storageMib);
            instance.attachStorage(storage);
            return instance;
        }
        public static void main(String[] args) {
            DemoClient aws = new DemoClient(new AwsResourceFactory());
            Instance i1 = aws.createServer(Instance.Capacity.micro, 20480);
            i1.start();
            i1.stop();
            System.out.println("-------------------------------------------");
            DemoClient gcp = new DemoClient(new GoogleResourceFactory());
            i1 = aws.createServer(Instance.Capacity.micro, 20480);
            i1.start();
            i1.stop();
        }
    }

    4. Usage

    5. Reference

    https://en.wikipedia.org/wiki/Abstract_factory_pattern

    'Modeling > DesignPattern' 카테고리의 다른 글

    Model–view–controller (MVC)  (0) 2020.02.23
    Categorizing Design Pattern  (0) 2019.09.29
    Factory Pattern  (0) 2019.08.22
    Simple Factory Pattern  (0) 2019.08.22
    Bridge Design Pattern  (0) 2019.08.19

    댓글

Designed by Tistory.