第一篇:設計模式-創(chuàng)建型模式的優(yōu)缺點比較
比較幾種創(chuàng)建型模式的優(yōu)缺點,仔細考察這幾種模式的區(qū)別和相關性。
第一類是工廠模式,工廠模式專門負責將大量有共同接口的類實例化。工廠模式可以動態(tài)決定將哪一個類實例化,不必事先知道每次要實例化哪一個類。
工廠模式有三種形態(tài):簡單工廠模式;工廠方法模式;抽象工廠模式是。前兩者是類的創(chuàng)建模式,后者是對象的創(chuàng)建模式。
簡單工廠:
簡單工廠模式是由一個工廠類根據(jù)傳入的參量決定創(chuàng)建出哪一種產(chǎn)品類的實例,涉及工廠角色(Creator)、抽象產(chǎn)品(Product)角色及具體產(chǎn)品(Concrete Product)角色等三個角色。
優(yōu)點:
模式的核心是工廠類,該類中含有必要的判斷邏輯,可以決定在什么時候創(chuàng)建哪一個產(chǎn)品類的實例,客戶端可以免除直接創(chuàng)建產(chǎn)品對象的責任,而僅僅負責“消費”產(chǎn)品。
簡單工廠模式實現(xiàn)了對責任的分割。
缺點:
當產(chǎn)品類有復雜的多層次等級結構時,工廠類只有它自己。
模式中工廠類集中了所有的產(chǎn)品創(chuàng)建邏輯,形成一個無所不知的全能類。
將多個創(chuàng)建邏輯放在一個類中,當產(chǎn)品類有不同接口種類時,工廠類需要判斷在什么時候創(chuàng)建某種產(chǎn)品,使得系統(tǒng)在將來進行功能擴展時較為困難。
該模式采用靜態(tài)方法作為工廠方法,而靜態(tài)方法無法由子類繼承,因此工廠角色無法形成基于繼承的等級結構。
簡單工廠模式只在有限的程度上符合“開-閉”原則。
工廠方法:
定義一個用于創(chuàng)建對象的接口,讓子類決定實例化哪一個類。Factory Method使一個類的實例化延遲到其子類。工廠方法模式是簡單工廠模式的進一步抽象和推廣,其基本思想是定義一個創(chuàng)建產(chǎn)品對象的工廠接口,將實際創(chuàng)建工作推遲到子類中。
優(yōu)點:
多態(tài)性:客戶代碼可以做到與特定應用無關,適用于任何實體類
子類可以重寫新的實現(xiàn),也可以繼承父類的實現(xiàn)。加一層間接性,增加了靈活性。良好的封裝性,代碼結構清晰。擴展性好,在增加產(chǎn)品類的情況下,只需要適當修改具體的工廠類或擴展一個工廠類,就可“擁抱變化”屏蔽產(chǎn)品類。產(chǎn)品類的實現(xiàn)如何變化,調用者都不需要關心,只需關心產(chǎn)品的接口,只要接口保持不變,系統(tǒng)中的上層模塊就不會發(fā)生變化。
典型的解耦框架。高層模塊只需要知道產(chǎn)品的抽象類,其他的實現(xiàn)類都不需要關心,符合迪米特法則,符合依賴倒置原則,符合里氏替換原則。
缺點: 需要Creator和相應的子類作為工廠方法的載體,如果應用模型確實需要creator和子類存在,則很好;否則的話,需要增加一個類層次。
抽象工廠:
提供一個創(chuàng)建一系列相關或相互依賴對象的接口,而無需指定它們具體的類,客戶端不必指定產(chǎn)品的具體類型,創(chuàng)建多個產(chǎn)品族中的產(chǎn)品對象。
優(yōu)點:
分離了具體的類,一個工廠封裝創(chuàng)建產(chǎn)品對象的責任和過程,它將客戶與類的實現(xiàn)分離易于交換產(chǎn)品系列,只需改變具體的工廠就可以使用不同的產(chǎn)品配置。
有利于產(chǎn)品的一致性,當一個系列中的產(chǎn)品對象被設計成一起工作時,一個應用一次只能使用同一個系列中的對象。
缺點:
難以支持新的產(chǎn)品等級結構,支持新的產(chǎn)品等級結構就要擴展抽象工廠接口。
單例模式:
一個類僅有一個實例,自行實例化并向整個系統(tǒng)提供一個訪問它的全局訪問點。確保一個類只能被實例化一次。
優(yōu)點: 跨平臺:使用合適的中間件,可以把singleton模式擴展為跨多個JVM和多個計算機工作。適用于任何類:只需把一個類的構造函數(shù)變成私有的,并且在其中增加相應的靜態(tài)函數(shù)和變量,就可以把這個類變?yōu)閟ingleton。
可以通過派生創(chuàng)建:給定一個類,可以創(chuàng)建它的一個singleton子類。
延遲求值:如果singleton從未使用過,那么就絕不會創(chuàng)建它。
缺點:
摧毀方法未定義: 沒有好的方法去摧毀一個singleton,或者解除其責任。
不能繼承:從singleton派生的類并不是singleton。如果要使其成為singleton,必須要增加所需的靜態(tài)函數(shù)和變量。
效率問題:每次調用instance方法都會執(zhí)行if語句,多余。
不透明性: singleton的使用者知道它們正在使用一個singleton,因為它們必須要調用instance方法。
建造者模式:
將一個復雜對象的構建與它的表示分離,使得同樣的構建過程可以創(chuàng)建不同的表示。建造模式利用一個導演者對象和具體建造者對象一個一個地建造出所有的零件,從而建造出完整的對象。
優(yōu)點: 建造模式的使用使得產(chǎn)品的內部表象可以獨立地變化。使用建造模式可以使客戶端不必知道產(chǎn)品內部組成的細節(jié)。
每一個Builder都相對獨立,而與其他的Builder無關。模式所建造的最終產(chǎn)品更易于控制。
缺點:
建造者模式的“加工工藝”是暴露的,這樣使得建造者模式更加靈活,也使得工藝變得對客戶不透明。
原型模式:
用原型實例指定創(chuàng)建對象的種類,并且通過拷貝這些原型創(chuàng)建新的對象。以一個已有的對象作為原型,通過它來創(chuàng)建新的對象。在增加新的對象的時候,新對象的細節(jié)創(chuàng)建工作由自己來負責,從而使新對象的創(chuàng)建過程與框架隔離開來。
優(yōu)點:
原型模式允許動態(tài)地增加或減少產(chǎn)品類。由于創(chuàng)建產(chǎn)品類實例的方法是產(chǎn)品類內部具有的,因此增加新產(chǎn)品對整個結構沒有影響。
原型模式提供簡化的創(chuàng)建結構。工廠方法常需要有一個與產(chǎn)品類相同的等級結構,而原型模式不需要。
具有給一個應用軟件加載新功能的能力。
產(chǎn)品類不需要非得有任何事先確定的等級結構,因為原型模式適用于任何的等級結構。
缺點:
每一個類必須配備一個克隆方法。
配備克隆方法需要對類的功能進行通盤考慮,這對于全新的類不是很難,但對于已有的類不一定很容易,特別當一個類引用不支持串行化的間接對象,或者引用含有循環(huán)結構的時候。
關系總結: 工廠方法模式是簡單工廠模式的擴展,由于使用了多態(tài)性,工廠方法模式保持了簡單工廠模式的優(yōu)點,而且克服了它的缺點。如果只有一個具體工廠類,工廠方法模式可以改造為簡單工廠模式。抽象工廠經(jīng)常用工廠方法來實現(xiàn)。Prototype:不需要創(chuàng)建Creator的子類,但它們通常需要一個針對Product類的Initialize操作。抽象工廠模式是對象的創(chuàng)建模式,是工廠方法模式的進一步推廣。抽象工廠模式是所有形態(tài)的工廠模式中最為抽象和最具一般性的一種形態(tài)。工廠方法模式針對的是一個產(chǎn)品等級結構;抽象工廠模式需要面對多個產(chǎn)品等級結構。Singleton與其他創(chuàng)建型模式并不矛盾,可以用Singleton來實現(xiàn)其他模式中的對象。包括Abstract Factory、Builder、Prototype等。Builder和Abstract Factory都可以創(chuàng)建復雜的對象。但是Builder模式著重于一步步構造一個復雜的對象,強調的是產(chǎn)品的內部組成,Abstract factory著重于多個系列的產(chǎn)品對象。Builder在最后一步返回產(chǎn)品,而Abstract factory立即返回產(chǎn)品。
Abstractor Factory處于更為具體的角度,Builder處于更宏觀的角度。一個系統(tǒng)可以由一個建造模式和一個抽象工廠模式組成,客戶端通過調用這個建造模式,間接地調用另一個抽象工廠模式的工廠角色。工廠模式返還不同產(chǎn)品族的零件,而建造模式把它們組裝起來。8 Prototype和Abstract factory往往是競爭的關系,但是它們也可以一起使用。抽象工廠可以存儲一個被克隆的原型的集合,并返回產(chǎn)品的對象。
第二篇:模式比較
玫琳凱模式---顧問交友式
1.皮膚分析卡 預約:了解基本信息、提前準備課前:了解皮膚問題,了解需求點課后:滿足需求、強化需求
2.美容課講義夾
講文化、企業(yè):對公司認同、文化認同講皮膚知識:打造專業(yè)形象
規(guī)范護膚步驟:強化需求、專業(yè)信服產(chǎn)品試用: 親身體驗 滿足需求
結束報價:讓情感上接受、心中有數(shù)促銷快報:物超所值 達成銷售
女主人禮物展示:達成延伸和刺激分享
3.售后服務送貨上門,打開試用,規(guī)范護膚步驟:拉近距離針對全面需求給予全套護理流程:
完整的護膚步驟和建議、專業(yè)和規(guī)范講解讓人信服、貼心服務逐步解決顧客需求,有專業(yè)的解決方案
了解喜好、邀約相關活動、了解他周圍的人,讓他成為女主人、邀約更多人、用她的分享帶動銷售
4.事業(yè)機會手冊:針對不同需求有不同版本的事業(yè)機會分享,有針對性的感情投入,既能成為優(yōu)質的主顧,也能有機會成為事業(yè)機會的合作者。
保險改進模式-------專業(yè)理財顧問
1.預約:了解基本財務狀況
授前:了解財務困惑、了解需求點授中:強化需求、滿足需求 2.理財課堂 講輕松氛圍講文化、企業(yè)
講理財重要、基本理財工具運用講家庭理財組合(4321)講保險意義與功用
3.售后服務強化理財理念,投資組合意識給予資產(chǎn)分類管理方案對每種方案的注意事項
對家庭成員完整的保險計劃
理賠會出現(xiàn)的問題,給予對應的解決方案
保險個人銷售行為-----------專業(yè)個人理財規(guī)劃師 從賣保險-------銷售理財計劃
對立的關系---------依賴信任的關系根本是解決問題的能力、讓他成為你滿意的顧客讓他的分享給你帶來源源不斷地利潤,所以情感投入,全身心的售前、售后專業(yè)全面的服務才是贏得客戶心的唯一途徑,這樣保險的營銷就 不成問題。
關鍵在于專業(yè)
第三篇:英文寫作---比較型說明文的模式
比較型說明文的模式
第一步:審題
1.A Major Advantage or Disadvantage of Playing Computer Games
2.The Advantages and Disadvantages of Air Travel
3.Some people believe that a college or university education should be available toall students;others believe that higher education should be available only to goodstudents.What’s your opinion?
第二步:構思
模式
I(該模式用于比較兩個事物,作者的傾向比較明顯時)
第一段, 引言句(現(xiàn)象或事實陳述法)引出要比較的兩個事物
(A 或B)用一句話表明自己的觀點(如:傾向A)
第二段, 段落主題句承接首段中的觀點,并對本段內容加以限定
比較之一:
指出A事物的優(yōu)點之一, 用拓展句加以說明.通過轉折指出B事物的缺點, 用拓展句加以說明.比較之二:
指出A事物的優(yōu)點之二, 用拓展句加以說明.通過轉折指出B事物的缺點,用拓展句加以說明.(A1B1A2B2A3B3 point-by-point pattern)
第三段, 得出結論,注意首尾觀點一致.Television As a Better Source of NewsAmong the three mass media—television, newspaper and radio, I regard television as a better source.My statement that television is a better source of news is based on the following two reasons.The first reason is that television can report news more quickly.We can get the news at the same time when something is happening or immediately after something happened.While newspapers can only offer the news that happened a day or two days ago.The second reason is that television can report news more vividly and impressively.With sounds and pictures on TV, we can be deeply impressed by the news as if we were personally on the spot.But when we try to get news from the newspaper, we have to concentrate our mind on the lines and use our imaginations to for a concrete image, which is always tiresome and time-consuming.According to the above mentioned reasons;we can easily draw a conclusion that television is really a better source of news.模式II
如果作文要求比較兩個事物的不同之處,宜用下列結構:
第一段, 引言句(一般用現(xiàn)象陳述法,提問)引出要比較的兩個;主題句指出的兩個事物存在著明顯的差異或兩個事物的不同特點.第二段, 直接評述A事物的主要特點,用拓展句加以說明(舉例,一般闡述);指出B事物的主要特點,用拓展句加以具體明.(A1A2A3B1B2B3
subject-by-subject pattern)
第三段, 通過比較得出傾向性的結論, 表明作者的態(tài)度.Where to Live–in the City or the Country
Many people appreciate the conveniences of the city.The city has better transportation service and health care.City dwellers can easily enjoy themselves in restaurants, department stores and concert halls.They are well-informed about what is going on at home and abroad, and have the access to better education, better jobs and more opportunities for business.But country life is also attractive.With the fresh air, the green trees and the singing birds, country people are close to nature and live a quiet life.They can make friends with simple and honest men or wander about leisurely without any pressure upon their mind.Both the country and the city, however, have their own disadvantages.In the country, educational facilities, medical services and transportation systems are still not fully developed.And the city also has many problems, such as heavy traffic, air pollution, great noise and poor housing conditions.As far as I’m concerned, I hated the terrible dirt and noise in the city.So, given the chance, I would prefer to live peacefully in the country.The Main Difference Between Exam-oriented Education and Quality-oriented Education
Education in China used to be exam-oriented one.But with the development of education reform, quality-oriented education is stressed again and again.So there rises a question: what is the main difference between these two?
Accordingto my understanding, exam-oriented education focuses on students’ ability of passing examination of different levels.This education is to test the students command of knowledge they
have obtained from the textbooks.So examination results are the only criterion which is used to evaluate the students whether they are “excellent” ones or “bad” ones.However, quality-oriented
education focuses on students’ various qualities, including academic moral quality, cultural quality, most important, their individual personality and potentialities.The main purpose of this education is to bring students’ practical potentialities into full play.Quality-oriented education can teach students how to face difficulties and solve problems independently.From the above comparison between exam-oriented education and quality-oriented education, we find that the latter one is superior.So it is time for our universities and colleges to bring quality-oriented education into students’ daily life.比較一個事物的兩個方面的時候也可以用這種模式:
第一段, 引言句引出要比較的兩個方面(advantage & disadvantage)
第二段, 直接評述advantage,用拓展句加以說明(舉例,一般闡述);然后再指出disadvantage,用拓展句加以具體明.(A1A2A3B1B2B3 subject-by-subject pattern)
第三段, 通過比較得出傾向性的結論, 表明優(yōu)劣或作者態(tài)度.The Advantages and Disadvantages of Air Travel
In the last fifty years, traveling by air has become the most popular form of long-distance journeys.However, there are both positive and negative associated with it.The first advantage of air travel is that it is quick.For instance, we can fly from Beijing to Guangzhou in less than three hours.The second advantage of traveling by air is that it is convenient.After we have checked in, all we have to do is to get on and sit down.The friendly air stewardess will look after our every need.The third advantage of traveling by air is that it is now relatively cheep, especially for long distance travel.We can fly from Beijing to Guangzhou for only 1200 Yuan.But on the other hand, traveling by air has its disadvantages.Perhaps the most obvious is the element of danger involved, such as hijackings and crashes.Traveling by air also makes it impossible for us to see many scenic spot on the way to our destination.In addition to the above mentioned, a long distance air travel may make us very tired, both mentally and physically.Many precious days of holiday may be wasted recovering from airsickness.To conclude, although air travel involves inconvenience and slight risk, I would say that the advantages mentioned above weigh much more than the disadvantages.Exercise:
With the development of computer, the Internet has become widely used.It is just like information net that has covered the world.Some people worry that it provides too much for people.Write a composition of about 200 words to state your view on this issue.Does the Internet Provide Too Much for People
When a great deal of information is exchanged on the Internet almost every second, some people worry that it provides too much for people.To know it clearly, we should look at both sides of the Internet.On one hand, the Internet shows us a closer and wider world than ever before.Just staying at home, we can get all kinds of useful information in any scientific research field whenever we need.Through the Internet, more and more chances are provided to improve our work, enrich our knowledge and help our country to keep up with the others.But on the other hand, the Internet also brings us many troubles.Useless information and games send to us unavoidably.From this angle, we have to say that those people’s complaints are understandable.Generally speaking, the Internet has played an important role in modern life.It is like a double-edged sword.We should make the best use of one edge, and try not to be hurt by the other one.模式III
(該模式用于比較兩個事物時作者的傾向比較明顯)
第一段, 引言句引出要比較的兩個事物;主題句概括地指出其中一個事物的優(yōu)點和缺點。
(傾向于A)
第二段, 只評述A事物的優(yōu)點,用拓展句加以說明(舉例,一般闡述);指出A事物的缺點,用拓展句加以具體明.第三段, 通過比較得出傾向性的結論, 表明作者的態(tài)度.這樣類似于比較一個事物的兩個方面。
Sharing a Room with a Stranger or Living by Yourself
Nowadays, more and more college students like to live off campus.Some prefer to share a flat with somebody else to divide the rent between them and others prefer to have a flat to themselves.There are always two aspects to one situation.Sharing a flat with somebody else has its advantages and disadvantages.Study requires peace and quietness.A stranger who shares a room with you may disturb you.Your thoughts can be interrupted by his sudden return, or by asking for a light, or merely by his cough or sneeze.At night, when you want to sleep, he may be tossing in bed, making enough noise to keep you awake.If the stranger is bad-tempered, he may quarrel with you, and yet, you must have patience.However, there are many positive aspects of sharing a room with a stranger.First, with another person in the room, you will not feel lonely although far way from you home.Second, you can always help each other.Whatever difficulty you are in, you can get a helping hand.If the person you live with is a senior student, you can even get advice from him on your work and study.Third, living with a stranger offers you a chance of learning to get along with people.Since you have to live with a stranger for quite a long period, you will do your best to make friends with him.If you are successful in gaining his trust, you may also be successful in the more complicated and competitive society.Therefore, I think, sharing a room with a stranger is a good arrangement of living for all the disadvantages.I prefer to share a flat with someone else rather than live alone.Where to Live– in the City or in the Country
Each year thousands of people rush into big cities.Some come for education, some for shopping and some on business.But most of them, attracted by the job opportunities and modern life, come to stay.People enjoy city life chiefly for its conveniences.To begin with, the city provides fast transport, with roads and buses leading to every corner.Then, there are department stores and shops all over the city, where you can buy all kinds of goods you want.Besides, hospitals and other services are
all within easy reach.Indeed, whatever your need is, you can easily get what your want.However, ig cities are confronted with many problems.One is the housing problem.For examples, it is not uncommon for two or even three generations to share an apartment.Another problem is pollution.The air is not clean and the environment is noisy.In short, the city is only a place for business, to be visited occasionally.It is not an
ideal place for permanent living.模式IV
第一段, 引言句引出要比較的兩個事物;主題句鮮明地表明作者的態(tài)度—傾向其中一個(如:
A)。
第二段, 只評述A事物的優(yōu)點,用拓展句加以說明(舉例,一般闡述)
第三段, 總結A的優(yōu)點,再次表明態(tài)度,和第一段里態(tài)度一致。
這種方式就將比較型的說明文變成了闡述型的說明文。
Where to Live– in the City or in the Country
There are undeniable advantages to both life in a big city and in a small town.However, despite the advantages of small town life, I prefer to live in a big city for several reasons.First, life in the big city is more convenient.More goods are available and stores stay open later.Also, there is better public transportation, so it is easier to get around.I can find almost anything I want easily in the city.Second, there are more ways to spend leisure time in the city.There are many places I can go to meet friends and have fun.Finally, and most importantly, the city offers more educational and job opportunities.The city often attracts the best teachers and the best companies.There is also a wider choice of jobs so it is easier to move up the career ladder.For all these reasons, I prefer to live in the city.Although I sometimes miss the fresh air and quiet life of a small town, nothing can make up for the opportunities that the city offers.Exercise:
Some people believe that a college or university education should be available to all students;others believe that higher education should be available only to good students.What’s your opinion?
Opening University Doors to All Students
Some people believe that university education should be available to all students.Others believe that higher education should be limited to only good students.I think universities should open their doors to all students.Those who argue for the availability of university education to good students think that having only good students study in university would save the government’s fund.However, these people neglect one fact that most school graduates are too young to work.These young people, especially the so-called bad students, since they are refused by colleges and universities, have nothing to do and would easily form gangs.Those who argue for the availability of higher education to all students believe that every student should have the equal right to education.Besides, when mass students have university education year after year, the education lever of the whole society will finally get promoted, resulting in a better quality of the nation.Apparently, opening university doors to all students benefits the society and
the whole nation;therefore, it is a better choice for us to take.
第四篇:JAVA設計模式之創(chuàng)建模式
設計模式之Builder
Builder模式定義: 將一個復雜對象的構建與它的表示分離,使得同樣的構建過程可以創(chuàng)建不同的表示.Builder模式是一步一步創(chuàng)建一個復雜的對象,它允許用戶可以只通過指定復雜對象的類型和內容就可以構建它們.用戶不知道內部的具體構建細節(jié).Builder模式是非常類似抽象工廠模式,細微的區(qū)別大概只有在反復使用中才能體會到.為何使用?
是為了將構建復雜對象的過程和它的部件解耦.注意: 是解耦過程和部件.因為一個復雜的對象,不但有很多大量組成部分,如汽車,有很多部件:車輪 方向盤 發(fā)動機還有各種小零件等等,部件很多,但遠不止這些,如何將這些部件裝配成一輛汽車,這個裝配過程也很復雜(需要很好的組裝技術),Builder模式就是為了將部件和組裝過程分開.如何使用?
首先假設一個復雜對象是由多個部件組成的,Builder模式是把復雜對象的創(chuàng)建和部件的創(chuàng)建分別開來,分別用Builder類和Director類來表示.首先,需要一個接口,它定義如何創(chuàng)建復雜對象的各個部件: public interface Builder {
//創(chuàng)建部件A 比如創(chuàng)建汽車車輪
void buildPartA();
//創(chuàng)建部件B 比如創(chuàng)建汽車方向盤
void buildPartB();
//創(chuàng)建部件C 比如創(chuàng)建汽車發(fā)動機
void buildPartC();
//返回最后組裝成品結果(返回最后裝配好的汽車)
//成品的組裝過程不在這里進行,而是轉移到下面的Director類中進行.//從而實現(xiàn)了解耦過程和部件
Product getResult();} 用Director構建最后的復雜對象,而在上面Builder接口中封裝的是如何創(chuàng)建一個個部件(復雜對象是由這些部件組成的),也就是說Director的內容是如何將部件最后組裝成成品: public class Director {
private Builder builder;
public Director(Builder builder){
this.builder = builder;} // 將部件partA partB partC最后組成復雜對象 //這里是將車輪 方向盤和發(fā)動機組裝成汽車的過程 public void construct(){
builder.buildPartA();
builder.buildPartB();
builder.buildPartC();
} } Builder的具體實現(xiàn)ConcreteBuilder: 通過具體完成接口Builder來構建或裝配產(chǎn)品的部件;定義并明確它所要創(chuàng)建的是什么具體東西;提供一個可以重新獲取產(chǎn)品的接口: public class ConcreteBuilder implements Builder {
Part partA, partB, partC;public void buildPartA(){
//這里是具體如何構建partA的代碼
};public void buildPartB(){
//這里是具體如何構建partB的代碼 };public void buildPartC(){
//這里是具體如何構建partB的代碼 };public Product getResult(){
//返回最后組裝成品結果 };} 復雜對象:產(chǎn)品Product: public interface Product { } 復雜對象的部件: public interface Part { }
我們看看如何調用Builder模式: ConcreteBuilder builder = new ConcreteBuilder();Director director = new Director(builder);
director.construct();Product product = builder.getResult();Builder模式的應用
在Java實際使用中,我們經(jīng)常用到“池”(Pool)的概念,當資源提供者無法提供足夠的資源,并且這些資源需要被很多用戶反復共享時,就需要使用池.“池”實際是一段內存,當池中有一些復雜的資源的“斷肢”(比如數(shù)據(jù)庫的連接池,也許有時一個連接會中斷),如果循環(huán)再利用這些“斷肢”,將提高內存使用效率,提高池的性能.修改Builder模式中Director類使之能診斷“斷肢”斷在哪個部件上,再修復這個部件.設計模式之Factory
定義:提供創(chuàng)建對象的接口.為何使用?
工廠模式是我們最常用的模式了,著名的Jive論壇 ,就大量使用了工廠模式,工廠模式在Java程序系統(tǒng)可以說是隨處可見。
為什么工廠模式是如此常用?因為工廠模式就相當于創(chuàng)建實例對象的new,我們經(jīng)常要根據(jù)類Class生成實例對象,如A a=new A()工廠模式也是用來創(chuàng)建實例對象的,所以以后new時就要多個心眼,是否可以考慮實用工廠模式,雖然這樣做,可能多做一些工作,但會給你系統(tǒng)帶來更大的可擴展性和盡量少的修改量。我們以類Sample為例,如果我們要創(chuàng)建Sample的實例對象: Sample sample=new Sample();可是,實際情況是,通常我們都要在創(chuàng)建sample實例時做點初始化的工作,比如賦值 查詢數(shù)據(jù)庫等。
首先,我們想到的是,可以使用Sample的構造函數(shù),這樣生成實例就寫成: Sample sample=new Sample(參數(shù));但是,如果創(chuàng)建sample實例時所做的初始化工作不是象賦值這樣簡單的事,可能是很長一段代碼,如果也寫入構造函數(shù)中,那你的代碼很難看了(就需要Refactor重整)。為什么說代碼很難看,初學者可能沒有這種感覺,我們分析如下,初始化工作如果是很長一段代碼,說明要做的工作很多,將很多工作裝入一個方法中,相當于將很多雞蛋放在一個籃子里,是很危險的,這也是有背于Java面向對象的原則,面向對象的封裝(Encapsulation)和分派(Delegation)告訴我們,盡量將長的代碼分派“切割”成每段,將每段再“封裝”起來(減少段和段之間偶合聯(lián)系性),這樣,就會將風險分散,以后如果需要修改,只要更改每段,不會再發(fā)生牽一動百的事情。
在本例中,首先,我們需要將創(chuàng)建實例的工作與使用實例的工作分開, 也就是說,讓創(chuàng)建實例所需要的大量初始化工作從Sample的構造函數(shù)中分離出去。
這時我們就需要Factory工廠模式來生成對象了,不能再用上面簡單new Sample(參數(shù))。還有,如果Sample有個繼承如MySample, 按照面向接口編程,我們需要將Sample抽象成一個接口.現(xiàn)在Sample是接口,有兩個子類MySample 和HisSample.我們要實例化他們時,如下: Sample mysample=new MySample();Sample hissample=new HisSample();隨著項目的深入,Sample可能還會“生出很多兒子出來”, 那么我們要對這些兒子一個個實例化,更糟糕的是,可能還要對以前的代碼進行修改:加入后來生出兒子的實例.這在傳統(tǒng)程序中是無法避免的.但如果你一開始就有意識使用了工廠模式,這些麻煩就沒有了.工廠方法
你會建立一個專門生產(chǎn)Sample實例的工廠: public class Factory{
public static Sample creator(int which){
//getClass 產(chǎn)生Sample 一般可使用動態(tài)類裝載裝入類。if(which==1)
return new SampleA();else if(which==2)
return new SampleB();
} } 那么在你的程序中,如果要實例化Sample時.就使用 Sample sampleA=Factory.creator(1);這樣,在整個就不涉及到Sample的具體子類,達到封裝效果,也就減少錯誤修改的機會,這個原理可以用很通俗的話來比喻:就是具體事情做得越多,越容易范錯誤.這每個做過具體工作的人都深有體會,相反,官做得越高,說出的話越抽象越籠統(tǒng),范錯誤可能性就越少.好象我們從編程序中也能悟出人生道理?呵呵.使用工廠方法 要注意幾個角色,首先你要定義產(chǎn)品接口,如上面的Sample,產(chǎn)品接口下有Sample接口的實現(xiàn)類,如SampleA,其次要有一個factory類,用來生成產(chǎn)品Sample,如下圖,最右邊是生產(chǎn)的對象Sample:
進一步稍微復雜一點,就是在工廠類上進行拓展,工廠類也有繼承它的實現(xiàn)類concreteFactory了。抽象工廠
工廠模式中有: 工廠方法(Factory Method)抽象工廠(Abstract Factory).這兩個模式區(qū)別在于需要創(chuàng)建對象的復雜程度上。如果我們創(chuàng)建對象的方法變得復雜了,如上面工廠方法中是創(chuàng)建一個對象Sample,如果我們還有新的產(chǎn)品接口Sample2.這里假設:Sample有兩個concrete類SampleA和SamleB,而Sample2也有兩個concrete類Sample2A和SampleB2 那么,我們就將上例中Factory變成抽象類,將共同部分封裝在抽象類中,不同部分使用子類實現(xiàn),下面就是將上例中的Factory拓展成抽象工廠: public abstract class Factory{
public abstract Sample creator();
public abstract Sample2 creator(String name);} public class SimpleFactory extends Factory{
public Sample creator(){
.........return new SampleA } public Sample2 creator(String name){
.........return new Sample2A } } public class BombFactory extends Factory{
public Sample creator(){
......return new SampleB } public Sample2 creator(String name){
......return new Sample2B } }
從上面看到兩個工廠各自生產(chǎn)出一套Sample和Sample2,也許你會疑問,為什么我不可以使用兩個工廠方法來分別生產(chǎn)Sample和Sample2? 抽象工廠還有另外一個關鍵要點,是因為 SimpleFactory內,生產(chǎn)Sample和生產(chǎn)Sample2的方法之間有一定聯(lián)系,所以才要將這兩個方法捆綁在一個類中,這個工廠類有其本身特征,也許制造過程是統(tǒng)一的,比如:制造工藝比較簡單,所以名稱叫SimpleFactory。在實際應用中,工廠方法用得比較多一些,而且是和動態(tài)類裝入器組合在一起應用,舉例
我們以Jive的ForumFactory為例,這個例子在前面的Singleton模式中我們討論過,現(xiàn)在再討論其工廠模式: public abstract class ForumFactory {
private static Object initLock = new Object();
private static String className = “com.jivesoftware.forum.database.DbForumFactory”;
private static ForumFactory factory = null;
public static ForumFactory getInstance(Authorization authorization){
//If no valid authorization passed in, return null.if(authorization == null){
return null;
}
//以下使用了Singleton 單態(tài)模式
if(factory == null){
synchronized(initLock){
if(factory == null){
......}
}
} try {
//動態(tài)轉載類
Class c = Class.forName(className);
factory =(ForumFactory)c.newInstance();} catch(Exception e){
return null;}
//Now, 返回 proxy.用來限制授權對forum的訪問
return new ForumFactoryProxy(authorization, factory,factory.getPermissions(authorization));
}
//真正創(chuàng)建forum的方法由繼承forumfactory的子類去完成.public abstract Forum createForum(String name, String description)
throws UnauthorizedException, ForumAlreadyExistsException;
....}
因為現(xiàn)在的Jive是通過數(shù)據(jù)庫系統(tǒng)存放論壇帖子等內容數(shù)據(jù),如果希望更改為通過文件系統(tǒng)實現(xiàn),這個工廠方法ForumFactory就提供了提供動態(tài)接口: private static String className = “com.jivesoftware.forum.database.DbForumFactory”;你可以使用自己開發(fā)的創(chuàng)建forum的方法代替com.jivesoftware.forum.database.DbForumFactory就可以.在上面的一段代碼中一共用了三種模式,除了工廠模式外,還有Singleton單態(tài)模式,以及proxy模式,proxy模式主要用來授權用戶對forum的訪問,因為訪問forum有兩種人:一個是注冊用戶 一個是游客guest,那么那么相應的權限就不一樣,而且這個權限是貫穿整個系統(tǒng)的,因此建立一個proxy,類似網(wǎng)關的概念,可以很好的達到這個效果.看看Java寵物店中的CatalogDAOFactory: public class CatalogDAOFactory {
/**
* 本方法制定一個特別的子類來實現(xiàn)DAO模式。
* 具體子類定義是在J2EE的部署描述器中。
*/
public static CatalogDAO getDAO()throws CatalogDAOSysException {
CatalogDAO catDao = null;
try {
InitialContext ic = new InitialContext();//動態(tài)裝入CATALOG_DAO_CLASS //可以定義自己的CATALOG_DAO_CLASS,從而在無需變更太多代碼 //的前提下,完成系統(tǒng)的巨大變更。
String className =(String)ic.lookup(JNDINames.CATALOG_DAO_CLASS);
catDao =(CatalogDAO)Class.forName(className).newInstance();
} catch(NamingException ne){
throw new CatalogDAOSysException(“
CatalogDAOFactory.getDAO: NamingException while
getting DAO type : n” + ne.getMessage());
} catch(Exception se){
throw new CatalogDAOSysException(“
CatalogDAOFactory.getDAO: Exception while getting
DAO type : n” + se.getMessage());
}
return catDao;
} } CatalogDAOFactory是典型的工廠方法,catDao是通過動態(tài)類裝入器className獲得CatalogDAOFactory具體實現(xiàn)子類,這個實現(xiàn)子類在Java寵物店是用來操作catalog數(shù)據(jù)庫,用戶可以根據(jù)數(shù)據(jù)庫的類型不同,定制自己的具體實現(xiàn)子類,將自己的子類名給與CATALOG_DAO_CLASS變量就可以。
由此可見,工廠方法確實為系統(tǒng)結構提供了非常靈活強大的動態(tài)擴展機制,只要我們更換一下具體的工廠方法,系統(tǒng)其他地方無需一點變換,就有可能將系統(tǒng)功能進行改頭換面的變化。
設計模式之Prototype(原型)
定義: 用原型實例指定創(chuàng)建對象的種類,并且通過拷貝這些原型創(chuàng)建新的對象.Prototype模式允許一個對象再創(chuàng)建另外一個可定制的對象,根本無需知道任何如何創(chuàng)建的細節(jié),工作原理是:通過將一個原型對象傳給那個要發(fā)動創(chuàng)建的對象,這個要發(fā)動創(chuàng)建的對象通過請求原型對象拷貝它們自己來實施創(chuàng)建。如何使用? 因為Java中的提供clone()方法來實現(xiàn)對象的克隆(具體了解clone()按這里),所以Prototype模式實現(xiàn)一下子變得很簡單.以勺子為例:
public abstract class AbstractSpoon implements Cloneable {
String spoonName;
public void setSpoonName(String spoonName){this.spoonName = spoonName;}
public String getSpoonName(){return this.spoonName;}
public Object clone()
{
Object object = null;
try {
object = super.clone();
} catch(CloneNotSupportedException exception){
System.err.println(“AbstractSpoon is not Cloneable”);
}
return object;
} } 有兩個具體實現(xiàn)(ConcretePrototype): public class SoupSpoon extends AbstractSpoon {
public SoupSpoon()
{
setSpoonName(“Soup Spoon”);
} } public class SaladSpoon extends AbstractSpoon {
public SaladSpoon()
{
setSpoonName(“Salad Spoon”);
} } 調用Prototype模式很簡單: AbstractSpoon spoon = new SoupSpoon();AbstractSpoon spoon = new SaladSpoon();當然也可以結合工廠模式來創(chuàng)建AbstractSpoon實例。
在Java中Prototype模式變成clone()方法的使用,由于Java的純潔的面向對象特性,使得在Java中使用設計模式變得很自然,兩者已經(jīng)幾乎是渾然一體了。這反映在很多模式上,如Interator遍歷模式。
設計模式之Singleton(單態(tài))
定義: Singleton模式主要作用是保證在Java應用程序中,一個類Class只有一個實例存在。在很多操作中,比如建立目錄 數(shù)據(jù)庫連接都需要這樣的單線程操作。
還有, singleton能夠被狀態(tài)化;這樣,多個單態(tài)類在一起就可以作為一個狀態(tài)倉庫一樣向外提供服務,比如,你要論壇中的帖子計數(shù)器,每次瀏覽一次需要計數(shù),單態(tài)類能否保持住這個計數(shù),并且能synchronize的安全自動加1,如果你要把這個數(shù)字永久保存到數(shù)據(jù)庫,你可以在不修改單態(tài)接口的情況下方便的做到。
另外方面,Singleton也能夠被無狀態(tài)化。提供工具性質的功能,Singleton模式就為我們提供了這樣實現(xiàn)的可能。使用Singleton的好處還在于可以節(jié)省內存,因為它限制了實例的個數(shù),有利于Java垃圾回收(garbage collection)。
我們常??吹焦S模式中類裝入器(class loader)中也用Singleton模式實現(xiàn)的,因為被裝入的類實際也屬于資源。如何使用?
一般Singleton模式通常有幾種形式: public class Singleton {
private Singleton(){}
//在自己內部定義自己一個實例,是不是很奇怪?
//注意這是private 只供內部調用
private static Singleton instance = new Singleton();
}
第二種形式: public class Singleton {
private static Singleton instance = null;public static synchronized Singleton getInstance(){
//這個方法比上面有所改進,不用每次都進行生成對象,只是第一次
//使用時生成實例,提高了效率!if(instance==null)
instance=new Singleton();return instance;} //這里提供了一個供外部訪問本class的靜態(tài)方法,可以直接訪問
public static Singleton getInstance(){
return instance;
} }
使用Singleton.getInstance()可以訪問單態(tài)類。
上面第二中形式是lazy initialization,也就是說第一次調用時初始Singleton,以后就不用再生成了。
注意到lazy initialization形式中的synchronized,這個synchronized很重要,如果沒有synchronized,那么使用getInstance()是有可能得到多個Singleton實例。關于lazy initialization的Singleton有很多涉及double-checked locking(DCL)的討論,有興趣者進一步研究。
一般認為第一種形式要更加安全些。使用Singleton注意事項:
有時在某些情況下,使用Singleton并不能達到Singleton的目的,如有多個Singleton對象同時被不同的類裝入器裝載;在EJB這樣的分布式系統(tǒng)中使用也要注意這種情況,因為EJB是跨服務器,跨JVM的。
我們以SUN公司的寵物店源碼(Pet Store 1.3.1)的ServiceLocator為例稍微分析一下:
在Pet Store中ServiceLocator有兩種,一個是EJB目錄下;一個是WEB目錄下,我們檢查這兩個ServiceLocator會發(fā)現(xiàn)內容差不多,都是提供EJB的查詢定位服務,可是為什么要分開呢?仔細研究對這兩種ServiceLocator才發(fā)現(xiàn)區(qū)別:在WEB中的ServiceLocator的采取Singleton模式,ServiceLocator屬于資源定位,理所當然應該使用Singleton模式。但是在EJB中,Singleton模式已經(jīng)失去作用,所以ServiceLocator才分成兩種,一種面向WEB服務的,一種是面向EJB服務的。
Singleton模式看起來簡單,使用方法也很方便,但是真正用好,是非常不容易,需要對Java的類 線程 內存等概念有相當?shù)牧私狻_M一步深入可參考:
Double-checked locking and the Singleton pattern When is a singleton not a singleton?
第五篇:審計模式的比較
審計模式的比較 審計模式是審計導向性的目的、范圍和方法等要素的組合,它規(guī)定了審計應從何處著手、如何著手以及何時著手等方面。按照歷史發(fā)展的順序,審計模式經(jīng)歷了賬項基礎審計模式、制度基礎審計模式、風險導向審計模式三個階段。由于各階段的導向性目標不同,所采用的審計程序和方法也有所不同,但是三個階段相互之間并非孤立,而是存在承前啟后的關系。
一、賬項基礎審計模式
特點:(1)以會計事項為主線,在審查會計事項的基礎上開展審計工作,工作量較大,適應早期審計的需要。
(2)主要目標是查錯糾弊,但不審查內部牽制制度,不對內部牽制制度發(fā)表評價意見。
(3)主要采用查賬的方法。它要審查被審計單位在審計期間內關于會計事項的會計記錄及其有關憑證,驗算其會計金額,核對轉賬事項,以證實賬簿和報表數(shù)字的正確性。
(4)沒有區(qū)分階段、步驟,沒有固定的審計程序,也不重視著手審計前的準備工作,只要求了解被審計單位的概況和搜集試算表、總賬、明細賬等會計資料,然后直接審查會計事項。
(5)此階段形成了豐富的審計方法和技術,并沿用至今,主要包括:①書面資料審計方法②財產(chǎn)物資審計方法③分析性復核方法。
二、制度基礎審計模式
特點:(1)主要目標是確定會計報表的公允性,盡管也揭露錯誤和舞弊,但只是因為錯誤和舞弊會影響財務報表的公允性。
(2)在評價內部控制的基礎上確定審計的重點和范圍,并非傳統(tǒng)的詳細審計,可以在保證審計結論具有一定可靠水平的前提下提高審計工作效率。
(3)形成了標準化的審計程序,一般包括計劃、實施、完成三個階段,重視審計計劃階段,并把內部控制的評審作為關鍵步驟,審計范圍和重點都比較明確。
(4)除采用賬項基礎審計的技術方法之外,還引入了對內部控制的符合性測試和審計抽樣。
三、風險導向審計模式
風險導向審計模式經(jīng)歷了從傳統(tǒng)風險導向審計向現(xiàn)代風險導向審計的轉變。
現(xiàn)代風險導向審計模式與傳統(tǒng)風險導向審計模式的主要區(qū)別:
(1)從審計風險模型來看,傳統(tǒng)風險導向審計的風險模型為
審計風險=固有風險*控制風險*檢查風險
而現(xiàn)代風險導向審計的風險模型為
審計風險=重大錯報風險*檢查風險
(2)從審計思路來看,傳統(tǒng)風險導向審計模式一般采用自下而上的審計方式,以企業(yè)的內部控制為審計起點,以內部控制風險評估為風險評估的重心;而現(xiàn)代風險導向審計模式主要從企業(yè)內外部環(huán)境、經(jīng)營風險分析入手,以此來發(fā)現(xiàn)可能出現(xiàn)重大錯報的領域及評估被審計單位的會計政策和會計估計的適當性,加強了對固有風險的評估,同時仍未完全放棄自下而上的審計方式。
(3)從審計程序來看,傳統(tǒng)風險導向審計模式在實務中存在重程序、輕風險的傾向,而
現(xiàn)代風險導向審計模式則針對不同的客戶、不同的風險領域采取不同的審計程序,對分析性程序也更為重視。
現(xiàn)代風險導向審計模式更能適應現(xiàn)代審計環(huán)境的變化,它要求注冊會計師一重大錯報風險的識別、評估和應對為審計工作的主線,以提高審計效率和效果。