SlideShare une entreprise Scribd logo
1  sur  27
可視性指定子と設計の話
@r_ohki
1
2
public に簡単に write を書くのをやめよう!
目次
• 本日の主張
• 自己紹介
• 発端
• おさらい
• 実際のコード
• どこからはじめよう?
• その次
3
自己紹介
• 氏名: 大城 亮
• @r_ohki
•
• ROki1988
• Windows のクライアントを開発してます
• 最近、他の言語にも手を出してます
4
前回の勉強会にて…
私「property に write 書くの、すっごい慎重になりません…?」
「……」
「ふつうちゃう?」
ふつうちゃう!!
5
6
本日の目標
property に write を書くのを躊躇してもらう
可視性識別子のおさらい
• 厳しいほうからゆるくなると
• strict private
• private
• strict protected
• protected
• public
• class だけでなく record にもある
7
strict private
• 一番厳しい
• 同じクラスのインスタンスからのみ見える
UnitA.pas
TClassA = class
strict private
Fid: string;
end;
TClassB = class
end;
UnitB.pas
TClassC = class(TClassA)
end;
TClassD = class
end;
8
UnitB.pas
TClassC = class(TClassA)
end;
TClassD = class
end;
private
• まだ厳しい
• 同じユニットからのみ見える
UnitA.pas
TClassA = class
private
Fid: string;
end;
TClassB = class
end;
9
UnitB.pas
TClassC = class(TClassA)
end;
TClassD = class
end;
strict protected
• まだ厳しい
• 継承関係にあるクラスから見える
UnitA.pas
TClassA = class
strict protected
Fid: string;
end;
TClassB = class
end;
10
protected
• だいぶゆるい
• 同じユニットと継承関係にあるクラスから見える
UnitA.pas
TClassA = class
protected
Fid: string;
end;
TClassB = class
end;
UnitB.pas
TClassC = class(TClassA)
end;
TClassD = class
end;
11
public
• やわらか
• 誰でも変更できる
UnitA.pas
TClassA = class
public
Fid: string;
end;
TClassB = class
end;
UnitB.pas
TClassC = class(TClassA)
end;
TClassD = class
end;
12
Q. public で何がいけない!!
• A. 大事なデータが書き換えられていないことを
人の手で担保しなきゃあいけなくなります
13
[復習]public
• やわらか
• 誰でも変更できる
UnitA.pas
TClassA = class
public
Fid: string;
end;
TClassB = class
end;
UnitB.pas
TClassC = class(TClassA)
end;
TClassD = class
end;
14
ID なんて大事なものを!
• たとえば)
• 会員番号とか
• 弊社であれば、ログの日付とか
• 何かあれば全チェック!?
• この確認のためにいろんな処理を組むとか不毛すぎる…
• ので、これを絞る!
15
[復習] strict private
• Fid を変更できるのは、TClassA だけ
• なので、TClassA の処理のみを追えばよくなる
UnitA.pas
TClassA = class
strict private
Fid: string;
end;
TClassB = class
end;
UnitB.pas
TClassC = class(TClassA)
end;
TClassD = class
end;
16
17
実際のコード
ROki1988/PerfSrv
public だけ取り出してみる
type
TCollectedMetric = class
public
property CollectedDateTime: TDateTime read FCollectedDateTime;
property CounterHandle: PDH_HCOUNTER read FCounterHandle;
property Metric: TPdhFmtCounterValue read FMetric;
end;
type
TMetricsCollectorThread = class(TThread)
public
constructor Create(AIntervalMilSec: WORD; AIntervalEvent: TNotifyEvent);
destructor Destroy; override;
function TryAddCounter(const Element: PPdhCounterPathElements;
out HCounter: PDH_HCOUNTER): Boolean;
procedure RefilList<TTarget>(const ExportList: TList<TTarget>;
AConvertFunc: TFunc<TCollectedMetric, TPdhFmtType, TTarget>);
end;
type
TTcpSendThread = class(TThread)
public
constructor Create(const CreateSuspended: Boolean; const HostAddr: string;
HostPort, SendIntervalMSec, MaxStateCounter: Integer;
const EncodeType: IdTextEncodingType);
destructor Destroy; override;
procedure AddSendData(const Data: string); overload;
procedure AddSendData(const Data: TList<string>); overload;
end;
18
全然いじれない
• むしろそれがいい
• そのクラスが果たすべき役割が明確
• たとえば、今収集スレッドクラスはきっと分かれる。きっと。
1. 性能データの集め方を書いたクラス
2. 定期的に集めてリストに蓄えるスレッドクラス
• テストコードで品質も向上
• あんまり、書いてないですが…
19
オブジェクト指向
• これまで話したこと = オブジェクト指向のカプセル化
設計用語でいうと、誘導可能性
• オブジェクト指向の要素には色々ありますが、まず1つ
• データを更新する際に、一緒に更新する必要があるもの
=> 関数にして一緒に更新するようにする
• コード読んだら分かる、を体現したもの
• 冗長なコードを書いておいて、読んだら分かるは不親切
20
ですので
• class helper の private 問題、僕は見れなくていい派です
• private は private でしょう
21
どんないいことが?
• 責任が明確になる
• モノの関係が見えてくる
• 設計をどうすれば、ということを考えるようになる
22
まずは読んで使ってみる
• Generics.Collections のクラス当たりがよかったです
• TList.Contains() とかあったり
• Create() の引数の違いとか
23
本とかも読んでみる
• オブジェクト指向をきちんと使いたいあなたへ
(Software Design編集部 編)
• どういうメリットがあるのか、どう考えればいいのかの入りがあります
• リーダブルコード
• 直接ではないですが…
24
書いてみる
• strict private より始めてみました
• 結構思ったよりかけます。無理じゃあない。
25
さらにその先
• オブジェクト指向エクササイズ
• ある9つのルールに沿ってコーディング
• ドメイン駆動設計
• Eric Evans 氏が提唱
• いろんな人が取り組んでいて、誰もが言うのが
「まだ、道半ば」
26
まとめ
• strict private ~ public の識別子の話をしました
• クラス設計大事
• 後の追跡性とか対応とかに影響します
• 関係が明確になるので、DB 設計のヒントにもなる
27

Contenu connexe

En vedette

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

En vedette (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

20160910 可視性指定子と設計の話