Categories
Uncategorized

Create a schema design pattern 1–

I. Introduction

Object-oriented code that is often used to create objects,

Create a design pattern is to create and use objects objects separated.

After obtaining objects easy to use when writing class creational pattern will make.

 

. Twenty-four kinds Creator mode:

 

Example 1. Single Mode

Appreciated: a class has only one object, saving memory, four kinds of methods can be used frequently to create and destroy objects, objects are often heavyweight embodiment with a single mode (such as the data source, session factory) 🙁 every word constructor is a short code) (1) 2 kinds of starving the formula (static variable / static block of code):

Create a static object instance within the class and provide static get method (ie constructor privatization)

Static variables on the implementation class is loaded, so there is no lazy loading,

Static variables may result in wasted memory class loading mechanisms loaded once, to ensure the security thread

  

 

 

 

(2) Formula 3 kinds lazy:

Static objects within the class defined by the static member variables get method for providing object instance (instance constructor filtration method)

get method is not thread-safe lock, locking method is low efficiency, unsafe locking thread inside instance == null

To control a single target, lock only resource is to control the order

 

 

 

. (3) Double-check (good):

Define static member variable objects within the class get static methods provided by the object instances when the object is first created, the class loader and a second air lock object is determined

(4) a static method within the class:

Constructor privatization, create a static object instances within the class and provide the get method

Use static inner class mechanism, safe and efficient, it is recommended to use

 

. (5) enumeration way:

Class enum class to write multi-threaded avoid, prevent deserialization recommended

 

 

2. prototype model

To create many different objects: the situation

Practice: the creator of the interface class class inherits cloneable

Examples: 10 to produce the same sheep

New ten sheep same codes into two parts: a user part and a portion creator

Creator of the sheep class that implements the interface cloneable, through both the attributes of work to clone method, after the new properties will not bother the user, is equal to the expansion of the property is enhanced when copying.

After the user class regenerates the first sheep, the other sheep can call the clone method of the first sheep.

 

3.1 Simple factory pattern

 

Appreciated: the code that creates an abstract class factory object into a package, requiring only a single type of external parameters

 

The code is divided into four parts, client, using the class (class creators direct call), and the creator of the class factory class

Use categories: factory class of polymerization

Factory class: Factory method according to the specific creator class flag, choose to make the corresponding creation class construction

If the class only to change the factory-plus-function method

 

3.2 Abstract Factory pattern

 

Understood that: when the creator classes very often, a simple determination on many factories else / if, according to the creator so that a different type of classification,

A category a factory, factory class together to achieve multi factory interface factory interface is used based polymer

 

Use categories: aggregation only an abstract factory, production method only provides a unified interface to create a class by class

Abstract factory interface: factory method defined

Factory class: Simple factory class and the same type of parameters to factory method

Created by category: Press a standard factory belonging to different classes,

client: New class object factory class and the user

The user class factory object passed

The type identifier to the constructor method

 

 

4 builder mode

Point applies: Method for the case of a similar class.

Appreciated: the builder pattern builder is mainly based polymerization product class and defines the method for building overridden by subclasses, and director based polymer

According to the director class is passed to build sub-class object generation products.

    

 

Example: Factories built house

  

The code is divided into four parts, client, using the class (class direct call function), and product line based builders

New subclass creator, classes and product categories are unchanged

HouseDirector factory, the polymerization HouseBuilder interface / abstract class, comprising its constructor; house method has a structure, stable

House and HouseBuilder products and processes are decoupled

House is a Bean, stable

Abstract various methods HouseBuilder Bean is an interface object

Specific implementation classes which implement the method

Client user (front end) of subclass HouseBuiler

 

 

 

  

Leave a Reply