Use Fourier descriptors to identify shapes (matlab)

With the entire program and test pictures in the document, the test can be run.

File: n459.com/file/25127180-478678004

The following are irrelevant:

-------------------------------------------Dividing line----- ----------------------------------------

1. Overview The
  abstract factory pattern provides a unified creation interface for a product family. When a certain series of this product family is needed, the corresponding series can be selected from the abstract factory to create a specific factory category. This type of design pattern is a creational pattern, which provides the best way to create objects.

In the abstract factory pattern, the interface is responsible for creating a factory of related objects, without explicitly specifying their classes. Each generated factory can provide objects according to the factory pattern.

Related terms:

Product hierarchy: The inheritance structure of products is similar to the inheritance of classes. For example, notebooks are an abstract category, so Huawei notebooks, Apple and Lenovo notebooks are its subcategories.

Product family: refers to a group of products produced in the same factory and located in different product hierarchies. For example, Huawei notebooks, mobile phones, routers, etc. are all produced by Huawei. The grade structure of notebooks is different, forming a product family.

Abstract factory: is an interface, the core of the abstract factory pattern, which contains declarations of multiple product hierarchical structures. Any factory class must implement this interface.

Concrete factory: It is the realization of abstract factory that is responsible for instantiating product objects in a product family. For example, the Huawei factory produces Huawei notebooks, mobile phones, routers, etc.
2. Product family and product level structure diagram
  We take one brand as a product family, and computers, mobile phones, and routers as product levels. Each brand has its own product family, which constitutes a complete product group;

The horizontal represents a family, the vertical represents a level, and the horizontal and vertical intersection represents a certain product of a certain brand (for example, the intersection point in the figure below is a computer), please see the figure below;

Third, UML diagram
  This class diagram is actually relatively simple, a brief description:

Product top-level interface: mainly implemented by product abstract classes;

Product abstract class: a class to be realized by a specific product;

Specific implementation category: specific product implementation, such as Huawei router implementation from the abstract class AbstractRouter;

Factory interface: The method of creating each product is defined in the factory interface;

Specific Huawei factory: implement factory interface and create Huawei family of products (routers, mobile phones, computers);

Fourth, the specific code implementation    In the
  code, we take Huawei products as an example to define Huawei computers, mobile phones, and router products respectively. From the UML class diagram, we can see that our product structure is relatively clear. Now we design our products first.

Start to define the product below;

Top product interface;

Copy the code
package pattern.abstractfactory.product;
/**

  • Define product interface
  • @author ningbeibei
    */
    public interface InterfaceProduct { void get(); } Copy code   Define the computer abstract class and implement the product InterfaceProduct interface;



Copy the code
package pattern.abstractfactory.product;
/**

  • Define the abstract class of computer products and implement the product interface InterfaceProduct
  • @author ningbeibei
    */
    public abstract class AbstractComputers implements InterfaceProduct { public abstract void get(); } Copy code   Define the mobile phone abstract class and implement the product InterfaceProduct interface;



Copy the code
package pattern.abstractfactory.product;
/**

  • Define the mobile phone abstract class and implement the product interface InterfaceProduct
  • @author ningbeibei
    */
    public abstract class AbstractPhone implements InterfaceProduct { public abstract void get(); } Copy code   Define the router abstract class and implement the product InterfaceProduct interface;



Copy the code
package pattern.abstractfactory.product;
/**

  • Define the router product abstract class and implement the InterfaceProduct interface
  • @author ningbeibei
    */
    public abstract class AbstractRouter implements InterfaceProduct{ public abstract void get(); } Copy code   Define the concrete implementation class of Huawei computers and inherit the AbstractComputers abstract class;



Copy the code
package pattern.abstractfactory.product;
/**

  • Huawei computer implementation class
  • ningbeibei @author
    * /
    public class HuaWeiComputer the extends AbstractComputers { @Override public void GET () { System.out.println ( "Huawei notebook"); } } copy the code   defined Huawei cell phone implementation class, the abstract class inheritance AbstractPhone;






Copy the code
package pattern.abstractfactory.product;
/**

  • Huawei mobile phone implementation class,
  • @author ningbeibei
    */
    public class HuaWeiPhone extends AbstractPhone{ @Override public void get() { System.out.println("Huawei mobile phone"); } } Copy code   Define the concrete implementation class of Huawei router, inherit the AbstractRouter abstract class;






