1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
namespace ConsoleApplication1 { class Program { static void Main(string[] args) { } } /* Visitor 訪問者であるVisitorに処理を記述する。 処理対象はAcceptorでVisitorを受け入れる必要がある。 */ interface Visitor { void Visit(Acceptor acceptor); } class ConcreateVisitor : Visitor { public void Visit(Acceptor acceptor) { } } interface Acceptor { void Accept(Visitor visitor); } class ConcreateAcceptor: Acceptor { public void Accept(Visitor visitor) { visitor.Visit(this); } } } |
C# Decorator
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
namespace ConsoleApplication1 { class Program { static void Main(string[] args) { } } /* Decorator */ interface Component { void Operation(); } class ConcreateComponent: Component { public void Operation() { } } class Decorator: Component { private Component component; public Decorator(Component c) { this.component = c; } public void Operation() { } public void Operation2() { //拡張機能 } } } |
C# Composite
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
namespace ConsoleApplication1 { class Program { static void Main(string[] args) { } } /* Composite ファイルとフォルダのような複数の要素に共通のインターフェース。*/ interface Component { void operation(); } class Leaf: Component { public void operation() { } } class Composite: Component { public void operation() { } //再帰構造の場合、compositeにadd,removeなど定義 } } |
C# Strategy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Context c = new Context(new ConcreateStrategy1()); c.Method(); } } /* Strategy アルゴリズムだけ抜き出してアルゴリズムを切り替えやすくする。*/ class Context { private Strategy strategy = null; public Context(Strategy strategy) { this.strategy = this.strategy = strategy; } public void Method() { this.strategy.Method(); } } interface Strategy { void Method(); } class ConcreateStrategy1: Strategy { public void Method() { //アルゴリズム1 } } class ConcreateStrategy2 : Strategy { public void Method() { //アルゴリズム2 } } } |
C# Bridge
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
namespace ConsoleApplication1 { class Program { static void Main(string[] args) { //abstraction生成時にConcreateImplementorのインスタンスを渡す。 Abstraction abstraction = new Abstraction(new ConcreateImplementor()); } } /* Bridge 機能と実装の分離 機能実装の度の継承をなくため、機能実装専用のインターフェースを利用。 */ public class Abstraction { private Implementor implementor; public void function() { //コンストラクタで保持したオブジェクトに処理を任せる。 this.implementor.implementation(); } public Abstraction(Implementor i) { this.implementor = i; } } public class RefinedAbstraction: Abstraction { public RefinedAbstraction(Implementor i) : base(i) { } public void RefinedMethod1() { //追加機能 } } //機能実装クラスの抽象と具象。 public abstract class Implementor { public abstract void implementation(); } public class ConcreateImplementor: Implementor { public override void implementation() { //実装 } } } |
C# AbstractFactory
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
namespace ConsoleApplication1 { class Program { static void Main(string[] args) { } } /* AbstractFactory 関連ある複数インスタンスをまとめて生成。それぞれの整合性を保証する */ public abstract class AbstractFactory { //各パーツはお互いに関連性がある。 abstract public ConcreateProduct1 CreateProduct1(); abstract public ConcreateProduct2 CreateProduct2(); } public class ConcreateFactory:AbstractFactory { //このファクトリを使うことでパーツの関連性が保証される。 public override ConcreateProduct1 CreateProduct1() { return new ConcreateProduct1(); } public override ConcreateProduct2 CreateProduct2() { return new ConcreateProduct2(); } } public class AbstractProduct1 { } public class AbstractProduct2 { } public class ConcreateProduct1: AbstractProduct1 { } public class ConcreateProduct2 : AbstractProduct2 { } } |
C# Builder
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
namespace ConsoleApplication1 { class Program { static void Main(string[] args) { //ConcreateBuilderはBuilderで提供されたインターフェースのみ。 //DirectorにConcreateBuilderのインスタンスを渡す。 Director d = new Director(new ConcreateBuilder()); /* Directorは内部Concstructの中で、 BuilderインターフェースのBuildPart,GetResultを呼び出す。*/ } } /* Builder Directorは作成過程を決定、Builderは表現形式を決定する。*/ class Product{} abstract class Builder { abstract public void BuildPart(); abstract public Product GetResult(); } class ConcreateBuilder: Builder { Product product; public ConcreateBuilder() { this.product = new Product(); } public override Product GetResult() { return product; } public override void BuildPart() { //処理 } } class DiffConcreateBuilder : Builder { Product product; public DiffConcreateBuilder() { this.product = new Product(); } public override Product GetResult() { return product; } public override void BuildPart() { //チョット違う処理 } } class Director { Builder builder; public Director(Builder builder) { this.builder = builder; } public object Construct() { this.builder.BuildPart(); return this.builder.GetResult(); } } } |
C# Prototype
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
namespace ConsoleApplication1 { class Program { static void Main(string[] args) { ConcreatePrototype c1 = new ConcreatePrototype("name"); ConcreatePrototype c2 = (ConcreatePrototype)c1.CreateClone(); } } /* Prototype 複製を作成するメソッドを用意する。*/ public interface Prototype { Prototype CreateClone(); } public class ConcreatePrototype : Prototype { private string name; public ConcreatePrototype() { } public ConcreatePrototype(string name) { this.name = name; } public Prototype CreateClone() { ConcreatePrototype c = new ConcreatePrototype(); c.name = this.name; return c; } } } |
C# 2二次関数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
using System; using System.Drawing; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Graphics g = CreateGraphics(); Pen p = new Pen(Color.Black, 5);//線幅 for (int x = 10; x <= 30; x++) { g.DrawEllipse(p, x * 10, (int)Math.Pow(20-x,2), 5, 5);//x,y,width,height } } } } |
C# Adapter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
using System.Windows.Forms; namespace WindowsFormsApp2 { public partial class Form1 : Form { public Form1() { InitializeComponent(); ITarget target = new Adapter(new Adaptee()); target.Request(); // Adapteeはコンストラクタで渡さず、内部で生成している場合もある。 ITarget target2 = new Adapter2("Hello World"); target2.Request(); } } // Wrapperパターンともいう。(Adapterがラッパー) // Adapteeが実績のあるライブラリで再利用したい場合など // 基本は、Clientからは(Adapterのインスタンスを持った)Targetを利用する。 // Adapterの中ではTargetのメソッドがAdapteeのメソッドを呼んでいるということ。 public interface ITarget { void Request(); // 必要な機能 } public class Adapter : ITarget { private Adaptee adaptee; public Adapter(Adaptee target) { this.adaptee = target; } public void Request() { adaptee.AdapteeMethod(); } } public class Adaptee { public void AdapteeMethod() { // 実際に提供されている機能 } } // -------------------------------------------------------- public class Adapter2 : ITarget { private Adaptee2 adaptee2; private string extra; public Adapter2(string extra) { this.extra = extra; this.adaptee2 = new Adaptee2(); } public void Request() { MessageBox.Show(this.extra); adaptee2.Adaptee2Method(); } } public class Adaptee2 { public void Adaptee2Method() { // 実際に提供されている機能 } } } |