• Abstract Factory


    1.意图

    提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。

    Code in Java:

    static class Expression {
            protected String str;
            public Expression(String s) {
                str = s;
            }
            
            public Expression cloan() {
                return null;
            }
            
            public String toString() {
                return str;
            }
        }
        
        static abstract class Factory {
            protected Expression prototype = null;
            
            public Expression makephrase() {
                return prototype.cloan();
            }
            
            public abstract Expression makeCompromise();
            
            public abstract Expression makeGrade();
        }
        
         static class PCPhrase extends Expression
            {
                static String[] list = 
                {
                    ""animal companion"", ""vertically challenged"", 
                        ""factually inaccurate"", ""chronologically gifted""
                };
                private static int next = 0;
                public PCPhrase()
                {
                    super(list[next]);
                    next = (next + 1) % list.length;
                }
                public Expression cloan()
                {
                    return new PCPhrase();
                }
            }
    
            static class NotPCPhrase extends Expression
            {
                private static String[] list = 
                {
                    ""pet"", ""short"", ""lie"", ""old""
                };
                private static int next = 0;
                public NotPCPhrase()
                {
                    super(list[next]);
                    next = (next + 1) % list.length;
                }
                public Expression cloan()
                {
                    return new NotPCPhrase();
                }
            }
        
        static class PCFactory extends Factory {
    
            public PCFactory()
            {
                prototype = new PCPhrase();
            }
            
            @Override
            public Expression makeCompromise() {
                // TODO Auto-generated method stub
                return new Expression(""do it your way, any way, or no way"");
            }
    
            @Override
            public Expression makeGrade() {
                // TODO Auto-generated method stub
                return new Expression(""you pass, self-esteem intact"");
            }
            
        }
        
        static class NotPCFactory extends Factory
        {
            public NotPCFactory()
            {
                prototype = new NotPCPhrase();
            }
            public Expression makeCompromise()
            {
                return new Expression(""my way, or the highway"");
            }
            public Expression makeGrade()
            {
                return new Expression(""take test, deal with the results"");
            }
        }
        
         public static void main(String[] args)
            {
                Factory factory;
                if (args.length <= 0)
                    factory = new PCFactory();
                else
                    factory = new NotPCFactory();
                for (int i = 0; i < 3; i++)
                    System.out.print(factory.makephrase() + "  ");
                System.out.println();
                System.out.println(factory.makeCompromise());
                System.out.println(factory.makeGrade());
            }

    Code in C#

     class Program
        {
            /// <summary>
            /// abstract class AbstractProductA
            /// </summary>
            abstract class AbstractProductA
            {
                
            }
    
            /// <summary>
            /// abstract class AbstractProductB
            /// </summary>
            abstract class AbstractProductB
            {
                public abstract void Interact(AbstractProductA a);
            }
    
            /// <summary>
            /// class ProductA2
            /// </summary>
            class ProductA1 : AbstractProductA
            {
            }
    
            /// <summary>
            /// class ProductA2
            /// </summary>
            class ProductA2 : AbstractProductA
            {
            }
    
            /// <summary>
            /// class ProductB1
            /// </summary>
            class ProductB1 : AbstractProductB
            {
                public override void Interact(AbstractProductA a)
                {
                    Console.WriteLine(this.GetType().Name + " interacts with " + a.GetType().Name);
                }
            }
    
            /// <summary>
            /// class Product2
            /// </summary>
            class ProductB2 : AbstractProductB
            {
                public override void Interact(AbstractProductA a)
                {
                    Console.WriteLine(this.GetType().Name + " interacts with " + a.GetType().Name);
                }
    
            }
    
            /// <summary>
            /// abstract class AbstractFactory
            /// </summary>
            abstract class AbstractFactory
            {
                public abstract AbstractProductA createProductA();
                public abstract AbstractProductB createProductB();
            }
    
            /// <summary>
            /// class ConcreteFacroty1
            /// </summary>
            class ConcreteFactory1 : AbstractFactory
            {
                public override AbstractProductA createProductA()
                {
                    return new ProductA1();
                }
    
                public override AbstractProductB createProductB()
                {
                    return new ProductB1();
                }
            }
    
            /// <summary>
            /// class ConcreteFacroty2
            /// </summary>
            class ConcreteFactory2 : AbstractFactory
            {
                public override AbstractProductA createProductA()
                {
                    return new ProductA2();
                }
    
                public override AbstractProductB createProductB()
                {
                    return new ProductB2();
                }
            }
    
            class Client
            {
                private AbstractProductA abstractProductA;
                private AbstractProductB abstractProductB;
    
                public Client(AbstractFactory factory)
                {
                    abstractProductA = factory.createProductA();
                    abstractProductB = factory.createProductB();
                }
    
                public void Run()
                {
                    if (null != abstractProductB && null != abstractProductA)
                    {
                        abstractProductB.Interact(abstractProductA);
                    }
                }
            }
    
            static void Main(string[] args)
            {
                // Abstract factory #1 
                AbstractFactory factory1 = new ConcreteFactory1();
                Client c1 = new Client(factory1);
                c1.Run();
    
                // Abstract factory #2 
                AbstractFactory factory2 = new ConcreteFactory2();
                Client c2 = new Client(factory2);
                c2.Run();
    
                // Wait for user input 
                Console.Read();
            }
        }
  • 相关阅读:
    uva 11995 I Can Guess the Data Structure!
    poj 1981 Circle and Points
    hdoj 2149 Public Sale
    hdoj 2188 悼念512汶川大地震遇难同胞——选拔志愿者
    hdoj 1846 Brave Game
    vue 细节注意
    meta标签设置不缓存
    -webkit-overflow-scrolling
    ios上表单默认样式
    vue-cli项目上传到github预览问题
  • 原文地址:https://www.cnblogs.com/wiessharling/p/3331585.html
Copyright © 2020-2023  润新知