|
Rarestyleへようこそ
|
|
|
|
|
|
デザインパターンでキャリアアップ
|
Bridgeパターン (デザインパターン)
Bridgeパターン 導入
Bridgeパターンは、機能と実装を分離してそれぞれ独立できるようにするパターンです。
Bridgeパターン ポイント
実装の違いで同一な機能を行いたい場合 (クラスの実装がその実行環境に依存する場合など) その実装をクラスの 機能と分離することで、それぞれの拡張を独立して行えるようになります。
補足
抽象機能(このサンプルでは,Abstractionクラス)の操作を属性でImprementor保持していますが、 Collectionとして複数の実装を考慮してもかまわないと思います。
UML Bridgeパターン
C#ソースコード
|
Sample.cs
|
|
using System;
namespace Bridge
{
public abstract
class Abstraction
{
private Implementor
imp;
public Implementor PropImplementor
{
set{this.imp=value;}
get{return this.imp;}
}
public
virtual void Operation()
{
this.PropImplementor.OperationImp();
}
}
public class
RefinedAbstraction:Abstraction
{
}
public abstract
class Implementor
{
public
abstract void OperationImp();
}
public class
ConcreteImplementor_Graphic :Implementor
{
public
override void OperationImp()
{
Console.WriteLine("映像を映し出すことができる。");
}
}
public class
ConcreteImplementor_Sound :Implementor
{
public
override void OperationImp()
{
Console.WriteLine("音声を出すことができる。");
}
}
public class
ConcreteImplementor_GraphicSound :ConcreteImplementor_Graphic
{
public
override void OperationImp()
{
base.OperationImp();
Console.WriteLine("3Dサラウンド機能搭載。");
}
}
class Class1
{
[STAThread]
static
void Main(string[] args)
{
Abstraction Radio =new RefinedAbstraction();
Abstraction TV =new RefinedAbstraction();
Abstraction HomeTheater
=new RefinedAbstraction();
Radio.PropImplementor
=new ConcreteImplementor_Sound();
TV.PropImplementor
=new
ConcreteImplementor_Graphic();
HomeTheater.PropImplementor=new ConcreteImplementor_GraphicSound();
Radio.Operation();
TV.Operation();
HomeTheater.Operation();
}
}
}
|
bridge 実行結果
UMLプラグインによる描画 Bridgeパターン
Javaソースコード Bridgeパターン
| Implementor.java |
1: package bridge;
2:
3: public abstract class Implementor {
4:
5: abstract void operationImp();
6: }
|
| Abstraction.java |
1: package bridge;
2:
3: //機能の抽象化
4:
5: //機能とはImplementor(実行操作)の組み合わせで
6: //実装されます。「機能」とはあらかた予定されているものを
7: //いいます。
8: //今回はMedia機能を抽象化することで
9: //モデルをまとめました。
10: public abstract class Abstraction {
11:
12:
13: private Implementor implementor;
14:
15: public Implementor getImplementor() {
16: return implementor;
17: }
18:
19: public void setImplementor(Implementor implementor) {
20: this.implementor = implementor;
21: }
22:
23: //実装した機能を実行します。
24: //Clientはここを実行します
25: public void operation() {
26: this.getImplementor().operationImp();
27: }
28:
29: }
|
| ConcreteImplementor_Graphic.java |
1: package bridge;
2:
3: //具象実装(グラフィック)
4: public class ConcreteImplementor_Graphic extends Implementor {
5:
6: public void operationImp() {
7: System.out.println("映像を映し出すことができる。");
8: }
9:
10: }
|
| ConcreteImplementor_Sound.java |
1: package bridge;
2:
3: //具象実装(サウンド)
4: public class ConcreteImplementor_Sound extends Implementor {
5:
6: public void operationImp() {
7: System.out.println("音声を出すことが出来る");
8: }
9:
10: }
|
| ConcreteInplementor_GraphicSound.java |
1: package bridge;
2:
3: public class ConcreteInplementor_GraphicSound extends ConcreteImplementor_Graphic {
4:
5: //overrideされます。
6: public void operationImp() {
7: super.operationImp();
8: System.out.println("3Dサラウンド機能搭載。");
9: }
10:
11: }
|
| RefinedAbstraction.java |
1: package bridge;
2:
3: //具象機能
4: //Abstractionに実行操作の実体が追加されることで
5: public class RefinedAbstraction extends Abstraction {
6:
7: }
8:
|
| Client.java |
1: package bridge;
2:
3: public class Client {
4:
5: public static void main(String[] args) {
6: //Media機能を定めます。
7: Abstraction absRadio =new RefinedAbstraction(); //ラジオ
8: Abstraction absTV =new RefinedAbstraction(); //テレビ
9: Abstraction absHomeTheater =new RefinedAbstraction(); //シアター
10: //各機能を実装します。
11: absRadio.setImplementor(new ConcreteImplementor_Sound());
12: absTV.setImplementor(new ConcreteImplementor_Graphic());
13: absHomeTheater.setImplementor(new ConcreteInplementor_GraphicSound());
14: //各機能の振る舞い
15: absRadio.operation();
16: absTV.operation();
17: absHomeTheater.operation();
18: }
19: }
|
デザインパターン「ひとくちメモ」へ
Homeへもどる
|
|
| お天気情報 |
|
東京都 東京 - 今日の天気
|
曇り
-
|
|
|
|
|
取得日:2012/02/05 17:00:00
ST:
True |
|
|
東京都 東京 - 明日の天気
|
晴のち雨
-
|
|
|
最高
|
最低
|
| 気温
|
9 |
3 |
| 湿度
|
48.2 |
37.4 |
|
|
|
|
取得日:2012/02/05 17:00:00
ST:
True |
|
|
東京都 東京 - あさっての天気
|
晴時々曇
-
|
|
|
最高
|
最低
|
| 気温
|
11 |
4 |
| 湿度
|
51.8 |
39.2 |
|
|
|
|
取得日:2012/02/05 17:00:00
ST:
True |
|
|
大阪府 大阪 - 今日の天気
|
晴のち曇
-
|
|
|
|
|
取得日:2012/02/05 17:00:00
ST:
True |
|
|
大阪府 大阪 - 明日の天気
|
雨
-
|
|
|
|
|
取得日:2012/02/05 17:00:00
ST:
True |
|
|
大阪府 大阪 - あさっての天気
|
曇時々晴
-
|
|
|
最高
|
最低
|
| 気温
|
7 |
3 |
| 湿度
|
44.6 |
37.4 |
|
|
|
|
取得日:2012/02/05 17:00:00
ST:
True |
|
| livedoorのWebサービスより提供 |
|
【工事中】お天気配信 ユーザー登録 |
|
|
|
|
|
Internet Explorer5.0以上のご利用を推奨いたします。
|
 |
Copyright 2006 Rarestyle
このページへのリンクは確認不要です。
Programming by Xenon Project Team
postmaster@rarestyle.net
免責事項について
|