디자인 모델 총화 (1)
18372 단어 c#
단순 공장
public class Operation {
public double NumberA { get; set; }
public double NumberB { get; set; }
public virtual double GetResult()
{
double result = 0;
return result;
}
}
public class OperationAdd:Operation
{
public override double GetResult()
{
double result = 0;
result = NumberA + NumberB;
return result;
}
}
public class OperationFactory
{
public static Operation createOperate(string operate)
{
Operation oper = null;
switch (operate)
{
case "+":oper = new OperationAdd(); break;
default:
break;
}
return oper;
}
}
Operation oper;
oper = OperationFactory.createOperate("+");
oper.NumberA = 3;
oper.NumberB = 5;
double result = oper.GetResult();
정책 모드 - 기본 코드
class Program
{
static void Main(string[] args)
{
Context context;
context = new Context(new ConcreteStrategyA());
context.ContextInterface();
context = new Context(new ConcreteStrategyB());
context.ContextInterface();
context = new Context(new ConcreteStrategyC());
context.ContextInterface();
Console.Read();
}
}
//
abstract class Strategy
{
//
public abstract void AlgorithmInterface();
}
// A
class ConcreteStrategyA : Strategy
{
// A
public override void AlgorithmInterface()
{
Console.WriteLine(" A ");
}
}
// B
class ConcreteStrategyB : Strategy
{
// B
public override void AlgorithmInterface()
{
Console.WriteLine(" B ");
}
}
// C
class ConcreteStrategyC : Strategy
{
// C
public override void AlgorithmInterface()
{
Console.WriteLine(" C ");
}
}
//
class Context
{
Strategy strategy;
public Context(Strategy strategy)
{
this.strategy = strategy;
}
//
public void ContextInterface()
{
strategy.AlgorithmInterface();
}
}
장식 모드
class Program
{
static void Main(string[] args)
{
Person xc = new Person(" ");
Console.WriteLine("
:");
Sneakers pqx = new Sneakers();
BigTrouser kk = new BigTrouser();
TShirts dtx = new TShirts();
pqx.Decorate(xc);
kk.Decorate(pqx);
dtx.Decorate(kk);
dtx.Show();
Console.WriteLine("
:");
LeatherShoes px = new LeatherShoes();
Tie ld = new Tie();
Suit xz = new Suit();
px.Decorate(xc);
ld.Decorate(px);
xz.Decorate(ld);
xz.Show();
Console.WriteLine("
:");
Sneakers pqx2 = new Sneakers();
LeatherShoes px2 = new LeatherShoes();
BigTrouser kk2 = new BigTrouser();
Tie ld2 = new Tie();
pqx2.Decorate(xc);
px2.Decorate(pqx);
kk2.Decorate(px2);
ld2.Decorate(kk2);
ld2.Show();
Console.Read();
}
}
class Person
{
public Person()
{ }
private string name;
public Person(string name)
{
this.name = name;
}
public virtual void Show()
{
Console.WriteLine(" {0}", name);
}
}
class Finery : Person
{
protected Person component;
//
public void Decorate(Person component)
{
this.component = component;
}
public override void Show()
{
if (component != null)
{
component.Show();
}
}
}
class TShirts : Finery
{
public override void Show()
{
Console.Write(" T ");
base.Show();
}
}
class BigTrouser : Finery
{
public override void Show()
{
Console.Write(" ");
base.Show();
}
}
class Sneakers : Finery
{
public override void Show()
{
Console.Write(" ");
base.Show();
}
}
class Suit : Finery
{
public override void Show()
{
Console.Write(" ");
base.Show();
}
}
class Tie : Finery
{
public override void Show()
{
Console.Write(" ");
base.Show();
}
}
class LeatherShoes : Finery
{
public override void Show()
{
Console.Write(" ");
base.Show();
}
}
에이전트 모드
class Program
{
static void Main(string[] args)
{
SchoolGirl jiaojiao = new SchoolGirl();
jiaojiao.Name = " ";
Proxy daili = new Proxy(jiaojiao);
daili.GiveDolls();
daili.GiveFlowers();
daili.GiveChocolate();
Console.Read();
}
}
//
interface GiveGift
{
void GiveDolls();
void GiveFlowers();
void GiveChocolate();
}
class Proxy : GiveGift
{
Pursuit gg;
public Proxy(SchoolGirl mm)
{
gg = new Pursuit(mm);
}
public void GiveDolls()
{
gg.GiveDolls();
}
public void GiveFlowers()
{
gg.GiveFlowers();
}
public void GiveChocolate()
{
gg.GiveChocolate();
}
}
class Pursuit : GiveGift
{
SchoolGirl mm;
public Pursuit(SchoolGirl mm)
{
this.mm = mm;
}
public void GiveDolls()
{
Console.WriteLine(mm.Name + " ");
}
public void GiveFlowers()
{
Console.WriteLine(mm.Name + " ");
}
public void GiveChocolate()
{
Console.WriteLine(mm.Name + " ");
}
}
class SchoolGirl
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
}
///
///
///
class Operation
{
private double _numberA = 0;
private double _numberB = 0;
public double NumberA
{
get { return _numberA; }
set { _numberA = value; }
}
public double NumberB
{
get { return _numberB; }
set { _numberB = value; }
}
///
///
///
///
public virtual double GetResult()
{
double result = 0;
return result;
}
}
///
///
///
class OperationAdd : Operation
{
public override double GetResult()
{
double result = 0;
result = NumberA + NumberB;
return result;
}
}
///
///
///
class OperationSub : Operation
{
public override double GetResult()
{
double result = 0;
result = NumberA - NumberB;
return result;
}
}
///
///
///
class OperationMul : Operation
{
public override double GetResult()
{
double result = 0;
result = NumberA * NumberB;
return result;
}
}
///
///
///
class OperationDiv : Operation
{
public override double GetResult()
{
double result = 0;
if (NumberB == 0)
throw new Exception(" 0。");
result = NumberA / NumberB;
return result;
}
}
///
///
///
interface IFactory
{
Operation CreateOperation();
}
///
/// “+”
///
class AddFactory : IFactory
{
public Operation CreateOperation()
{
return new OperationAdd();
}
}
///
/// “-”
///
class SubFactory : IFactory
{
public Operation CreateOperation()
{
return new OperationSub();
}
}
///
/// “*”
///
class MulFactory : IFactory
{
public Operation CreateOperation()
{
return new OperationMul();
}
}
///
/// “/”
///
class DivFactory : IFactory
{
public Operation CreateOperation()
{
return new OperationDiv();
}
}
원형 모드
class Program
{
static void Main(string[] args)
{
Resume a = new Resume(" ");
a.SetPersonalInfo(" ", "29");
a.SetWorkExperience("1998-2000", "XX ");
Resume b = (Resume)a.Clone();
b.SetWorkExperience("1998-2006", "YY ");
Resume c = (Resume)a.Clone();
c.SetWorkExperience("1998-2003", "ZZ ");
a.Display();
b.Display();
c.Display();
Console.Read();
}
}
//
class Resume : ICloneable
{
private string name;
private string sex;
private string age;
private WorkExperience work;
public Resume(string name)
{
this.name = name;
work = new WorkExperience();
}
private Resume(WorkExperience work)
{
this.work = (WorkExperience)work.Clone();
}
//
public void SetPersonalInfo(string sex, string age)
{
this.sex = sex;
this.age = age;
}
//
public void SetWorkExperience(string workDate, string company)
{
work.WorkDate = workDate;
work.Company = company;
}
//
public void Display()
{
Console.WriteLine("{0} {1} {2}", name, sex, age);
Console.WriteLine(" :{0} {1}", work.WorkDate, work.Company);
}
public Object Clone()
{
Resume obj = new Resume(this.work);
obj.name = this.name;
obj.sex = this.sex;
obj.age = this.age;
return obj;
}
}
//
class WorkExperience : ICloneable
{
private string workDate;
public string WorkDate
{
get { return workDate; }
set { workDate = value; }
}
private string company;
public string Company
{
get { return company; }
set { company = value; }
}
public Object Clone()
{
return (Object)this.MemberwiseClone();
}
}
class Program
{
static void Main(string[] args)
{
AbstractClass c;
c = new ConcreteClassA();
c.TemplateMethod();
c = new ConcreteClassB();
c.TemplateMethod();
Console.Read();
}
}
abstract class AbstractClass
{
public abstract void PrimitiveOperation1();
public abstract void PrimitiveOperation2();
public void TemplateMethod()
{
PrimitiveOperation1();
PrimitiveOperation2();
Console.WriteLine("");
}
}
class ConcreteClassA : AbstractClass
{
public override void PrimitiveOperation1()
{
Console.WriteLine(" A 1 ");
}
public override void PrimitiveOperation2()
{
Console.WriteLine(" A 2 ");
}
}
class ConcreteClassB : AbstractClass
{
public override void PrimitiveOperation1()
{
Console.WriteLine(" B 1 ");
}
public override void PrimitiveOperation2()
{
Console.WriteLine(" B 2 ");
}
}
외관 모드
class Program
{
static void Main(string[] args)
{
Facade facade = new Facade();
facade.MethodA();
facade.MethodB();
Console.Read();
}
}
class SubSystemOne
{
public void MethodOne()
{
Console.WriteLine(" ");
}
}
class SubSystemTwo
{
public void MethodTwo()
{
Console.WriteLine(" ");
}
}
class SubSystemThree
{
public void MethodThree()
{
Console.WriteLine(" ");
}
}
class SubSystemFour
{
public void MethodFour()
{
Console.WriteLine(" ");
}
}
class Facade
{
SubSystemOne one;
SubSystemTwo two;
SubSystemThree three;
SubSystemFour four;
public Facade()
{
one = new SubSystemOne();
two = new SubSystemTwo();
three = new SubSystemThree();
four = new SubSystemFour();
}
public void MethodA()
{
Console.WriteLine("
A() ---- ");
one.MethodOne();
two.MethodTwo();
four.MethodFour();
}
public void MethodB()
{
Console.WriteLine("
B() ---- ");
two.MethodTwo();
three.MethodThree();
}
}
건설 자 모드
class Program
{
static void Main(string[] args)
{
Director director = new Director();
Builder b1 = new ConcreteBuilder1();
Builder b2 = new ConcreteBuilder2();
director.Construct(b1);
Product p1 = b1.GetResult();
p1.Show();
director.Construct(b2);
Product p2 = b2.GetResult();
p2.Show();
Console.Read();
}
}
class Director
{
public void Construct(Builder builder)
{
builder.BuildPartA();
builder.BuildPartB();
}
}
abstract class Builder
{
public abstract void BuildPartA();
public abstract void BuildPartB();
public abstract Product GetResult();
}
class ConcreteBuilder1 : Builder
{
private Product product = new Product();
public override void BuildPartA()
{
product.Add(" A");
}
public override void BuildPartB()
{
product.Add(" B");
}
public override Product GetResult()
{
return product;
}
}
class ConcreteBuilder2 : Builder
{
private Product product = new Product();
public override void BuildPartA()
{
product.Add(" X");
}
public override void BuildPartB()
{
product.Add(" Y");
}
public override Product GetResult()
{
return product;
}
}
class Product
{
IList parts = new List();
public void Add(string part)
{
parts.Add(part);
}
public void Show()
{
Console.WriteLine("
----");
foreach (string part in parts)
{
Console.WriteLine(part);
}
}
}
본 고 는 블 로그 에서 한 글 을 여러 발 로 보 내 는 등 운영 도구 플랫폼 이다.
OpenWrite 발표
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
C#Task를 사용하여 비동기식 작업을 수행하는 방법라인이 완성된 후에 이 라인을 다시 시작할 수 없습니다.반대로 조인(Join)만 결합할 수 있습니다 (프로세스가 현재 라인을 막습니다). 임무는 조합할 수 있는 것이다. 연장을 사용하여 그것들을 한데 연결시키는 것이...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.