flame

又见炊烟

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: :: 管理 ::
  14 随笔 :: 0 文章 :: 3 评论 :: 0 引用

Singleton
意图: 保证一个类仅有一个实例,并提供一个该实例的全局访问点。
    //单线程  懒实例化

    public class Singleton
    {
        private static Singleton instance;

        private Singleton() { }

        public static Singleton Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new Singleton();
                }
                return instance;
            }
        }
    }

    //多线程 Double-Check Locking

    public sealed class Singleton
    {
        private static volatile Singleton instance;

        private static object syncRoot = new Object();

        private Singleton() { }

        public static Singleton Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        if (instance == null)
                        {
                            instance = new Singleton();
                        }
                    }
                }
                return instance;
            }
        }
    }

    //静态初始化  类的 MSIL 定义中添加了 beforefieldinit 标记
    //内联初始化,下面两个例子等同。
    public sealed class Singleton
    {
        private static readonly Singleton instance = new Singleton();

        private Singleton() { }

        public static Singleton Instance
        {
            get
            {
                return instance;
            }
        }
    }
    public sealed class Singleton
    {
        private static readonly Singleton instance;

        static Singleton()
        {
            instance = new Singleton() ;
        }

        private Singleton() { }

        public static Singleton Instance
        {
            get
            {
                return instance;
            }
        }
    }

Abstract Factory 多系列对象的创建,简单工厂再抽象
意图: 提供一个接口,让该接口负责创建一系列“相关或者相互依赖的对象”,无需指定它们具体的类。

如果没有应对“多系列对象构建”的需求变化,则没有必要使用Abstract Factory模式,这时候使用简单的静态工厂完全可以


    //1
    public abstract class Road
    {

    }
    public abstract class Building
    {

    }
    public abstract class Tunnel
    {

    }
    public abstract class Jungle
    {

    }
    public abstract class FacilitiesFactory
    {
        public abstract Road CreateRoad();

        public abstract Building CreateBuilding();

        public abstract Tunnel CreateTunnel();

        public abstract Jungle CreateJungle();
    }
    //2
    public  class ModernRoad:Road
    {

    }
    public class ModernBuilding:Building
    {

    }
    public class ModernTunnel:Tunnel
    {

    }
    public class ModernJungle:Jungle
    {

    }
    public class ModernFacilitiesFactory:FacilitiesFactory
    {
        public override Road CreateRoad()
        {
            return new ModernRoad();
        }

        public override Building CreateBuilding()
        {
            return new ModernBuilding();
        }

        public override Tunnel CreateTunnel()
        {
            return new ModernTunnel();
        }

        public override Jungle CreateJungle()
        {
            return new ModernJungle();
        }
    }
    //Client
    class GameManager
    {
        FacilitiesFactory facilitiesFactory;

        Road road;
        Building building;
        Tunnel tunnel;
        Jungle jungle;

        public GameManager(FacilitiesFactory facilitiesFactory)
        {
            this.facilitiesFactory = facilitiesFactory;
        }

        public void BuildFacilities()
        {
            road = facilitiesFactory.CreateRoad();
            building =facilitiesFactory.CreateBuilding ();
            tunnel = facilitiesFactory.CreateTunnel();
            jungle = facilitiesFactory.CreateJungle();
        }
        public void Run()
        {
            //road.AAA();
            //road.BBB();
            //road.CCC();
            //road.DDD();
        }
    }
    class App
    {
        public void main()
        {
            GameManager g=new GameManager(new ModernFacilitiesFactory());

            g.BuildFacilities();
            g.Run();
        }
    }

Builder 生成器
意图: 将一个复杂对象的构建与其表示相分离,使得同样的构建过程可以创建不同的表示。


变化点在哪里,封装哪里—— Builder模式主要在于应对“复杂对象各个部分”的频繁需求变动。

    //1
    public abstract class Product
    {

    }
    public abstract class Part
    {

    }
    public abstract class Builder
    {
        public abstract void buildPartA();
        public abstract void buildPartB();
        public abstract void buildPartC();

        public abstract Product getResult();
    }
    //2
    public class Director
    {
        private Builder builder;
        public Director(Builder builder)
        {
            this.builder = builder;
        }
        public void construct()
        {
            builder.buildPartA();
            builder.buildPartB();
            builder.buildPartC();
        }
    }
    //文件3
    public class ConcreteProduct : Product
    {

    }
    public class ConcretePart : Part
    {

    }
    public class ConcreteBuilder : Builder
    {
        ConcretePart partA, partB, partC;

        public override void buildPartA()
        {
           
        }
        public override void buildPartB()
        {

        }
        public override void buildPartC()
        {
          
        }
        public override Product getResult()
        {
           
            return new ConcreteProduct();
        }
    }

    //我们看看如何调用Builder 模式:
    public class App
    {
        public void main()
        {
            ConcreteBuilder builder = new ConcreteBuilder();

            Director director = new Director(builder);
            director.construct();

            Product product = builder.getResult();
        }
    }

对于抽象工厂来说, 不变的是对象的接口。 而对于生成器来讲不变的是事物的结构。

Factory Method
意图: 定义一个用于创建对象的接口,让子类决定实例化哪一个类。Factory Method使得一个类的实例化延迟到子类。

    //1
    public abstract class AbstractCar
    {
        public abstract void Startup();
        public abstract void Run();
        public abstract void Turn();
        public abstract void Stop();
    }
    public abstract class CarFactory
    {
        public abstract AbstractCar CreateCar();
    }
    //2
    public class HongqiCar:AbstractCar
    {
        public override void Startup()
        {

        }
        public override void Run()
        {

        }
        public override void Turn()
        {

        }
        public override void Stop()
        {

        }
    }
    public class HongqiCarFactory : CarFactory
    {
        public override AbstractCar CreateCar()
        {
            return new HongqiCar();
        }
    }
    //3
    public class CarTestFrameWork
    {
        public void BuildContext(CarFactory carFactory)
        {
            AbstractCar abstractCar1 = carFactory.CreateCar();

            AbstractCar abstractCar2 = carFactory.CreateCar();
        }
        public void Test()
        {

        }
    }

    class App
    {
        public void main()
        {
            CarTestFrameWork carTestFrameWork = new CarTestFrameWork();
            carTestFrameWork.BuildContext(new HongqiCarFactory());
        }

    }
Prototype
意图: 使用原型实例指定创建对象的种类,然后通过拷贝这些原型来创建新的对象。

posted on 2006-06-28 10:54 又见炊烟 阅读(...) 评论(...) 编辑 收藏