Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Design pattern opening and factory method pattern

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

Design pattern opening and factory method pattern

Opening remarks

Recently, I am going to implement 23 design patterns in ruby and javascript respectively. Why should I do this?

one。 The company-wide learning design pattern is carried out because of the communication problems caused by the disunity of code style. two。 As a post-study note. three。 I chose ruby and javascript because I mainly use these two languages now. four。 As a suitable point to experience various models.

Design pattern usage

Why learn design patterns? Here I would like to talk about my views on design patterns.

one。 The essence of a design pattern is to shield points of change. (very incisive understanding point, look at the best point of view of the design pattern, thank Dr. Yi) two. No design pattern is omnipotent, so talking about which design pattern is best is useless, not the best, only just right. (based on Article 1) three. The design pattern is not designed to be cool, but to develop a standard noun and standard action to facilitate communication with other OOA programmers.

Complete the nonsense and get into the text.

Factory method (factory method)

First, let's take a look at our UML.

Overview

In the software system, we are often faced with the creation of "some object". Due to the change of requirements, the specific implementation of this object is often faced with drastic changes, but it has a relatively stable interface. How to deal with this change? Provide an encapsulation mechanism to isolate the changes of "this volatile object", so that the "other objects that depend on this object" in the system do not change with the change of requirements? This is the Factory Method mode we are talking about.

Intention

Define an interface for user-created objects, and let subclasses decide which class to instantiate. Factory Method delays the instantiation of a class to its subclasses.

Shielding change point

1. The factory class is no longer responsible for the specific process of the instance, but gives the specific construction process of the change to the subclass to implement. two。 Leave it to the caller to decide which class to instance.

Implementation of ruby class Car def self.factory typeName const_get (typeName). New # query ancestor chain If this static value endendclass BMW < Car def drive p "my it's BMW" endendclass Benz < Car def drive p "my it's Benz" endendjavascript is returned, var car = car | {} car.BMW = function () {console.log ("my it's BMW")} car.Benz = function () {console.log ("my it's Benz")} car.factory= function (name) {var o = {} o.method = car [name] return o} experience

The factory method is suitable for the situation.

1. Specific methods are not defined or cannot be defined and need to be implemented by subclasses. two。 You don't know which subclass to instantiate at design time. 3. Deal with a large number of similar instances

Shortcoming

1. Rely on inheritance chain, too complex to test

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report