Copy the code
package pattern.abstractfactory.product;
/**

  • Huawei router
  • @author ningbeibei
    */
    public class HuaWeiRouter extends AbstractRouter { @Override public void get() { System.out.println("Huawei brand router"); } } Copy code   The factory will be defined below;






Define the factory interface;

Copy the code
Package pattern.abstractfactory.factory;
Import pattern.abstractfactory.product.InterfaceProduct;
/ **

  • Define the product factory interface,
  • ningbeibei @author
    * /
    public interface InterfactFactory { // phones InterfaceProduct createPhone (); // computer products InterfaceProduct createComputer (); // routers InterfaceProduct createRouter (); } copy the code   concrete factory implementation class that implements the interface InterfactFactory;








复制代码
package pattern.abstractfactory.factory;
import pattern.abstractfactory.product.HuaWeiComputer;
import pattern.abstractfactory.product.HuaWeiPhone;
import pattern.abstractfactory.product.HuaWeiRouter;
import pattern.abstractfactory.product.InterfaceProduct;
/**

  • Huawei factory
  • @author ningbeibei
    /
    public class HuaWeiFactory implements InterfactFactory {
    /
    *
    • Create a computer object and return
      /
      @Override
      public InterfaceProduct createComputer() { return new HuaWeiComputer(); } /


      *
    • Create a mobile phone object and return
      /
      @Override
      public InterfaceProduct createPhone() { return new HuaWeiPhone(); } /


      *
    • Create a router object and return
      */
      @Override
      public InterfaceProduct createRouter() { return new HuaWeiRouter(); } } Copy code   test class;




