Java implementation of simple factory (easy Factory) mode [00 original]__java

Source: Internet
Author: User

These days are learning design patterns, read the book, must write a simple example bar:-)
So, from today began to write a simple pure design pattern to implement the Java code, the first more shallow to experience the idea of design patterns. So that it can be used correctly later in the actual project.

In this example, Arttracer's accusation is to draw a variety of graphics according to the customer's request; The shape interface provides some common drawing operations, which are implemented by 3 graphic classes, and thrown badshapeexception when the drawing is abnormal.

1. Simple factory instance UML class diagram

2. Java Implementation Code
Package cn.edu.ynu.sei.simpleFactory;

/**
* Customer, require drawing (<code>ArtTracer</code>) instances to draw the desired graphics
*
* @author 88250
* @version 1.0.0, 2007-8-12
*/
public class Client
{

/**
* @param args
*/
public static void Main (string[] args)
{
Shape myShape = null;

Try
{
MyShape = Arttracer.factory ("Circle");
Myshape.draw ();
MyShape = Arttracer.factory ("Square");
Myshape.draw ();
MyShape = arttracer.factory ("triangle");
Myshape.draw ();
Myshape.erase ();
Arttracer.factory ("Diamond"); Throws an exception
}
catch (badshapeexception BSE)
{
Bse.printstacktrace ();
}
catch (Exception e)
{
E.printstacktrace ();
}
}
}



Package cn.edu.ynu.sei.simpleFactory;

/**
* Graphic drawing device, drawing the customer needs graphics
* @author 88250
* @version 1.0.0, 2007-8-12
* @uml. Dependency supplier= "Cn.edu.ynu.sei.simpleFactory.Shape"
* @uml. Dependency supplier= "Cn.edu.ynu.sei.simpleFactory.BadShapeException"
*/
public class Arttracer
{
/**
* Generate the desired graphic instance
* @param which graphic description
* @return Graphic Example
* @throws badshapeexception to produce graphics anomalies
*/
public static Shape factory (String which) throws Badshapeexception
{
if (Which.equalsignorecase ("Circle"))
{
return new Circle ();
}
else if (which.equalsignorecase ("Square"))
{
Return to New Square ();
}
else if (which.equalsignorecase ("triangle"))
{
return new triangle ();
}
Else
{
throw new Badshapeexception (which);
}
}
}


Package cn.edu.ynu.sei.simpleFactory;

/**
* Graphics Anomaly <br>
* For example, this exception is thrown when a graphic that the customer needs cannot be produced
* @author 88250
* @version 1.0.0, 2007-8-12
*/
public class Badshapeexception extends Exception
{
/**
* Exception class Version ID
*/
Private static final long serialversionuid = 4781771889363091309L;

/**
* Builder
* @param message Exception information
*/
Public badshapeexception (String message)
{
Super (message + "has any implementation!");
}
}


Package cn.edu.ynu.sei.simpleFactory;

/**
* Generalized graphical Operation interface
* @author 88250
* @version 1.0.0, 2007-8-12
*/
public interface Shape
{

/**
* Drawing Graphics
*/
public abstract void Draw ();

/**
* Erase Graphics
*/
public abstract void erase ();

}


Package cn.edu.ynu.sei.simpleFactory;

/**
* Round
*
* @author 88250
* @version 1.0.0, 2007-8-12
*/
public class Circle implements Shape
{
@Override
public void Draw ()
{
System.out.println ("Circle.draw ()");
}

@Override
public void Erase ()
{
System.out.println ("Circle.erase ()");
}

}


Package cn.edu.ynu.sei.simpleFactory;

/**
* Square
* @author 88250
* @version 1.0.0, 2007-8-12
*/
public class Square implements Shape
{

@Override
public void Draw ()
{
System.out.println ("Square.draw ()");
}

@Override
public void Erase ()
{
System.out.println ("Square.erase ()");
}

}


Package cn.edu.ynu.sei.simpleFactory;

/**
* Triangle
*
* @author 88250
* @version 1.0.0, 2007-8-12
*/
public class Triangle implements Shape
{

@Override
public void Draw ()
{
System.out.println ("Triangle.draw ()");
}

@Override
public void Erase ()
{
System.out.println ("Triangle.erase ()");
}

}


3. SummaryThe core of a simple factory is the factory class. When it determines when to create the desired product, the client can dispense with the task of directly creating the product,
It is more in line with the logic of our daily life to say what you want. For instance, when you need a toothbrush, it's
A few people will make their own toothbrush. Most people should go to the toothbrush shop to buy XX model of the toothbrush, pay is of course,
But worry oh, hey. But toothbrush shop owner to worry about, management so many different varieties, different models of toothbrushes, a new breed also need to hurry
Go to stock >_<~~~ so, the responsibility of the factory class is very heavy, if added the new product is bound to modify its source code.
Remember the OCP (open-closed principle) as mentioned in the abstract of object-oriented design principles. This is a very important principle in ood.
Design should do this, then does the simple factory model support OCP?
Answer: It can only be supported on a limited scale. Because the OCP requirement cannot modify the behavior of "publish", such as a field or method, it can be
To expand, you can add fields or methods. The factory class in the simple factory model cannot do this, when the new product class is added
You have to modify the logic of the factory class to create product methods.
Still, the simple factory model is important because it is one of many design patterns, and sometimes it is often simple
Design can do a better job.

4. Reference Documents"Design Patterns", "Java and Mode"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.