Design Patterns notes three abstract factory pattern

Look at a pizza project: To facilitate the pizza kind of expansion, to facilitate maintenance

  1. Many different types of pizza (such as GreekPizz, CheesePizz, etc.)
  2. Production pizza there prepare, bake, cut, box
  3. Pizzeria order to complete the function.
Using traditional way to accomplish
  1. Thought analysis (FIG class)
    Here Insert Picture Description

The advantages and disadvantages of the traditional way

  1. The advantage is relatively easy to understand, easy to operate.
  2. The disadvantage is a violation of the principles of design patterns ocp, that is open for extension, but closed for modification. That is, when we add new features to the class, try not to modify the code, modify the code or less as possible.
  3. For example, we then want to add a new Pizza species (Pepper pizza), we need to make the following modifications. If we add a class Pizza, Pizza as long as the order of the code needs to be modified.
  4. Improved analysis of ideas
    Analysis: Modify the code is acceptable, but if we have code creates Pizza in other places, it means that also need to be modified, created Pizza code tend to have many.

Ideas: When the Pizza object encapsulates create a class, so we have a new kind of Pizza, you only need to modify the class can be, there are other Pizza to create
objects do not need to modify the code .-> Simple factory pattern

Simple Factory

basic introduction

  1. Simple factory pattern to create a schema is part of, is a factory model. Simple factory pattern is determined by a factory object to create an instance of what kind of product class. Simple factory pattern is the factory model family is the most simple and practical mode

  2. Simple factory pattern: creating a definition of object classes, the instance of the object class to encapsulate behavior (code)

  3. In software development, when we use a lot of create some kind, or if some sort of a batch of objects, it will use the factory pattern.

Use simple factory pattern

  1. Simple factory pattern design: the definition of a class can be instantiated Pizaa object, the package code to create objects.

Here Insert Picture Description

Factory Method pattern

Look at a new demand

The new project requirements pizza: pizza customers in the point, the point may be different flavors of pizza, cheese pizza, such as Beijing, Beijing pepper pizza or cheese pizza London, London pepper pizza.

Ideas 1

Use simple factory pattern to create a variety of simple factory class, such as BJPizzaSimpleFactory, LDPizzaSimpleFactory etc. From this current case, it is also possible, but considering the maintainability of the size of the project, as well as software scalability is not particularly it is good

Ideas 2

Use the factory method pattern

Factory Method pattern presentation

  1. Factory method design pattern: item pizzas instantiated abstract features into an abstract method, embodied in different flavors ordering subclass.
  2. Factory Method pattern: a method of creating an abstract definition of the object is determined to be instantiated by subclasses. Factory method to instantiate the object model is deferred to subclasses.

Factory Method pattern Applications

  1. Pizza new project requirements: Pizza Pizza customers in the points, you can point in different flavors, such as Beijing's cheese pizza, Beijing pepper pizza cheese pizza or London, London pepper pizza

  2. Analysis of graphic ideas
    Here Insert Picture Description

Abstract factory pattern

basic introduction

  1. Abstract factory pattern: Defines the interface used to create a cluster of objects related or dependent relationship, without indicating a specific class
  2. Abstract factory pattern can be simple factory pattern and factory method pattern integration.
  3. From the design perspective, the abstract factory pattern is simple factory pattern of improvement (or called for further abstraction).
  4. The abstract factory into two layers, AbsFactory (abstract factory) and plant specific implementation subclasses. Programmers can use the corresponding sub-class factory to create objects according to the type. This will become a single simple factory class factory cluster, more conducive to the maintenance and expansion of the code.
  5. Class Diagram
    Here Insert Picture Description
Published 93 original articles · won praise 31 · views 30000 +

Guess you like

Origin blog.csdn.net/weixin_43866567/article/details/104614101