复制代码
package pattern.abstractfactory;
import pattern.abstractfactory.factory.HuaWeiFactory;
import pattern.abstractfactory.factory.InterfactFactory;
import pattern.abstractfactory.product.InterfaceProduct;
/**

  • Abstract factory pattern test class
  • @author ningbeibei
    */
    public class test { public static void main(String[] args) { //Create a Huawei brand factory InterfactFactory huawei = new HuaWeiFactory(); //Obtain Huawei computer objects through the Huawei factory InterfaceProduct computer = huawei.createComputer( ); computer.get(); //Obtain Huawei mobile phone objects through Huawei factory InterfaceProduct phone = huawei.createPhone(); phone.get(); //Obtain Huawei router objects through Huawei factory InterfaceProduct router = huawei.createRouter(); router.get(); } } Copy code   Run result;















5. How the abstract factory method model expands the product family. The
  abstract factory model is convenient for horizontal expansion and very difficult for vertical expansion. That is to say: If we want to expand a new brand, such as expanding a Xiaomi brand, Xiaomi products include computers, mobile phones, For routers, expanding a new brand is horizontal expansion, which is very convenient, but it is very difficult for us to add a rice cooker product to Xiaomi. This is vertical expansion, so when using the abstract factory model, we must choose the appropriate scene, that is, in different scenes. Using the most suitable pattern is the essence of design patterns.

Next, let's expand the product family of a new brand horizontally. We need to add specific types of computers, mobile phones, and routers (Xiaomi brand) with the following codes;

Xiaomi Computer

Copy the code
package pattern.abstractfactory.product;
/**

  • Mi computers, inherited from AbstractComputers abstract class
  • @author ningbeibei
    */
    public class MiComputer extends AbstractComputers { @Override public void get() { System.out.println("Millet Computer"); } } Copy code   Xiaomi Mobile






Copy the code
package pattern.abstractfactory.product;
/**

  • Mi mobile phone, inherited from AbstractPhone abstract class
  • @author ningbeibei
    */
    public class MiPhone extends AbstractPhone { @Override public void get() { System.out.println("Mi mobile phone"); } } Copy code   Mi router






Copy the code
package pattern.abstractfactory.product;
/**

  • Mi router, inherited from AbstractRouter abstract class
  • @author ningbeibei
    */
    public class MiRouter extends AbstractRouter{ @Override public void get() { System.out.println("Millet router"); } } Copy code   add the specific millet factory class






复制代码
package pattern.abstractfactory.factory;
import pattern.abstractfactory.product.InterfaceProduct;
import pattern.abstractfactory.product.MiComputer;
import pattern.abstractfactory.product.MiPhone;
import pattern.abstractfactory.product.MiRouter;
/**

  • Mi Factory, which implements the InterfactFactory interface
  • @author ningbeibei
    */
    public class MiFactory implements InterfactFactory{ //Xiaomi mobile phone @Override public InterfaceProduct createPhone() { return new MiPhone(); } //Xiaomi computer @Override public InterfaceProduct createComputer() { return new MiComputer(); } / /Xiaomi router @Override public InterfaceProduct createRouter() { return new MiRouter(); } } Copy the code   Finally write the test category, the red font in the code is the newly extended brand product;

















复制代码
package pattern.abstractfactory;
import pattern.abstractfactory.factory.HuaWeiFactory;
import pattern.abstractfactory.factory.InterfactFactory;
import pattern.abstractfactory.factory.MiFactory;
import pattern.abstractfactory.product.InterfaceProduct;
/**

  • Abstract factory pattern test class

  • @author ningbeibei
    */
    public class test { public static void main(String[] args) { // Create a Huawei brand factory InterfactFactory huawei = new HuaWeiFactory(); // Obtain a Huawei computer object through the Huawei factory InterfaceProduct computer = huawei.createComputer( ); computer.get(); // Obtain Huawei mobile phone object InterfaceProduct through Huawei factory phone = huawei.createPhone(); phone.get(); // Obtain Huawei router object InterfaceProduct router = huawei.createRouter(); router.get();











     // 创建小米品牌工厂
     InterfactFactory Mifactory = new MiFactory();
     // 通过小米工厂获取小米电脑对象
     InterfaceProduct micomputer = Mifactory.createComputer();
     micomputer.get();
     // 通过小米工厂获取小米手机对象
     InterfaceProduct miphone = Mifactory.createPhone();
     miphone.get();
     // 通过小米工厂获取小米路由器对象
     InterfaceProduct mirouter = Mifactory.createRouter();
     mirouter.get();
    

    }
    }
    Copy code
      Run result:

Note: Through the above brand expansion, we found that horizontal expansion is easy, and vertical expansion is very difficult. The code can easily expand an existing product of a brand, but it is extremely difficult to expand an undefined product, such as expanding a Huawei tablet. , It is necessary to modify the factory logic code and add new product structure. This obviously does not conform to the principle of design mode opening and closing, so you must consider it clearly when using it and make sure that there is no new product level extension.

Sixth, the advantages and disadvantages and usage scenarios
advantages

The abstract factory pattern isolates the generation of concrete classes, so that customers do not need to know what is created. Because of this isolation, it is relatively easy to replace a concrete factory. All concrete factories implement the public interfaces defined in the abstract factory. Therefore, only by changing the instance of the concrete factory, the entire software can be changed to some extent. The behavior of the system.

When multiple objects in a family are designed to work together, it can ensure that the client always uses only the objects in the same family.

It is very convenient to add new families, no need to modify the existing system, in line with the "open and close principle".

Disadvantage

Adding a new hierarchical structure is troublesome, requires major modifications to the original system, and even needs to modify the abstract layer code, which obviously brings greater inconvenience and violates the "open and close principle".
scenes to be used

A system should not depend on the details of how specific class instances are created, combined, and expressed. This is important for all types of factory patterns. Users do not need to care about the creation process of objects and decouple the creation and use of objects.

There is more than one family in the system, and only one of them is used each time. The user can dynamically change the family through configuration files and other methods, and can also easily add new families.

The hierarchical structure is stable. After the design is completed, no new hierarchical structure will be added to the system or the existing hierarchical structure will not be deleted.

Seven, I hope to criticize and correct
  the deficiencies in the writing. Please point out the deficiencies in the writing in the comment area, so that I can correct the mistakes in time and avoid misunderstandings for readers. I hope you can provide more opinions.

Guess you like

Origin blog.csdn.net/gumenghua_com1/article/details/112390433