Westward design patterns - simple factory pattern (study notes and java realization)

java learning for some time, have some understanding of object-oriented, but always felt deep understanding, these days start to see design mode, using Cheng Jie lying design patterns, but the book's source code is C #, and their use java to achieve, but also deepened the java learning.
1, the definition of simple factory pattern
provides a function to create an object instance, does not care about its implementation. Examples of the type to be created can enable interfaces, abstract classes, may be a concrete class.
2, simple nature plant
simple factory pattern of nature is this: choose to implement. Key is to choose, no matter how simple, must be implemented by a specific category, can not be achieved in a simple factory. Simple plant purpose is to select the appropriate client implemented, so that the client and the decoupling. This way, no matter what happens to the specific implementation, clients do not change, this change is masked by simple factory pattern out.
3, when selecting a simple factory
If you want to completely isolate the specific implementation package, so that can only be operated outside the package through the interface, you can use a simple plant, allowing the client to retrieve the corresponding interface through a factory, without caring about the specific implementation .
If you want to create objects of external responsibilities centralized management and control, you can use a simple factory, a very simple plant can create a lot of unrelated objects, can focus duties of foreign objects to create a simple factory to, in order to achieve focus management and control.
4, a simple factory pattern of the advantages and disadvantages of
advantages: the factory class is the key to the whole model contains the necessary logic judgment, according to outside information given to determine whether an object which specific classes should be created by using the factory class, the outside world can be. emerge from specific products directly create objects of embarrassment, just need to be responsible for "consumer" objects on it. And do we need these objects exactly how to create and how to organize. Clear the respective responsibilities and rights, help to optimize the entire software architecture.

Disadvantages: As the factory class focused on creating a logical all instances of violation of the principle of high cohesion allocation of responsibilities, all to create a logical focus a factory class; it can only be created in advance taking into account the class, if you need to add the new class, we need to change the factory class. When the system is in specific product categories growing time, there may be requirements created by the factory needs of different instances according to different criteria. Such judgment and the judgment of the condition of particular types of products intertwined, it is difficult to avoid the spread of the function modules, maintenance and expansion of the system is very unfavorable; these drawbacks have been overcome in certain factory method mode.

/*Client.java   简单计算器的客户端    */
package Test1;

import java.io.Console;
import java.io.PrintWriter;
import java.util.Scanner;

public class Client {
    public static void main(String[] args) {
        Operation result=null;
        Scanner scin=new Scanner(System.in);    
                try {
                    System.out.print("请输入数字A: ");
                    double NumA=scin.nextDouble();
                    System.out.print("请输入运算符(+、-、*、/): ");
                    String strOperate=scin.next();
                    System.out.print("请输入数字B: ");
                    double NumB=scin.nextDouble();
                    result=OperationFactory.createOperate(strOperate);
                    result.setA(NumA);
                    result.setB(NumB);
                    System.out.println("结果是: "+result.getResult());
                } catch (Exception e) {
                    System.out.println("您输入有错: "+e);
            }

    }

}
/*计算部分的超类*/
package Test1;

public class Operation {
    protected double numberA=0;
    protected double numberB=0;

        double getA()
        {
            return numberA;
        }
        double setA(double value)
        {
            this.numberA=value;
            return numberA;
        }


        double getB()
        {
            return numberB;
        }
        double setB(double value)
        {
            this.numberB=value;
            return numberB;
        }
    public double getResult()
    {
        double result=0;
        return result;
    }
}

`public class OperationAdd extends Operation{
public double getResult()
{
double result=0;
result=numberA+numberB;
return result;
}

}
package Test1;

public class OperationDiv extends Operation{
public double getResult(double numberA,double numberB)
{
double result=0;
if(numberB==0)
try {
throw new Exception(“除数不能是0.”);
} catch (Exception e) {
e.printStackTrace();
}
result=numberA/numberB;
return result;
}

}
package Test1;

public class OperationMul extends Operation{
public double getResult()
{
double result=0;
result=numberA*numberB;
return result;
}

}
package Test1;

public class OperationSub extends Operation{
public double getResult()
{
double result=0;
result=numberA-numberB;
return result;
}

}
`


/*工厂*/
package Test1;

public class OperationFactory {
    //static double numberA,numberB;
    public static Operation createOperate(String operate)
    {
        Operation result=null;

        switch(operate)
        {
        case "+":
            result=new OperationAdd();
            break;
        case "-":
            result=new OperationSub();
            break;
        case "*":
            result=new OperationMul();
            break;
        case "/":
            result=new OperationDiv();
            break;

        default:
            break;
        }
        return result;
    }

}
Released nine original articles · won praise 4 · views 10000 +

Guess you like

Origin blog.csdn.net/juan190755422/article/details/44961667