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

Source: Internet
Author: User
This example continues the orchard farm in the last factory method. This time, the farm company introduced plastic shed technology to plant tropical (tropical) fruits and vegetables in the greenhouse.

1. Abstract factory instance class diagram


2. Java Implementation CodePackage cn.edu.ynu.sei.abstractFactory;

/**
* Fruit Interface
* @author 88250
* @version 1.0.0, 2007-8-13
* @see Cn.edu.ynu.sei.factoryMethod.Fruit
*/
public interface Fruit
{

}


Package cn.edu.ynu.sei.abstractFactory;

/**
* Tropical Fruits
*
* @author 88250
* @version 1.0.0, 2007-8-13
*/
public class Tropicalfruit implements Fruit
{
/**
* Fruit Name
*/
private String name;

/**
* Builder
* @param name Fruit names
*/
Public Tropicalfruit (String name)
{
this. Name = name;
}

/**
* @return The name
*/
Public String GetName ()
{
return name;
}

/**
* @param name the name to set
*/
public void SetName (String name)
{
this. Name = name;
}
}
Package cn.edu.ynu.sei.abstractFactory;

/**
* Northern Fruit interface
*
* @author 88250
* @version 1.0.0, 2007-8-13
*/
public class Northernfruit implements Fruit
{
/**
* Fruit Name
*/
private String name;

/**
* Builder
* @param name Fruit names
*/
Public Northernfruit (String name)
{
this. Name = name;
}

/**
* @return The name
*/
Public String GetName ()
{
return name;
}

/**
* @param name the name to set
*/
public void SetName (String name)
{
this. Name = name;
}


}


Package cn.edu.ynu.sei.abstractFactory;

/**
* Vegetable Interface
*
* @author 88250
* @version 1.0.0, 2007-8-13
* @see Cn.edu.ynu.sei.factoryMethod.Fruit
*/
public interface Veggie
{

}


Package cn.edu.ynu.sei.abstractFactory;

/**
* Northern Vegetables
*
* @author 88250
* @version 1.0.0, 2007-8-13
*/
public class Northernveggie implements Veggie
{
/**
* Vegetable Name
*/
private String name;

/**
* Builder
* @param name Vegetable name
*/
Public Northernveggie (String name)
{
this. Name = name;
}

/**
* @return The name
*/
Public String GetName ()
{
return name;
}

/**
* @param name the name to set
*/
public void SetName (String name)
{
this. Name = name;
}
}


Package cn.edu.ynu.sei.abstractFactory;

/**
* Tropical Vegetables
*
* @author 88250
* @version 1.0.0, 2007-8-13
*/
public class Tropicalveggie implements Veggie
{
/**
* Vegetable Name
*/
private String name;

/**
* Builder
* @param name Vegetable name
*/
Public Tropicalveggie (String name)
{
this. Name = name;
}

/**
* @return The name
*/
Public String GetName ()
{
return name;
}

/**
* @param name the name to set
*/
public void SetName (String name)
{
this. Name = name;
}
}


Package cn.edu.ynu.sei.abstractFactory;

/**
* Gardener Interface
*
* @author 88250
* @version 1.0.0, 2007-8-13
*/
public interface Gardener
{
/**
* Fruit Factory method to produce a fruit
* @param name Fruit name
* @return Fruit
*/
Public abstract Fruit Createdfruit (String name);

/**
* Vegetable Factory method produces a kind of vegetable
* @param name Vegetable name
* @return Vegetables
*/
Public abstract Veggie Createveggie (String name);
}


Package cn.edu.ynu.sei.abstractFactory;

/**
* North Garden Gardener
*
* @author 88250
* @version 1.0.0, 2007-8-13
*/
public class Northengardener implements gardener
{
@Override
Public Fruit createdfruit (String name)
{
return new Northernfruit (name);
}

@Override
Public Veggie Createveggie (String name)
{
return new Northernveggie (name);
}
}


Package cn.edu.ynu.sei.abstractFactory;

/**
* Tropical Garden Gardener
*
* @author 88250
* @version 1.0.0, 2007-8-13
* * Public class Tropicalgardener implements gardener
{

@Override
Public Fruit createdfruit (String name)
{
return new Tropicalfruit (name);
}

@Override
Public Veggie Createveggie (String name)
{
return new Tropicalveggie (name);
}
}


3. Summary
Here we would also discuss the support of the abstract factory model for the most basic OCP principles in object-oriented principles. when adding a new product family we just need to add new specific factory roles and specific product roles to the system, no need to modify the original factory or product role code, so at this time the abstract mode is fully supported OCP. When you add a new product hierarchy, you need to modify the specific factory code associated with it. For example, in the farm company system, there is a tropical fruit category inherited from the current tropical fruit category, then we have to modify the tropical fruit specific plant code, a new plant to add a method to it. This is not in line with OCP.

So the abstract factory is only to some extent to support the OCP, in the design time this is worth our attention.

4. Reference "Design Patterns", "Java and model"

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.